Sunday, April 16, 2006

Bash : sample loops (shell scripting)

Here are some sample loops in bash scripting. Feel free to leave a comment if something is not clear to you. I recommend using bash for learing shell scripting.
--------------------
#!/bin/bash
for i in `seq 1 10`
do
let i=i*5

echo $i
done

--------------------
# seq outputs a sequence of numbers (1 to 10)
# let is a bash built-in command that allows arithmetic operations to be performed and assigned to a variable as done above
---------------
for i in $(ls)
do
echo $i
done

---------------
j=0
while [ $j -lt 10 ]
do
echo $j
let j=j+1
done

----------------
# same example with until
# note the -eq switch in until loop
--------------------
j=0
until [ $j -eq 10 ]
do
echo $j
let j=j+1
done

---------------------

No comments: