if statement with grep [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • Quoting within $(command substitution) in Bash

    3 answers



all I'm new to Linux script, would like to ask for help.



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 please anyone help me with the scrip below?



really appreciated any help.



 read -p "Enter keyword: " keyword
if search="$(cat ./records | grep '$keyword')"
then
echo "$search"
else
echo "$keyword not found!"
fi ;;









share|improve this question















marked as duplicate by jasonwryan, Community♦ Sep 27 '17 at 6:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • the error is that you are using '$keyword'. within single quotes the variable is not expanded and treated as text. use "$keyword" instead.
    – John Smith
    Sep 27 '17 at 6:40















up vote
0
down vote

favorite













This question already has an answer here:



  • Quoting within $(command substitution) in Bash

    3 answers



all I'm new to Linux script, would like to ask for help.



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 please anyone help me with the scrip below?



really appreciated any help.



 read -p "Enter keyword: " keyword
if search="$(cat ./records | grep '$keyword')"
then
echo "$search"
else
echo "$keyword not found!"
fi ;;









share|improve this question















marked as duplicate by jasonwryan, Community♦ Sep 27 '17 at 6:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • the error is that you are using '$keyword'. within single quotes the variable is not expanded and treated as text. use "$keyword" instead.
    – John Smith
    Sep 27 '17 at 6:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Quoting within $(command substitution) in Bash

    3 answers



all I'm new to Linux script, would like to ask for help.



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 please anyone help me with the scrip below?



really appreciated any help.



 read -p "Enter keyword: " keyword
if search="$(cat ./records | grep '$keyword')"
then
echo "$search"
else
echo "$keyword not found!"
fi ;;









share|improve this question
















This question already has an answer here:



  • Quoting within $(command substitution) in Bash

    3 answers



all I'm new to Linux script, would like to ask for help.



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 please anyone help me with the scrip below?



really appreciated any help.



 read -p "Enter keyword: " keyword
if search="$(cat ./records | grep '$keyword')"
then
echo "$search"
else
echo "$keyword not found!"
fi ;;




This question already has an answer here:



  • Quoting within $(command substitution) in Bash

    3 answers







linux bash shell-script scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 27 '17 at 6:20









Kusalananda

106k14209327




106k14209327










asked Sep 27 '17 at 6:17









Prin Puyakul

225




225




marked as duplicate by jasonwryan, Community♦ Sep 27 '17 at 6:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by jasonwryan, Community♦ Sep 27 '17 at 6:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • the error is that you are using '$keyword'. within single quotes the variable is not expanded and treated as text. use "$keyword" instead.
    – John Smith
    Sep 27 '17 at 6:40

















  • the error is that you are using '$keyword'. within single quotes the variable is not expanded and treated as text. use "$keyword" instead.
    – John Smith
    Sep 27 '17 at 6:40
















the error is that you are using '$keyword'. within single quotes the variable is not expanded and treated as text. use "$keyword" instead.
– John Smith
Sep 27 '17 at 6:40





the error is that you are using '$keyword'. within single quotes the variable is not expanded and treated as text. use "$keyword" instead.
– John Smith
Sep 27 '17 at 6:40











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










read -r -p 'Enter pattern: ' pattern

result=$( grep "$pattern" records )

if [ -n "$result" ]; then
printf '%sn' "$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 the if 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 as cat.*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 "$pattern" records; then
true
else
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, thus the true statement).



Regarding the use of printf in place of echo: Why is printf better than echo?




Shorter:



read -r -p 'Enter pattern: '

grep "$REPLY" records || printf 'No match found for pattern "%s"n' "$REPLY"





share|improve this answer






















  • I would replace if grep -q by if grep, and replace second grep by true.
    – Archemar
    Sep 27 '17 at 6:34










  • @Archemar Doubleplusgood
    – Kusalananda
    Sep 27 '17 at 6:36










  • Thanks so much for your help, It works ! I learn something new !!!
    – Prin Puyakul
    Sep 27 '17 at 6:38










  • on side note alternative is useful only if you don't intend to reuse grep's result.
    – Archemar
    Sep 27 '17 at 6:41










  • @Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
    – Kusalananda
    Sep 27 '17 at 6:43

















