#!/bin/bash
echo 'Please enter the name of the species you are looking for: '
read speciesName
grep "$speciesName" speciesDetails.txt | awk '{print $0}'
echo
echo 'Would you like to search for another species? Press y to search or n to go back
to the main menu: '
read answer
case $answer in
[yY] | [yY][eE][sS] )
./searchSpecies.sh;;
[nN] | [nN][oO] )
./speciesMenu.sh;;
*) echo exit;;
esac
If there is no entry of that species name in the file how do I give the user an error to say not found?
The answer to your immediate question is to examine the exit code from grep. But probably also refactor the loop:
#!/bin/bash
while true; do
read -p 'Please enter the name of the species you are looking for: ' -r speciesName
grep -e "$speciesName" speciesDetails.txt || echo "$speciesName: not found" >&2
read -p 'Would you like to search for another species? Press n to quit: ' -r answer
case $answer in
[nN] | [nN][oO] )
break;;
esac
done
A better design altogether is probably to make the search term a command-line argument. This makes the script easier to use from other scripts, and the user can use the shell's facilities for history, completion, etc to run it as many times as they like, and easily fix e.g. typos by recalling the previous invocation and editing it.
#!/bin/bash
grep -e "$1" speciesDetails.txt || echo "$1: not found" >&2
The short-circuit one || two corresponds to the longhand
if one; then
: nothing
else
two
fi
If you want to search for static strings, not regular expressions, maybe add -F to the grep options.
If you need just check existance then execute it using next way :
if grep speciesName4 speciesDetails.txt; then
echo "exist";
else
echo "Not exist";
fi
Use $? to check exit code of the command if you need return value as well
set -o pipefail
$ echo "speciesName1" > speciesDetails.txt
$ echo "speciesName2" >> speciesDetails.txt
$ echo "speciesName3" >> speciesDetails.txt
$ l_result=$(grep speciesName3 speciesDetails.txt); l_exit_code=$?
$ echo $l_exit_code
0
$ l_result=$(grep speciesName4 speciesDetails.txt); l_exit_code=$?
$ echo $l_exit_code
1
Updated:
It is not antipattern if you need to use later output of your command
I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
What's wrong in my code?
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
You can specify commands as an condition of if. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.
$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
As you can see you run here the programs directly. No additional [] or [[]].
In case if you want to check whether file does not contain a specific string, you can do it as follows.
if ! grep -q SomeString "$File"; then
Some Actions # SomeString was not found
fi
In addition to other answers, which told you how to do what you wanted, I try to explain what was wrong (which is what you wanted.
In Bash, if is to be followed with a command. If the exit code of this command is equal to 0, then the then part is executed, else the else part if any is executed.
You can do that with any command as explained in other answers: if /bin/true; then ...; fi
[[ is an internal bash command dedicated to some tests, like file existence, variable comparisons. Similarly [ is an external command (it is located typically in /usr/bin/[) that performs roughly the same tests but needs ] as a final argument, which is why ] must be padded with a space on the left, which is not the case with ]].
Here you needn't [[ nor [.
Another thing is the way you quote things. In bash, there is only one case where pairs of quotes do nest, it is "$(command "argument")". But in 'grep 'SomeString' $File' you have only one word, because 'grep ' is a quoted unit, which is concatenated with SomeString and then again concatenated with ' $File'. The variable $File is not even replaced with its value because of the use of single quotes. The proper way to do that is grep 'SomeString' "$File".
Shortest (correct) version:
grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no"
can be also written as
grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no"
but you dont need to explicitly test it in this case, so the same with:
grep -q "something" file && echo "yes" || echo "no"
##To check for a particular string in a file
cd PATH_TO_YOUR_DIRECTORY #Changing directory to your working directory
File=YOUR_FILENAME
if grep -q STRING_YOU_ARE_CHECKING_FOR "$File"; ##note the space after the string you are searching for
then
echo "Hooray!!It's available"
else
echo "Oops!!Not available"
fi
grep -q [PATTERN] [FILE] && echo $?
The exit status is 0 (true) if the pattern was found; otherwise blankstring.
if grep -q [string] [filename]
then
[whatever action]
fi
Example
if grep -q 'my cat is in a tree' /tmp/cat.txt
then
mkdir cat
fi
In case you want to checkif the string matches the whole line and if it is a fixed string, You can do it this way
grep -Fxq [String] [filePath]
example
searchString="Hello World"
file="./test.log"
if grep -Fxq "$searchString" $file
then
echo "String found in $file"
else
echo "String not found in $file"
fi
From the man file:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of
which is to be matched.
(-F is specified by POSIX.)
-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by
POSIX.)
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero
status if any match is
found, even if an error was detected. Also see the -s or --no-messages
option. (-q is specified by
POSIX.)
Try this:
if [[ $(grep "SomeString" $File) ]] ; then
echo "Found"
else
echo "Not Found"
fi
I done this, seems to work fine
if grep $SearchTerm $FileToSearch; then
echo "$SearchTerm found OK"
else
echo "$SearchTerm not found"
fi
grep -q "something" file
[[ !? -eq 0 ]] && echo "yes" || echo "no"
How do I check if a string is contained in the text produced by a cat command in bash? I want to execute a certain action if the string is found in the text.
Like this, using the pipe :
cat my_file | grep my_string
if [ ! $(cat my_file.txt | grep my_text) == "" ]
then echo "exists"
else
echo "not exists"
fi;
Piping to grep is fine if it's a one-time match. Otherwise in bash you can match strings using patterns (==) or regexes (=~) like this:
# date looks like "Mon, Jun 22, 2020 11:04:54 AM"
today=$(date)
[[ $today == Mon* ]] && echo "It's the start of the week"
[[ $today =~ ^(Tue|Wed|Thu) ]] && echo "It's the middle of the week"
[[ $today == Fri* ]] && echo "Thank God It's Friday!"
This is really a comment on the other answer - you can just directly test the exit status of a command
# grep succeeds (exit status 0) if there's a match
# grep -q is to suppress the output of the matched line
# we just care whether there is a match or not
if grep -q my_text my_file.txt; then
echo "exists"
else
echo "not exists"
fi
Simply:
if grep -q my_text file; then
echo "exists"
else
echo "not exists"
fi
I wrote this shell code, but it doesn't get the good output.
Even though the $csoport gets the "...: No such user" output, id doesn't echoes the following line I wrote there.
read felhasznalo
while [ "$felhasznalo" != "exit" ]
do
csoport=`groups $felhasznalo`
echo "$csoport"
if [[ "$csoport" == *": No such user"* ]] ; then
echo -n "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi
echo -n "Felhasznalo: "
read felhasznalo
done
You shouldn't try to match the error messsage since you only care if groups fails. You ought to do:
if ! csoport=$(groups "$felhasznalo"); then
printf "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi
I am trying to match two strings (IpAddress) as below. But it's not matching.
i=192.168.2.29
ipCheckInConfig="SG_1=192.168.2.24,192.168.2.29
> SG_2=192.168.2.20,192.168.2.23,192.168.2.31"
if echo "$i" | egrep -q "$ipCheckInConfig" ; then
echo "Matched"
else
echo "Not Matched"
fi
Could someone please help?
You don't need to call egrep for that. Use bash's internal regex capabilities:
if [[ "$ipCheckInConfig" =~ $i ]]; then
echo "Matched"
else
echo "Not Matched"
fi