Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error correction

 
kalyani kal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kalyani kal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic