Bash loop examples

For Loop Example 1

#!/bin/bash
# Date: 15.04.08
# Author: bash@roshankarki.com.np
# Purpose: Demonstrate for loop
clear
for item in usa india china japan nepal
do
echo $item
done
#END


For Loop Example 2

#!/bin/bash
# Date: 15.04.08
# Author: bash@roshankarki.com.np
# Purpose: Demonstrate for loop
clear
for i in 1 2 3 4 5
do
echo $i
done
#END


For Loop Example 3

#!/bin/bash
# Date: 15.04.08
# Author: bash@roshankarki.com.np
# Purpose: Demonstrate for loop
clear
for i in `seq 100`
do
echo $i
done
#END


For Loop Example 4

#!/bin/bash
# Date: 15.04.08
# Author: bash@roshankarki.com.np
# Purpose: Demonstrate for loop
clear
for file in `ls *`
do
echo $file
done
#END


While Loop Example

#!/bin/bash
# Date: 15.04.08
# Author: bash@roshankarki.com.np
# Purpose: Demonstrate until loop
clear
c=1
while [ $c -gt 5 ]
do
echo Welocme
let "c += 1"
done
#END


gt >
lt <
eq =
le <=
ge >=
nq !=


Until Loop Example

#!/bin/bash
# Date: 15.04.08
# Author: bash@roshankarki.com.np
# Purpose: Demonstrate while loop
clear
c=1
while [ $c -lt 5 ]
do
echo Welocme
let "c += 1"
done
#END

Comments

  1. "for i in `seq 100`"

    No need to start an entire new process ( seq ), bash 3.x has this feature built in:

    for i in {1..100}
    do echo $i
    done

    ReplyDelete

Post a Comment

Comments are moderated. No spam please.

Popular posts from this blog

Perm Root HTC Desire Gingerbread 2.3.3

Increase Free Space On HTC Desire Android Phones

Lenovo Y510 surround sound in Ubuntu