loop

loop in bash

for i in *.csv ; do

cat $i| awk '{ print $16" "$2" "$12" "$13" "$15}' >> `echo $i| sed "s/csv$//"`txt

done

for i in calc2 calc3 calc4 cal5 ; do

...

done

for i in {1..100};

do

.....

done

for i in `seq 1 10`;do

...

done

CEILING=10000

for n in $(seq $CEILING)

do

...

done

COUNTER=0

while [ $COUNTER -lt 10 ]; do

echo The counter is $COUNTER

let COUNTER=COUNTER+1

done

COUNTER=0

while true; do

echo The counter is $COUNTER

let COUNTER=COUNTER+1

done

while read line

do

echo $line

done

#!/bin/bash

#Declare array with 4 elements

ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )

# get number of elements in the array

ELEMENTS=${#ARRAY[@]}

# echo each element in array

# for loop

for (( i=0;i<$ELEMENTS;i++)); do

echo ${ARRAY[${i}]}

done

>`echo $i| sed "s/.mpeg.txt$//"`.txt

directory="./BashScripting"

# bash check if directory exists

if [ -d $directory ]; then

echo "Directory exists"

else

echo "Directory does not exists"

fi

NUM1=2

NUM2=2

if [ $NUM1 -eq $NUM2 ]; then

echo "Both Values are equal"

else

echo "Values are NOT equal"

fi

-lt -gt -le -ge -eq -ne

NUM1=2

NUM2=1

if [ $NUM1 -eq $NUM2 ]; then

echo "Both Values are equal"

elif [ $NUM1 -gt $NUM2 ]; then

echo "NUM1 is greater then NUM2"

else

echo "NUM2 is greater then NUM1"

fi

let RESULT1=$1+$2

echo $1+$2=$RESULT1 ' -> # let RESULT1=$1+$2'

declare -i RESULT2

RESULT2=$1+$2

echo $1+$2=$RESULT2 ' -> # declare -i RESULT2; RESULT2=$1+$2'

echo $1+$2=$(($1 + $2)) ' -> # $(($1 + $2))'

echo 4 + 5 = $((4 + 5))

echo 7 - 7 = $[ 7 - 7 ]

echo 4 x 6 = $((3 * 2))

echo 6 / 3 = $((6 / 3))

echo 8 % 7 = $((8 % 7))

echo 2 ^ 8 = $[ 2 ** 8 ]

echo "Enter input:"

read userinput

echo "Result with 2 digits after decimal point:"

echo "scale=2; ${userinput}" | bc

echo "Result with 10 digits after decimal point:"

echo "scale=10; ${userinput}" | bc

echo "Result as rounded integer:"

echo $userinput | bc