Computing.Net > Forums > Unix > Different Script Problem

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Different Script Problem

Reply to Message Icon

Original Message
Name: HelpME
Date: June 25, 2005 at 07:50:59 Pacific
Subject: Different Script Problem
OS: XP
CPU/Ram: 2.8/512
Comment:

Hey i am having a little trouble with this problem as well.

x=0
while true
do
echo "input a number (x to quit):"
read num1

[ $num1 -eq x] && break

if [ $num1 -eq x ]
then
echo "Bye Bye"
elif [ $num1 !=x ]
echo "Please choose an option"
echo "1 count from 0 or"
echo "2 count down to 0"
echo "Please select an option"
read num2
if [ $num2 = "1" ]
then
while [ $counter = $num1 ]
do
counter=`expr $counter + 1`
echo $counter
elif
while [ $counter = $num1 ]
do
counter=`expr $counter - 1`
echo $counter
fi
done



Report Offensive Message For Removal


Response Number 1
Name: Dlonra
Date: June 25, 2005 at 08:59:28 Pacific
Reply: (edit)

what os/*nix/shell are you using?
does the shell generate diagnostics?


Report Offensive Follow Up For Removal

Response Number 2
Name: HelpME
Date: June 25, 2005 at 09:05:17 Pacific
Reply: (edit)

UNIX Bourne Shell


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: June 25, 2005 at 09:13:23 Pacific
Reply: (edit)

The script has several structural problems.  Make sure that each while loop is delimited properly with do-done, and make sure all the if-statements are properly balanced.  Indented spacing helps readability, and of course this web site strips indentation unless you force it not to.

Some notes on my version below ...

Use = for string compares and -eq for numeric compares.  And the code would never get to your Bye Bye because the command above it would break out of the loop before it executes.

Your code has too much redundancy.  In the following pseudo code, the else branch is taken when the condition is false, meaning that num is not 5, so it is redundant to check for non-5:

if num = 5
  then do this
  else if num != 5
          do that

Instead, simplify to this:

if num = 5
  then do this
  else do that

And following the check for x, don't code an else or elif at that point.  If an x is entered, the code will break out of the loop at that point.  If not, your remaining code will execute, so it does not need to be under an else.

I replaced a series of echos with a single cat command.  When you have a lengthy menu or something to display, the single cat command is better than a bunch of echos, and easier to format also.

For your counting loops, you were not initializing the counter variable, and you don't want = for the while-loop condition.  You want -lt and -gt so that the condition will be true from the get go and will remain true until your destination is reached.


x=0
while true
do

echo "input a number (x to quit):"
read num1

# [ $num1 = x ] && break

if [ $num1 = x ] ; then
  echo "Bye Bye"
  break
fi

cat << !

----------------------
1) count upward from 0
2) count down to 0
----------------------

!
echo "Please select an option: \c"
read num2

if [ $num2 = 1 ] ; then
   counter=0
   while [ $counter -lt $num1 ]
   do
   counter=`expr $counter + 1`
   echo $counter
   done
else
   counter=$num1
   while [ $counter -gt 0 ]
   do
   echo $counter
   counter=`expr $counter - 1`
   done
fi

done


Report Offensive Follow Up For Removal

Response Number 4
Name: Jim Boothe
Date: June 25, 2005 at 09:21:54 Pacific
Reply: (edit)

And one more note ...

For the countdown loop, notice that echo $counter is before the decrement.

Place it below to see the results of that.


Report Offensive Follow Up For Removal

Response Number 5
Name: HelpME
Date: June 25, 2005 at 09:29:07 Pacific
Reply: (edit)

Hey Jim, is there any other way to finish this problem without using that "simple cat command" I never learned how to put cat commands inside a script before... also any way i can remove the # symbol?


Report Offensive Follow Up For Removal


Response Number 6
Name: Jim Boothe
Date: June 25, 2005 at 09:32:29 Pacific
Reply: (edit)

Sure, just replace the cat command and put your echo's back in place.

A shell script line that starts with a # is just a comment line, so you can remove the entire line.


Report Offensive Follow Up For Removal

Response Number 7
Name: HelpME
Date: June 25, 2005 at 09:43:57 Pacific
Reply: (edit)

Hey Jim, thats you for your support. I have 1 more question. I am trying to get the program to quit when a user enter a number other that 1 or 2 for there choice to choose to count up or down. Here is what i have. I put an echo and it work but it still does the counting.

x=0
while true
do

echo "input a number (x to quit):"
read num1


if [ $num1 = x ] ; then
echo "Have a Great Day!"
break
fi
e
echo "Please choose an option:"
echo "1 count from 0"
echo "2 count down to 0"
e

echo "Please select an option: \c"
read num2

if [ $num2 -ge 2 ] ; then
echo "Sorry"
fi

if [ $num2 = 1 ] ; then
counter=0
while [ $counter -lt $num1 ]
do
counter=`expr $counter + 1`
echo $counter
done
else
counter=$num1
while [ $counter -gt 0 ]
do
echo $counter
counter=`expr $counter - 1`
done
fi

done


Report Offensive Follow Up For Removal

Response Number 8
Name: HelpME
Date: June 25, 2005 at 09:48:24 Pacific
Reply: (edit)

Hey Jim never mind i got it... Thanks for all of you help!


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software