wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes error correction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "error correction" Watch "error correction" New topic
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: 1502
    
    5

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, OCEEJBD)
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..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: error correction
 
Similar Threads
Getting error - Chapter 4 of Ejb3 in action using glassfish server 2.1
Unable to compile java file through ant
Tomcat fails to restart with "Address already in use" exception
shell script in tcsh
Expert guidance needed in understanding the part of a shell script