Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ! operator on a shell command

I'm trying to write a script which does something if a particular string is not present in a file.

I know that in order to check if string is available, we can do something like:

if grep -qi "sms" $FILE; then

But how do I combine a ! operator with a shell command?

like image 518
drunkenfist Avatar asked Aug 25 '26 11:08

drunkenfist


1 Answers

Just add it in front of your condition to make it evaluate on the contrary:

if ! grep -qi "sms" $FILE; then echo "yes"; fi
   ^

Test

$ cat a
hello
bye
$ if ! grep -qi "sms" a; then echo "sms not found"; fi
sms not found
like image 50
fedorqui 'SO stop harming' Avatar answered Aug 27 '26 04:08

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!