| Author |
error correction
|
kalyani kal
Greenhorn
Joined: Jan 28, 2012
Posts: 3
|
|
Hello all..
I wrote the following code as part of my project work. I need to print 'i' value based on the number of ';'(semi colons) in the input string. but thw while loop is not getting executed. It is returning errors. I tried lot of alternatives bt could not figure it out.. Badly need in help.. please replty me
IN="aa;bb;cc;"
c= echo $IN | tr -dc ';' | wc -c
echo $c
i=1
while [ $i -le $c ];
do
echo $i
i=`expr $i + 1`
done
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
Hi,
I'm pretty rookie myself in shell script, but what I can see from below script is : output of the piped command is not getting assigned to 'c', i.e. if you try to print value of c, it prints nothing.
Secondly, during number comparison, one should wrap variables with double quotes like : while [ "$i" -le "$c" ];
Hope this helps. Also, reply to this if you find a way to assign output of piped commands to a variable.
Thanks.
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
kalyani kal
Greenhorn
Joined: Jan 28, 2012
Posts: 3
|
|
Thank you for your reply dear Anayonkar Shivalkar. Yes what you said was correct. ther is a problem with value assignment to c. So i modified it as follows
IN="aa;bb;cc;"
c=`echo $IN | tr -dc ';' | wc -c`
echo $c
i=1
while [ $i -le $c ];
do
echo $i
i=`expr $i + 1`
done
I kept the second statement in back tick (`). It's working fine now..
|
 |
 |
|
|
subject: error correction
|
|
|