[grep] how to use grep return value as if condition

 

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

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

 

posted @ 2024-12-06 19:27  profesor  阅读(2)  评论(0编辑  收藏  举报