Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to while loop correctly in bash script?

Tags:

bash

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
like image 646
frodo Avatar asked Dec 07 '22 17:12

frodo


1 Answers

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
like image 65
Michael Krelin - hacker Avatar answered Jan 04 '23 04:01

Michael Krelin - hacker