所以我在我的shell程序中'if [-s $ fname]'。有人可以告诉我它的作用吗?我搜索了它,但没有关于'-s'的信息。
if [ -s $fname ]
then
echo -e "\tname\tph\t\tcity\tpin\tstate"
cat $fname
else
echo -e "\nfile is empty"
fi
;;
这是我的代码的一部分,其中-s用于你需要它。谢谢!
答案 0 :(得分:1)
你的问题本身就有答案: - )
如果变量-s $fname
中的文件存在且非空,则 True
表达式求值为$fname
。因此,如果$fname
文件不存在或为空,那么您的其他屏蔽将被激活并且代码段打印为file is empty
几个例子,
fname="test.txt"
## case when the file is not existing
if [ -s $fname ]
then
echo -e "case-1: $fname exists and is not empty"
cat $fname
else
echo -e "case-1: $fname do not exists or is empty"
fi
## case when the file exists but is of empty size
touch $fname
if [ -s $fname ]
then
echo -e "case-2: $fname exists and is not empty"
cat $fname
else
echo -e "case-2: $fname do not exists or is empty"
fi
## case when the file exists and is having contents too
echo "now its not empty anymore" > $fname
if [ -s $fname ]
then
echo -e "case-3: $fname exists and is not empty"
cat $fname
else
echo -e "case-3: $fname do not exists or is empty"
fi
输出是,
case-1: test.txt do not exists or is empty
case-2: test.txt do not exists or is empty
case-3: test.txt exists and is not empty
now its not empty anymore