如何检查文件是否存在于 bash 脚本的特定目录中?
How to check if a files exists in a specific directory in a bash script?
这是我一直在尝试的,但没有成功。如果我想检查 ~/.example 目录中是否存在文件
FILE=
if [ -e $FILE ~/.example ]; then
echo "File exists"
else
echo "File does not exist"
fi
您可以使用$FILE
与目录拼接成完整路径如下。
FILE=""
if [ -e ~/.myexample/"$FILE" ]; then
echo "File exists"
else
echo "File does not exist"
fi
应该这样做:
FILE=
if [[ -e ~/.example/$FILE && ! -L ~/example/$FILE ]]; then
echo "File exists and not a symbolic link"
else
echo "File does not exist"
fi
它将告诉您 $FILE
是否存在于 .example
目录中,忽略符号链接。
你也可以用这个:
[[ -e ~/.example/$FILE && ! -L ~/example/$FILE ]] && echo "Exists" || echo "Doesn't Exist"
来晚了,但一个简单的解决方案是使用 -f
if [[ ! -f $FILE]]
then
echo "File does not exist"
fi
再举几个例子here如果你好奇的话
这是我一直在尝试的,但没有成功。如果我想检查 ~/.example 目录中是否存在文件
FILE=
if [ -e $FILE ~/.example ]; then
echo "File exists"
else
echo "File does not exist"
fi
您可以使用$FILE
与目录拼接成完整路径如下。
FILE=""
if [ -e ~/.myexample/"$FILE" ]; then
echo "File exists"
else
echo "File does not exist"
fi
应该这样做:
FILE=
if [[ -e ~/.example/$FILE && ! -L ~/example/$FILE ]]; then
echo "File exists and not a symbolic link"
else
echo "File does not exist"
fi
它将告诉您 $FILE
是否存在于 .example
目录中,忽略符号链接。
你也可以用这个:
[[ -e ~/.example/$FILE && ! -L ~/example/$FILE ]] && echo "Exists" || echo "Doesn't Exist"
来晚了,但一个简单的解决方案是使用 -f
if [[ ! -f $FILE]]
then
echo "File does not exist"
fi
再举几个例子here如果你好奇的话