Answer by unifex for Can grep return true/false or are there alternative methods
Old post but really the answer was never given.Yes Grep returns 0 if pattern is found (true) and 1 if the pattern is not found (false).Keep in mind grep is line based so the search term you thought...
View ArticleAnswer by Denis Pitzalis for Can grep return true/false or are there...
I know I am late for this, but I love this short version:grep -q ^$1 schemas.txt && echo "Schema already exists. Please try again" || echo "$@">> schemas.txt
View ArticleAnswer by JJoao for Can grep return true/false or are there alternative methods
If we want to catch the first word of a file we need do add -zw to grepif grep -qzw "^$1" filethen ... else ... fiWithout -z we get the first word of a line. Without -w we get partial words.
View ArticleAnswer by k_vishwanath for Can grep return true/false or are there...
If you want to use it with square brackets, you can execute the belowif [ `grep -q PATTERN file.txt` ]; then echo foundelse echo not foundThis Logic works for all commands, Just place your commands...
View ArticleAnswer by amigal for Can grep return true/false or are there alternative methods
Another simple way is to use grep -c. That outputs (not return as exit code), the number of lines that match the pattern, so 0 if there's no match or 1 or more if there's a match.So, if you wanted to...
View ArticleAnswer by derobert for Can grep return true/false or are there alternative...
grep returns a different exit code if it found something (zero) vs. if it hasn't found anything (non-zero). In an if statement, a zero exit code is mapped to "true" and a non-zero exit code is mapped...
View ArticleCan grep return true/false or are there alternative methods
As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message; if it doesn't, append the arguments to the file....
View Article