up vote
0
down vote













See below:



read -p "Enter keyword: " keyword

if search=$(grep "$keyword" ./records)
then
echo "$search"
else
echo "$keyword not found!"
fi


  1. Don't use quotting with $() as @jasonwryan was noted above

  2. Not nesessary to use cat piping with grep. Use grep <pattern> file instead

  3. Delete ;; after fi





share|improve this answer




















  • Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
    – Prin Puyakul
    Sep 27 '17 at 6:38

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










read -r -p 'Enter pattern: ' pattern

result=$( grep "$pattern" records )

if [ -n "$result" ]; then
printf '%sn' "$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 the if 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 as cat.*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 "$pattern" records; then
true
else
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, thus the true statement).



Regarding the use of printf in place of echo: Why is printf better than echo?




Shorter:



read -r -p 'Enter pattern: '

grep "$REPLY" records || printf 'No match found for pattern "%s"n' "$REPLY"





share|improve this answer






















  • I would replace if grep -q by if grep, and replace second grep by true.
    – Archemar
    Sep 27 '17 at 6:34










  • @Archemar Doubleplusgood
    – Kusalananda
    Sep 27 '17 at 6:36










  • Thanks so much for your help, It works ! I learn something new !!!
    – Prin Puyakul
    Sep 27 '17 at 6:38










  • on side note alternative is useful only if you don't intend to reuse grep's result.
    – Archemar
    Sep 27 '17 at 6:41










  • @Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
    – Kusalananda
    Sep 27 '17 at 6:43














up vote
1
down vote



accepted










read -r -p 'Enter pattern: ' pattern

result=$( grep "$pattern" records )

if [ -n "$result" ]; then
printf '%sn' "$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 the if 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 as cat.*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 "$pattern" records; then
true
else
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, thus the true statement).



Regarding the use of printf in place of echo: Why is printf better than echo?




Shorter:



read -r -p 'Enter pattern: '

grep "$REPLY" records || printf 'No match found for pattern "%s"n' "$REPLY"





share|improve this answer






















  • I would replace if grep -q by if grep, and replace second grep by true.
    – Archemar
    Sep 27 '17 at 6:34










  • @Archemar Doubleplusgood
    – Kusalananda
    Sep 27 '17 at 6:36










  • Thanks so much for your help, It works ! I learn something new !!!
    – Prin Puyakul
    Sep 27 '17 at 6:38










  • on side note alternative is useful only if you don't intend to reuse grep's result.
    – Archemar
    Sep 27 '17 at 6:41










  • @Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
    – Kusalananda
    Sep 27 '17 at 6:43












up vote
1
down vote



accepted







up vote
1
down vote



accepted






read -r -p 'Enter pattern: ' pattern

result=$( grep "$pattern" records )

if [ -n "$result" ]; then
printf '%sn' "$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 the if 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 as cat.*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 "$pattern" records; then
true
else
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, thus the true statement).



Regarding the use of printf in place of echo: Why is printf better than echo?




Shorter:



read -r -p 'Enter pattern: '

grep "$REPLY" records || printf 'No match found for pattern "%s"n' "$REPLY"





share|improve this answer














read -r -p 'Enter pattern: ' pattern

result=$( grep "$pattern" records )

if [ -n "$result" ]; then
printf '%sn' "$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 the if 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 as cat.*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 "$pattern" records; then
true
else
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, thus the true statement).



Regarding the use of printf in place of echo: Why is printf better than echo?




Shorter:



read -r -p 'Enter pattern: '

grep "$REPLY" records || printf 'No match found for pattern "%s"n' "$REPLY"






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 27 '17 at 6:41

























answered Sep 27 '17 at 6:28









Kusalananda

106k14209327




