首页 > 解决方案 > #!/bin/csh Cshell 检查查找结果 if else

问题描述

目标:检查查找结果,如果为空则回显某些内容,如果不是则回显查找结果

#!/bin/csh
set mtime = 0;
echo "<i>Title</i>" >> $outputfile
set text=`find start*middle*.txt -mtime $mtime -ls`
echo "$text" >> $outputfile

我已经尝试了以下(全部失败)......

if (-f "$text" ) then
    echo Exists
else
    echo No such file
endif

-

if [[ -n $(find start*middle*.txt -mtime $mtime -ls) ]]
then
 echo "res" >> $outputfile
fi

-

if [[ -n "$text" ]]
then
 echo "res" >> $outputfile
fi

标签: linuxshellcsh

解决方案


#!/bin/csh
set outputfile = test
set mtime = 0;
echo "<i>Title</i>" >> $outputfile
set text=`find start*middle*.txt -ls`
if ("$text" != "") then
        echo "exists"
else
        echo "dosn't exist"
endif
echo "$text" >> $outputfile

如果您需要使用该-mtime选项,则值为 0 是不正确的。如果要列出在最后一天创建的文件,则需要-mtime -12 天-mtime -2


推荐阅读