[英]Linux Bash Shell Script: How to “ls” a directory and check some output string?
我只是Linux Shell Scripting的新手。 我需要知道的是,通常在命令行中,我们只需使用:
# ls /var/log
audit ConsoleKit cups maillog messages ntpstats secure-20130616 spooler-20130623 vsftpd.log-20130616
boot.log cron dmesg maillog-20130616 messages-20130616 prelink secure-20130623 spooler-20130701 vsftpd.log-20130623
... . . . ..
然后
# ls /var/aaaaaaaaaaaaaaa
ls: cannot access /var/aaaaaaaaaaaaaaa: No such file or directory
所以使用Shell脚本:
# ls /var/aaaaaaaaa
ls: cannot access
? 注意 :您可能会问我是否只想检测故障。 或输出字符串。 我非常渴望知道这两种方式。 谢谢。
要检查目录:
if [ ! -d '/var/aaaaaaa' ]; then
echo 'no dir!'
fi
对于文件:
if [ ! -f '/var/aaaaaaa' ]; then
echo 'no file!'
fi
检查输出:
if ls '/var/aaaaaaa' 2>&1 | grep 'No such'; then
echo 'no such';
fi
要检查ls何时失败:
if ! ls '/var/aaaaaaa' &> /dev/null; then
echo 'failed'
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.