if statement with grep
Solution 1
read -r -p 'Enter pattern: ' pattern
result=$( grep -e "$pattern" records )
if [ -n "$result" ]; then
printf '%s\n' "$result"
else
printf 'No match found for pattern "%s"\n' "$pattern"
fi
-
The assignment to
search
(result
in my code) is best done outside of theif
statement. -
Test with
-n
("is this string non-empty?") on the result. -
Don't single-quote the variable (it will prevent the shell from expanding its value). Double quote it instead.
-
Notice "pattern" rather than "keyword". The way you use
grep
here will use the user-supplied string as a regular expression (a pattern, such ascat.*dog
), not necessarily as a plain fixed string. -
cat
should be used to concatenate files, in most other cases it's more or less useless. -
Use
read -r
to allow the user to enter backslashes.
Alternatively:
read -r -p 'Enter pattern: ' pattern
if ! grep -e "$pattern" records; then
printf 'No match found for pattern "%s"\n' "$pattern"
fi
This avoids storing a potentially huge amount of data in a shell variable and instead relies on the exit status of grep
to tell whether the pattern may be found in the file or not. If the pattern is found, the matching lines will be printed (and nothing more has to be done).
Regarding the use of printf
in place of echo
: Why is printf better than echo?
Shorter, using a short-circuit OR.
read -r -p 'Enter pattern: '
grep -e "$REPLY" records ||
printf 'No match found for pattern "%s"\n' "$REPLY"
Solution 2
See below:
read -p "Enter keyword: " keyword
if search=$(grep "$keyword" ./records)
then
echo "$search"
else
echo "$keyword not found!"
fi
- Don't use quotting with
$()
as @jasonwryan was noted above - Not nesessary to use
cat
piping withgrep
. Usegrep <pattern> file
instead - Delete
;;
afterfi
Related videos on Youtube
![Prin Puyakul](https://lh3.googleusercontent.com/-dkGdoVO6uRs/AAAAAAAAAAI/AAAAAAAAVmA/kQpVOYzijac/photo.jpg?sz=256)
Prin Puyakul
Updated on September 18, 2022Comments
-
Prin Puyakul almost 2 years
What I'm trying to do is the get the user input and search through the file, If it match it will show the result, else it will echo $keyword not found.
But my scrip is always return fault.
read -p "Enter keyword: " keyword if search="$(cat ./records | grep '$keyword')" then echo "$search" else echo "$keyword not found!" fi ;;
-
Prin Puyakul over 6 yearsThanks so much for your help, It works ! I learn something new !!!
-
Prin Puyakul over 6 yearsThanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
-
Арсений Черенков over 6 yearson side note alternative is useful only if you don't intend to reuse grep's result.
-
Kusalananda over 6 years@Archemar If you want to use the result of
grep
it is often the case that you should write it inawk
instead :-) -
Prin Puyakul over 6 yearsHi again, I just wonder how do i modify the script if the user dont provide input (eg. just press enter or return key). do i just elseif?
-
Kusalananda over 6 years@PrinPuyakul You may use
if [ -z "$pattern" ]; then ... fi
to do something if the pattern in$pattern
is empty. -
Prin Puyakul over 6 years@Kusalananda ok, but I thought -n is to check the empty string? -z is share similarity ?
-
Kusalananda over 6 years@PrinPuyakul
-n
checks for a non-empty string,-z
checks for an empty string.