9

I have wrote this script which takes a stop and start number and counts out the numbers in between - I am yting to get it to keep increasing no matter whether the "stop" number is reaced or not so it keeps counting until ctrl+z is pressed but it is not recognising the while condition for me - could anyone correct the syntax for me please ?

  #!/bin/sh

stopvalue=$1
startvalue=$2

if [ $# -ne 2  ]
then
   echo "Error - You must enter two numbers exactly - using default start value of 1"
#exit 0
fi

echo ${startvalue:=1}

while (test "$startvalue" -le "$stopvalue" ||  "$startvalue" -ge "$stopvalue")
do
       startvalue=$((startvalue+1))
        echo $startvalue
done

5 Answers 5

14

Now that you have two answers about the while loop, I'll suggest using for loop instead:

for((i=$startvalue;i<=$stopvalue;++i)) do
   echo $i
done
Sign up to request clarification or add additional context in comments.

Comments

5

I use the simple while loop program. Which works for me. as per my understanding before passing any variable in while loop we have to declare it:

sum=0            
temp=1          
while [ $temp -ne 0 ]           
do              
echo "Enter number: OR 0 to Quit :"             
read temp          
sum=`expr $sum + $temp`          
done          
echo $sum        

Comments

5

Your main problem is in your condition:

while (test "$startvalue" -le "$stopvalue" ||  "$startvalue" -ge "$stopvalue")

You are telling it to continue if $startvalue is (less than or equal to) or (greater than or equal to) $stopvalue. For ANY combination of $startvalue and $stopvalue, the one will be less than, equal to, or greater than the other. Take out the second condition so that it only continues if $startvalue is less than or equal to $stopvalue.

And you should write it:

while [ "$startvalue" -le "$stopvalue" ]

Comments

0
while [ "$startvalue" -le "$stopvalue" -o "$startvalue" -ge "$stopvalue" ]
do
       startvalue=$((startvalue+1))
       echo $startvalue
done

4 Comments

That's still going to loop forever because $startvalue is always less than (first condition), equal to (both), or greater than (second) $stopvalue.
@Kevin - Good point, I didn't spend much time looking at whether or not the loop made sense, I was just fixing the syntax.
This is correct syntax. In test you have to use -o and not ||. || is totally different thing and after that you need another shell command (like test).
you can use || with [[ ]], like [[ condition || condition ]]
-2
startvalue=50
stopvalue=0
while [ $startvalue -lt  $stopvalue -o $startvalue -ne $stopvalue ]; do
echo "Enter number equal to the start value: :"             
read stopvalue
done

1 Comment

A code-only answer is not high quality. While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.