这将检查文件是否存在:
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
echo "File $FILE exists."
else
echo "File $FILE does not exist."
fi
如何仅检查文件是否不存在?
这将检查文件是否存在:
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
echo "File $FILE exists."
else
echo "File $FILE does not exist."
fi
如何仅检查文件是否不存在?
当前回答
In
[ -f "$file" ]
[命令对$file中存储的路径执行stat()(而不是lstat())系统调用,如果系统调用成功并且stat()返回的文件类型为“正则”,则返回true。
因此,如果[-f“$file”]返回true,则可以判断该文件确实存在,并且是一个常规文件或符号链接,最终解析为常规文件(或者至少在stat()时是这样)。
但是,如果它返回false(或者如果[!-f“$file”]或![-f“$file“]返回true),则有很多不同的可能性:
文件不存在该文件存在,但不是常规文件(可以是设备、fifo、目录、套接字…)文件存在,但您没有对父目录的搜索权限文件存在,但访问该文件的路径太长该文件是到常规文件的符号链接,但您没有对符号链接解析中涉及的某些目录的搜索权限。…stat()系统调用可能失败的任何其他原因。
简而言之,它应该是:
if [ -f "$file" ]; then
printf '"%s" is a path to a regular file or symlink to regular file\n' "$file"
elif [ -e "$file" ]; then
printf '"%s" exists but is not a regular file\n' "$file"
elif [ -L "$file" ]; then
printf '"%s" exists, is a symlink but I cannot tell if it eventually resolves to an actual file, regular or not\n' "$file"
else
printf 'I cannot tell if "%s" exists, let alone whether it is a regular file or not\n' "$file"
fi
为了确定文件不存在,我们需要stat()系统调用返回错误代码ENOENT(ENOTDIR告诉其中一个路径组件不是目录,这是另一种情况,我们可以通过该路径判断文件不存在)。不幸的是,[命令没有让我们知道这一点。无论错误代码是ENOENT、EACESS(权限被拒绝)、ENAMETOOLONG或其他什么,它都会返回false。
[-e“$file”]测试也可以使用ls-Ld--“$file“>/dev/null完成。在这种情况下,ls将告诉您stat()失败的原因,尽管这些信息不能很容易地以编程方式使用:
$ file=/var/spool/cron/crontabs/root
$ if [ ! -e "$file" ]; then echo does not exist; fi
does not exist
$ if ! ls -Ld -- "$file" > /dev/null; then echo stat failed; fi
ls: cannot access '/var/spool/cron/crontabs/root': Permission denied
stat failed
至少ls告诉我它失败并不是因为文件不存在。这是因为它无法判断文件是否存在。[命令只是忽略了这个问题。
使用zsh shell,您可以在[命令失败后使用$ERRNO特殊变量查询错误代码,并使用zsh/system模块中的$errnos特殊数组解码该数字:
zmodload zsh/system
ERRNO=0
if [ ! -f "$file" ]; then
err=$ERRNO
case $errnos[err] in
("") echo exists, not a regular file;;
(ENOENT|ENOTDIR)
if [ -L "$file" ]; then
echo broken link
else
echo does not exist
fi;;
(*) syserror -p "can't tell: " "$err"
esac
fi
(注意,当使用最新版本的gcc构建zsh时,$errnos支持被一些版本破坏)。
其他回答
[[ -f $FILE ]] || printf '%s does not exist!\n' "$FILE"
此外,该文件可能是一个断开的符号链接,或者是一个非常规文件,例如套接字、设备或fifo。例如,要添加断开符号链接的检查:
if [[ ! -f $FILE ]]; then
if [[ -L $FILE ]]; then
printf '%s is a broken symlink!\n' "$FILE"
else
printf '%s does not exist!\n' "$FILE"
fi
fi
使用!:否定测试中的表达式([是其别名)
#!/bin/bash
FILE=$1
if [ ! -f "$FILE" ]
then
echo "File $FILE does not exist"
fi
相关的手册页是man-test,或者,对于内置的bash命令,相当于man[-或help-test或help]。
或者(不常用)您可以使用以下方法否定测试结果:
if ! [ -f "$FILE" ]
then
echo "File $FILE does not exist"
fi
该语法在“管道”和“复合命令”部分的“man 1 bash”中进行了描述。
我更喜欢使用POSIX shell兼容格式执行以下一行代码:
$ [ -f "/$DIR/$FILE" ] || echo "$FILE NOT FOUND"
$ [ -f "/$DIR/$FILE" ] && echo "$FILE FOUND"
对于一些命令,就像我在脚本中所做的那样:
$ [ -f "/$DIR/$FILE" ] || { echo "$FILE NOT FOUND" ; exit 1 ;}
一旦我开始这样做,我就很少再使用完全类型化的语法了!!
如果要使用test而不是[],则可以使用!要获得否定:
if ! test "$FILE"; then
echo "does not exist"
fi
要测试文件是否存在,参数可以是以下任一项:
-e: Returns true if file exists (regular file, directory, or symlink)
-f: Returns true if file exists and is a regular file
-d: Returns true if file exists and is a directory
-h: Returns true if file exists and is a symlink
以下所有测试都适用于常规文件、目录和符号链接:
-r: Returns true if file exists and is readable
-w: Returns true if file exists and is writable
-x: Returns true if file exists and is executable
-s: Returns true if file exists and has a size > 0
示例脚本:
#!/bin/bash
FILE=$1
if [ -f "$FILE" ]; then
echo "File $FILE exists"
else
echo "File $FILE does not exist"
fi