Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep statement is not working inside case statement

Tags:

linux

bash

shell

sh

I wanted to make a shell script that accepts a vowel and prints the number of occurrences of that vowel inside the text file "abc.txt".

The following script works perfectly (a script to print the number of occurrences of the vowel "a" inside the text file "abc.txt"):

#!/bin/bash
grep -o [aA] abc.txt|wc -l

But I want to implement this for all the vowels so I did this:

#!/bin/bash
echo -n "Enter the desired vowel: "
read ch
case ch in
a) grep -o [aA] abc.txt|wc -l;;
A) grep -o [aA] abc.txt|wc -l;;
e) grep -o [eE] abc.txt|wc -l;;
.
.
.
U) grep -o [uU] abc.txt|wc -l;;
esac

The code executes but after I enter the desired vowel nothing is displayed. I've also tried this (but the results are the same as those of the code above):

#!/bin/bash
x=0
echo -n "Enter the desired vowel: "
read ch
case ch in
a) x=grep -o [aA] abc.txt|wc -l;echo $x;;
A) x=grep -o [aA] abc.txt|wc -l;echo $x;;
e) x=grep -o [eE] abc.txt|wc -l;echo $x;;
.
.
.
U) x=grep -o [uU] abc.txt|wc -l;echo $x;;
esac

I'm lost as to why the grep statements aren't displaying anything when I put them inside a case statement.

like image 388
Zantorym Avatar asked Sep 28 '26 02:09

Zantorym


2 Answers

Multiple issues, but the one mostly causing your problem is not using a variable in the case construct. Use ch is just a constant and does not match any of the expressions below.

case "$ch" in
#    ^^^^^ This needs to be a variable used in read command

Also, to store the output of a command, you need to use command-substitution syntax of type $(cmd). Also instead of grep .. | wc -l you can just use the -c flag to return the total count of matched strings.

x=$(grep -oc '[aA]' abc.txt); echo "$x"

(or) an even improved grep command would be to enable case-insensitive match with the -i flag

x=$(grep -oci 'a' abc.txt); echo "$x"
like image 55
Inian Avatar answered Sep 29 '26 17:09

Inian


Your problem is not the case statement, but that you're using the variable assignment in a wrong way

x=grep -o [aA] abc.txt|wc -l;echo $x

you're assigning grep to the variable x for running -o [aA] abc.txt, as var=something command assigns the variable just for running command.

This of course doesn't make sense, but you can be glad you didn't try something like x=something rm * which would have deleted your files.

The correct syntax is

x="`grep -o '[aA]' abc.txt|wc -l`"

which means execute grep|wc in a subshell and assign the result to the variable x. I added quotes, as you get a problem without quotes when your command does not return anything, as x= is a syntax error while x="" is perfectly fine.

In bash you have the nice syntax (which can be nested)

x="$(grep -o '[aA]' abc.txt|wc -l)"

which does the same. But be sure to begin your script with #!/bin/bash, as /bin/sh often is another shell than bash where the syntax may not work.

The $() should work on every fully POSIX compatible shell, but /bin/sh may not be fully compatible, so using a specific shell is a good idea anyway.

like image 24
allo Avatar answered Sep 29 '26 15:09

allo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!