首页 > 其他 > 详细

Bash: 如何以grep的结果做为if condition

时间:2021-09-05 21:32:55      阅读:24      评论:0      收藏:0      [点我收藏+]

 

 

#!/bin/ksh
grep $NAME filename
if [ $? -eq 0 ]
   echo "Name Found"
else
   echo " Name not Found"
fi

The $? holds the exit status of the previously executed command. Check the man page please.

来源:https://www.unix.com/shell-programming-and-scripting/30996-using-grep-if-statement.html

 

虽然上面是针对是ksh,但是bash同样适合:

for item in *.json; do
    # grep "perl" "$item" > /dev/null #为了不在屏幕上显示grep结果
    # 或者:
    grep -q "perl" "$item"
    if [ $? -eq 0 ]; then #如果搜索到perl,则$?会是0,否则为1
        echo $item #输出含有perl的文件,对应的文件名
    fi
done

 

Bash: 如何以grep的结果做为if condition

原文:https://www.cnblogs.com/profesor/p/15227375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!