• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error correction

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic