Bash教程

Bash 写入文件

Bash 写入文件

当我们在 bash shell 中运行任何命令时,它通常会将该命令的输出打印到终端,以便我们可以立即读取它。但是 bash 还提供了一个选项,可以将任何 bash 命令的输出"重定向"到日志文件。它可以将输出保存到文本文件中,以便我们以后需要时可以查看它。

方法 1: 仅将输出写入文件

To将 Bash 命令的输出写入文件,我们可以使用右尖括号符号(>) 或双直角符号(>>):

右尖括号符号(>)

用于将bash命令的输出写入磁盘文件。如果没有指定名称的文件,则它会创建一个具有相同名称的新文件。如果指定名称的文件存在,则文件内容将被覆盖。

双直角符号(>>)

使用将 bash 命令的输出写入文件,将输出附加到文件的现有内容。如果文件不存在,它会创建一个具有指定名称的新文件。
从技术上讲,这两个运算符都将"stdout(标准输出)"重定向到一个文件。
简单来说,当我们第一次写入文件并且不希望文件中存在以前的数据时,我们应该使用右尖括号符号(>)。如果内容已经存在于文件中,它将覆盖内容。在进一步的脚本中,我们可以使用双直角符号(>>) 将数据附加到文件中。

示例

'ls'命令用于打印当前目录中存在的所有文件和文件夹。但是当我们运行带有右尖括号(>) 的 'ls' 命令时,它不会将文件和文件夹列表打印到屏幕上。它将输出保存到我​​们用它指定的文件中,即,如下所示:
Bash Script
#!/bin/bash
#Script to write the output into a file
#Create output file, override if already present
output=output_file.txt
#Write data to a file
ls > $output
#Checking the content of the file
gedit output_file.txt
输出
Bash Write File
如图所示, 'ls' 命令的输出被重定向到一个文件中。要将文件的内容打印到终端,我们可以使用以下形式的"cat"命令:
Bash 脚本
#!/bin/bash
#Script to write the output into a file
#Create output file, override if already present
output=output_file.txt
#Write data to a file
ls > $output
#Printing the content of the file
cat $output
输出
Bash Write File
如果我们想要要将多个命令的输出重定向到单个文件而不删除可用数据,那么我们可以使用 >> 运算符。假设我们想将系统信息附加到指定的文件中,我们可以通过以下方式实现:
Bash Script
#!/bin/bash
#Script to write the output into a file
#Create output file, override if already present
output=output_file.txt
#Write data to a file
ls > $output
#Appending the system information
uname-a >> $output
#Checking the content of the file
gedit output_file.txt
输出
Bash Write File
这里,第二个命令的结果被附加到文件的末尾。
我们可以多次重复这个过程以将输出附加到文件的末尾。

方法 2: 正常打印输出并写入文件

有些人可能不喜欢使用 > 或 >> 运算符将输出写入文件,因为终端中不会有命令输出.这就是使用"tee"命令的原因。 'tee' 命令用于将它接收到的输入打印到屏幕上。它可以同时将输出保存到一个文件中。
Bash Script
#!/bin/bash
#Script to write the output into a file
#Create output file, override if already present
output=output_file.txt
#Write data to a file
ls | tee $output
输出
Bash Write File
这将覆盖文件的内容,就像 > 操作符一样,但也会在屏幕上打印输出。
如果我们想使用 tee 命令将输出写入文件而不删除文件的内容,我们可以使用下面的表单也会将输出打印到终端:
Bash Script
#!/bin/bash
#Script to write the output into a file
#Create output file, override if already present
output=output_file.txt
echo "<<<List of Files and Folders>>>" | tee-a $output
#Write data to a file
ls | tee $output
echo | tee-a $output
#Append System Information to the file
echo "<<<OS Name>>>" | tee-a $output
uname | tee-a $output
输出
Bash Write File
这不会只将输出附加到文件的末尾,而且还会在屏幕上打印输出。

Bash 检查文件是否存在

大多数时候,我们可能会发现需要执行一个操作来检查文件是否存在。
在Bash中,我们可以使用'test command'来检查文件是否存在并判断文件的类型。
以下是test命令的语法,我们可以使用这些命令中的任何一个:
test expression
[ expression ]
[[ expression ]]
我们需要使用单个括号"["命令来使我们的脚本可移植到所有 POSIX shell。 test 命令的升级版本包含双括号"[[",大多数现代系统都支持使用 Bash、Zsh 和 Ksh 作为默认 shell。

检查文件是否存在

在检查文件是否存在时,最常用的文件运算符是-e 和-f。 '-e' 选项用于检查文件是否存在而不考虑类型,而 '-f' 选项用于仅当文件是常规文件(不是目录或设备)时才返回真值。
检查文件是否存在的最常用选项是使用带有"if 条件语句"的测试命令。
以下是检查"read_file.txt"是否存在的示例。 txt'文件存在:
方法一
#!/bin/bash
File=read_file.txt
if test-f "$File"; then
echo "$File exist "
fi
方法二
#!/bin/bash
File=read_file.txt
if [-f "$File" ]; then
echo "$File exist "
fi
方法三
#!/bin/bash
File=read_file.txt
if [[-f "$File" ]]; then
echo "$File exist "
fi
输出
所有三种方法的输出如下,因为我们在目录中有一个文件(read_file.txt):
     read_file.txt exist
如果我们想执行一个动作,根据文件是否存在提供结果,我们可以使用 if/then 结构如下:
示例
#!/bin/bash
File=read_file.txt
if [-f "$File" ]; then
echo "$File exist"
else
echo "$File does not exist"
fi
输出
     read_file.txt exist
我们也可以使用不带if语句的test命令。我们可以使用以下任何一种方法:
方法 1
#!/bin/bash
File=read_file.txt
test-f read_file.txt && echo "$File exist"
方法二
#!/bin/bash
File=read_file.txt
[-f read_file.txt ] && echo "$File exist"
方法三
#!/bin/bash
File=read_file.txt
[[-f read_file.txt ]] && echo "$File exist"
输出
所有三种方法的输出如下,因为我们在目录中有一个文件(read_file.txt):
     read_file.txt exist
如果在&&操作符后有多个命令要运行,则将这些命令用大括号括起来,用分号(;)或AND(&&)分隔,即:
示例
#!/bin/bash
File=read_file.txt
[-f read_file.txt ] && { echo "$File exist"; echo "Task Completed"; }
与&&不同,||后面的语句仅当测试命令的退出状态为"false"时才执行运算符。
示例
#!/bin/bash
File=read_file.txt
[-f read_file.txt ] && echo "$File exist" || echo "$File does not exist"
输出
     read_file.txt exist
这些是Bash中常用的检查文件是否存在的方法。

检查目录是否存在

操作符'-d'允许我们测试文件是否为目录。
以下是检查'lidihuo'目录是否存在的方法:
方法1
#!/bin/bash
File=lidihuo
if [-d "$File" ]; then
echo "$File is a directory"
fi
方法二
#!/bin/bash
File=lidihuo
[-d "$File" ] && echo "$File is a directory"
注意: 我们也可以使用双括号'[['代替单括号'['。
输出
以上两种方法的输出如下,因为我们在指定位置有一个目录(名为lidihuo) :
     lidihuo is a directory

检查 IF 文件不存在

可以使用感叹号(!-逻辑非运算符)否定测试表达式。查看以下示例:
示例
#!/bin/bash
File=missing_read_file.txt
if [ !-f "$File" ]; then
echo "$File does not exist"
fi
上面的脚本也可以写成如下:
#!/bin/bash
File=missing_read_file.txt
[ !-f "$File" ] && echo "$File unavailable"
输出
     missing_read_file.txt unavailable

文件测试操作符

测试命令包括以下文件操作符,它们允许我们测试特定类型的文件:
-b File Returns 'True' 如果 FILE 作为块特殊文件存在。
-c File Returns 'True' 如果 FILE 作为特殊字符文件存在。
-d File Returns 'True' 如果 FILE 作为目录存在。
-e File Returns 'True' 如果 FILE 作为文件存在,无论类型如何(节点、目录、套接字等)。
-f File Returns 'True' 如果 FILE 作为常规文件(不是目录或设备)存在。
-G File Returns 'True' 如果 FILE 存在并且包含与运行命令的用户相同的组。
-h File Returns 'True' 如果 FILE 作为符号链接存在。
-g File Returns 'True' 如果 FILE 存在并且包含 set-group-id(sgid) 标志集。
-k File Returns 'True' 如果 FILE 存在并且包含粘滞位标志集。
-L File Returns 'True' 如果 FILE 作为符号链接存在。
-O File Returns 'True' 如果 FILE 存在并且由运行命令的用户拥有。
-p File Returns 'True' 如果 FILE 作为管道存在。
-r File Returns 'True' 如果 FILE 作为可读文件存在。
-S File Returns 'True' 如果 FILE 作为套接字存在。
-s File Returns 'True' 如果 FILE 存在且大小非零。
-u File Returns 'True' 如果 FILE 存在,并且设置了 set-user-id(suid) 标志。
-w File Returns 'True' 如果 FILE 作为可写文件存在。
-x File Returns 'True' 如果 FILE 作为可执行文件存在。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4