1. 判断目录不存在
dir="directory"
if [ ! -d $dir ]; then
echo "$dir not exist"
fi
2. 判断目录为空
dir="directory"
if [ "$(ls -A $dir)" == "" ]; then
echo "$dir is empty"
fi
3. 判断文件存在
file="script"
if [ -f $file ]; then
echo "$file exist"
fi
4. 判断字符串为空
if [ -z "$var" ]; then
echo "string empty"
fi
5. 判断真假
var=true
if [ $var ]; then
echo "var is true"
fi
这篇博客介绍了如何使用Linux shell脚本来进行条件判断,包括检查目录是否存在、是否为空,文件是否存在,以及字符串是否为空。通过这些基本的shell脚本技巧,可以更好地进行自动化任务的编写和系统管理。

53万+

被折叠的 条评论
为什么被折叠?



