loop in DOS and Linux shell scripts

A couple of friends was asking how to do loop in shell scripts. Here is some simple code.
To loop in DOS:
@set /p start="From:"
@set /p end="To:"

:while

@REM test condition
@IF %start% GTR %end% (GOTO wend)

@REM procedure where condition is "true"
@REM replace with real operations
@echo %start%

@REM set new test value
@SET /a start=start+1

@REM loop
@GOTO while

:wend

This will print the first number to the last number you input on the screen. Replace @echo %start% with your real operation.

However, DOS script does not handle real numbers. So if I want to loop from 1 to 10, and print the number + 0.5, this will not work. One solution is using Perl. Second, using Linux. Here is the code for Linux.

echo -n "Enter starting code> "
read start
echo -n "Enter ending code> "
read end

#start loop
for ((i=start; i<=end;i++))
do
echo "$i+0.1" | bc
done

If you input 1 and 5, this is print 1.5, 2.5, 3.5, 4.5, 5.5.
Enjoy.

Note: I did not create these codes. They are from Google search. However, they are from different sources (especially the use of bc) so I lost the source link.

Advertisement

1 thought on “loop in DOS and Linux shell scripts

  1. Pingback: A simple low level test automation | Fang Mobile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s