什么命令检查Bash shell脚本中是否存在目录?
当前回答
if [ -d "$Directory" -a -w "$Directory" ]
then
#Statements
fi
上述代码检查目录是否存在以及是否可写。
其他回答
使用文件程序。考虑到所有目录也是Linux中的文件,发出以下命令就足够了:
文件$directory_name
检查不存在的文件:文件blah
输出:无法打开“blah”(没有这样的文件或目录)
检查现有目录:文件bluh
输出:bluh:目录
file="foo"
if [[ -e "$file" ]]; then echo "File Exists"; fi;
我发现双括号版本的测试使编写逻辑测试更自然:
if [[ -d "${DIRECTORY}" && ! -L "${DIRECTORY}" ]] ; then
echo "It's a bona-fide directory"
fi
较短形式:
# if $DIR is a directory, then print yes
[ -d "$DIR" ] && echo "Yes"
[[ -d "$DIR" && ! -L "$DIR" ]] && echo "It's a directory and not a symbolic link"
注:引用变量是一种很好的做法。
说明:
-d: 检查是否是目录-五十: 检查是否是符号链接