https://stackoverflow.com/questions/7522712/how-can-i-check-if-a-command-exists-in-a-shell-script
https://www.shellhacks.com/check-if-string-exists/
https://www.unix.com/shell-programming-and-scripting/259572-check-if-string-exists-file.html
a="hehe"
if ! grep -q $a file; then
echo "hehe" >> file
fi
$?为1:not found
$?为0:found
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
You don't need [[ ]] here. Just run the command directly. Add -q option when you don't need the string displayed when it was found.
The grep command returns 0 or 1 in the exit code depending on the result of search. 0 if something was found; 1 otherwise.
$ echo hello | grep hi ; echo $?
1
$ echo hello | grep he ; echo $?
hello
0
$ echo hello | grep -q he ; echo $?
0