Bash: 如何以grep的结果做为if condition
#!/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