-e <file>:文件是否存在(exists) -f <file>:文件存在且是一个常规文件(file) -d <file>:文件存在且是一个目录(directory) -r <file>:文件存在且有读权限(read) -w <file>:文件存在且有写权限(write) -x <file>:文件存在且有执行权限 (execute) -s <file>:文件存在且文件大小大于0(size) -S <...
用 man test 查看 test 命令的帮助:-h FILE FILE exists and is a symbolic link (same as -L)文件存在并且是一个字符链接(与-L选项相同)。! EXPRESSION EXPRESSION is false 表达式为false,即“非”操作(“取反”操作)。所以,这句判断语句的意思就是:如果 ../../Home 这个文件不存在...
if [ $var -eq 0 ]; then echo "True"; fi 1. 也可以写成: if test $var -eq 0; then echo "True"; fi 1. 说明:test 是一个外部程序,需要衍生出对应的进程,而Bash是一个内部函数,因此Bash的执行效率更高。test兼容于 Bourne shell,ash,dash等。
来自:Linux迷链接:https://www.linuxmi.com/bash-linux-test-command.html Linux test 命令是 Shell 内置命令,用来检测某个条件是否成立。test 通常和 if 语句一起使用,并且大部分 if 语句都依赖 test。可以将一个元素与另一个元素进行比较,但它更常用于BASH shell 脚本中,作为控制逻辑和程序流程 的条件语句的...
#!/bin/bash # 定义文件路径 FILE_PATH="test" # 判断文件是否存在 if [ ! -e "$FILE_PATH" ]; then echo "do not exist" else echo "exists" fi 给予执行权限: 在终端中,运行以下命令给予脚本执行权限: bash chmod +x check_file.sh 运行脚本: 执行脚本以检查文件是否存在: bash ./check_...
Linux test 命令是 Shell 内置命令,用来检测某个条件是否成立。test 通常和 if 语句一起使用,并且大部分 if 语句都依赖 test。可以将一个元素与另一个元素进行比较,但它更常用于BASH shell 脚本中,作为控制逻辑和程序流程 的条件语句的一部分。 test 命令有很多选项,可以进行数值、字符串和文件三个方面的检测。
•-d FILE:检测文件是否是目录。 •-r FILE:检测文件是否可读。 •-w FILE:检测文件是否可写。 •-x FILE:检测文件是否可执行。 下面是一些示例用法: #检测文件是否存在 iftest-e then echo" exists" fi #检测文件是否是目录,并输出信息 iftest-d/path/to/directory then echo"It is a directory...
bash if [ -f "/path/to/file" ]; then echo "file exists" fi if [ -d"/path/to/directory" ]; then echo "directory exists" fi if [ -r "/path/to/file" ]; then echo "file is readable" fi if [ -w "/path/to/file" ]; then echo "file is writable" fi 七、使用逻辑操作符 ...
/bin/bash str1="str1" str2="str2" str3="" if test -z $str3 then echo "长度为0" # 打印 else echo "长度不为0" fi if test $str1 = $str2 then echo "字符串相等" else echo "字符串不相等" # 打印 fi 文件测试 -e 文件名 如果文件存在则为真 # e: exists...
-a file True if file exists. (Not avail- able in sh.) -b file True if file exists and is a block special file. -c file True if file exists and is a character special file. -d file True if file exists and is a directory. ...