106k14209327











  • I would replace if grep -q by if grep, and replace second grep by true.
    – Archemar
    Sep 27 '17 at 6:34










  • @Archemar Doubleplusgood
    – Kusalananda
    Sep 27 '17 at 6:36










  • Thanks so much for your help, It works ! I learn something new !!!
    – Prin Puyakul
    Sep 27 '17 at 6:38










  • on side note alternative is useful only if you don't intend to reuse grep's result.
    – Archemar
    Sep 27 '17 at 6:41










  • @Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
    – Kusalananda
    Sep 27 '17 at 6:43
















  • I would replace if grep -q by if grep, and replace second grep by true.
    – Archemar
    Sep 27 '17 at 6:34










  • @Archemar Doubleplusgood
    – Kusalananda
    Sep 27 '17 at 6:36










  • Thanks so much for your help, It works ! I learn something new !!!
    – Prin Puyakul
    Sep 27 '17 at 6:38










  • on side note alternative is useful only if you don't intend to reuse grep's result.
    – Archemar
    Sep 27 '17 at 6:41










  • @Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
    – Kusalananda
    Sep 27 '17 at 6:43















I would replace if grep -q by if grep, and replace second grep by true.
– Archemar
Sep 27 '17 at 6:34




I would replace if grep -q by if grep, and replace second grep by true.
– Archemar
Sep 27 '17 at 6:34












@Archemar Doubleplusgood
– Kusalananda
Sep 27 '17 at 6:36




@Archemar Doubleplusgood
– Kusalananda
Sep 27 '17 at 6:36












Thanks so much for your help, It works ! I learn something new !!!
– Prin Puyakul
Sep 27 '17 at 6:38




Thanks so much for your help, It works ! I learn something new !!!
– Prin Puyakul
Sep 27 '17 at 6:38












on side note alternative is useful only if you don't intend to reuse grep's result.
– Archemar
Sep 27 '17 at 6:41




on side note alternative is useful only if you don't intend to reuse grep's result.
– Archemar
Sep 27 '17 at 6:41












@Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
– Kusalananda
Sep 27 '17 at 6:43




@Archemar If you want to use the result of grep it is often the case that you should write it in awk instead :-)
– Kusalananda
Sep 27 '17 at 6:43












up vote
0
down vote













See below:



read -p "Enter keyword: " keyword

if search=$(grep "$keyword" ./records)
then
echo "$search"
else
echo "$keyword not found!"
fi


  1. Don't use quotting with $() as @jasonwryan was noted above

  2. Not nesessary to use cat piping with grep. Use grep <pattern> file instead

  3. Delete ;; after fi





share|improve this answer




















  • Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
    – Prin Puyakul
    Sep 27 '17 at 6:38














up vote
0
down vote













See below:



read -p "Enter keyword: " keyword

if search=$(grep "$keyword" ./records)
then
echo "$search"
else
echo "$keyword not found!"
fi


  1. Don't use quotting with $() as @jasonwryan was noted above

  2. Not nesessary to use cat piping with grep. Use grep <pattern> file instead

  3. Delete ;; after fi





share|improve this answer




















  • Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
    – Prin Puyakul
    Sep 27 '17 at 6:38












up vote
0
down vote










up vote
0
down vote









See below:



read -p "Enter keyword: " keyword

if search=$(grep "$keyword" ./records)
then
echo "$search"
else
echo "$keyword not found!"
fi


  1. Don't use quotting with $() as @jasonwryan was noted above

  2. Not nesessary to use cat piping with grep. Use grep <pattern> file instead

  3. Delete ;; after fi





share|improve this answer












See below:



read -p "Enter keyword: " keyword

if search=$(grep "$keyword" ./records)
then
echo "$search"
else
echo "$keyword not found!"
fi


  1. Don't use quotting with $() as @jasonwryan was noted above

  2. Not nesessary to use cat piping with grep. Use grep <pattern> file instead

  3. Delete ;; after fi






share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 27 '17 at 6:29









Egor Vasilyev

1,792129




1,792129











  • Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
    – Prin Puyakul
    Sep 27 '17 at 6:38
















  • Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
    – Prin Puyakul
    Sep 27 '17 at 6:38















Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
– Prin Puyakul
Sep 27 '17 at 6:38




Thanks so much you solution is working too!! ";;" cos its in my Switch case but thanks for point it out ^^"
– Prin Puyakul
Sep 27 '17 at 6:38


Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)