Sunday 17 January 2016

Bash shell script looping statements

#!/bin/bash

#for loop , while loop

declare -a arr=( 1 2 3 4 5 )

echo ${arr[@]}

#< len of array

for (( i=0; i < ${#arr[@]} ; i++ ))
do
        echo "$i -> ${arr[$i]}"
done

#while loop

n=${#arr[@]}
i=0
while [ $i -lt $n ]
do
        echo "$i -- > ${arr[$i]}"
        let i++
done

#until
i=0
until [ ! $i -lt $n ]
do
        echo "$i -- > ${arr[$i]}"
        i=`expr $i + 1`
done

No comments:

Post a Comment