嗨,我在scrpt中使用find命令来获取一天内修改的日志文件列表,并使用该列表进行grep。但是,如果没有找到文件,那么它就不会出现shell问题。我可以在使用命令之前使用任何if条件进行检查。如果是,如何检查。

#!/bin/bash
grep 'EXSTAT|' $(find . -mtime 0 -type f)|grep '|S|'|/usr/xpg4/bin/awk -F"|" '{a[$11]++;c[$11]+=$14}END{for(b in a){print b"," a[b]","c[b]/a[b]}}'


我只在下面尝试了grep,但没有响应,我必须通过CTRL + C终止。

bash-3.2$ ls -ltr
total 126096
-rw-r-----   1 tibco    tibco    10486146 Sep  4 09:20 ivrbroker.log.6
-rw-r-----   1 tibco    tibco    10486278 Sep  9 14:45 ivrbroker.log.5
-rw-r-----   1 tibco    tibco    10492782 Sep 14 14:54 ivrbroker.log.4
-rw-r-----   1 tibco    tibco    10487657 Sep 16 13:17 ivrbroker.log.3
-rw-r-----   1 tibco    tibco    10486437 Oct 29 10:26 ivrbroker.log.2
-rw-r-----   1 tibco    tibco    10485955 Nov 17 11:28 ivrbroker.log.1
-rw-r-----   1 tibco    tibco    1537673 Nov 18 08:48 ivrbroker.log

bash-3.2$ find . -mtime 0 -type f
bash-3.2$ grep 'EXSTAT|' $(find . -mtime 0 -type f)
#!/bin/bash
bnkpath=/tibcouat1_fs/tibco/deployment/egypt/bnk/broker/logs/
file_list=$(find $bnkpath -mtime 0 -type f)
if [ -z $file_list ]; then
echo "No log file found"
else
echo "log file found"
grep 'EXSTAT|' $(find $file_list -mtime 0 -type f)|grep '|S|'|/usr/xpg4/bin/awk -F"|" '{a[$11]++;c[$11]+=$14}END{for(b in a){print b"," a[b]","c[b]/a[b]}}'
fi
bash-3.2$ ./bnk1.sh
./bnk1.sh: line 4: [: too many arguments
log file found

最佳答案

因为find命令返回NO文件名,所以grep命令保留了终端。这将与执行有效地产生相同的影响

grep 'EXSTAT|'


这是因为grep期望自己执行一些输入,如果没有给出任何输入(在这种情况下),它将寻找STDIN作为输入。

作为一个简单的快速解决方案,您可以尝试拆分find和grep命令。这样的事情会起作用

file_list=$(find . -mtime 0 -type f)
! [ -z $file_list ] || grep 'EXSTAT|' $file_list |grep '|S|'|/usr/xpg4/bin/awk -F"|" '{a[$11]++;c[$11]+=$14}END{for(b in a){print b"," a[b]","c[b]/a[b]}}'


会做。

09-16 10:08