Linux Bash/Shell simple, basic flow control

Last Updated on

“if else” and pass variable to the script

if [condition]
 then
     //if block code
 else
     // else block code
 fi
#!/bin/bash
# https://dannyda.com
if [ $1 == $2 ];then
	echo "Numbers are equal ("$1" = "$2")"
elif [ $1 -gt $2 ];then
	echo "1st Number is greater than 2nd Number ("$1" > "$2")"
elif [ $1 -lt $2 ];then
	echo "1st Number is smaller than 2nd Number ("$1" < "$2")"
else
	echo "error"
fi
if else, pass variables to script
if else, pass variables to script

for Loop

for [var] in [list]
do
    //command1
    //command2
done
#!/bin/bash
# https://dannyda.com
for variable in 1 2 3 4 $(echo 7)
do
	echo "The value is: $variable"
done
for i in {1..3}
do
	echo "Loop $i times"
done
echo "2 Done"
echo
for i in {0..10..2}
do 
	echo "Step of 2, $i times"
done
echo "3 Done"
echo
for loop
for loop

while Loop

while [condition]
do
    //command1
    //command2
done
while [condition]; do commands; done
#!/bin/bash
# https://dannyda.com
i=0
while [[ $i<5 ]]
do
    echo $i
    let "i++"
done
echo "Job Done"
echo
i=0
while [[ $i<5 ]]; do echo $1 $((i++)); done
while loop
while loop

while Loop with input (Interactive script)

while read [condition]
do
    //command1
    //command2
done
#!/bin/bash
# https://dannyda.com
echo '<CTRL-D> to exit'
while read input
do
	echo "The number is $input"
done
while with input (interactive script)
while with input (interactive script)

until Loop

Until loop is opposite to while loop, it execute the given commands as long as the given condition evaluates is false

until [condition]
do
    //command1
    //command2
done
#!/bin/bash
# https://dannyda.com
i=0
until [ $i -gt 5 ]
do
  echo Counter: $i
  ((i++))
done
echo "-----------"
i=0
while [ $i -lt 6 ]
do
  echo Counter: $i
  ((i++))
done
until loop
until loop

Infinite while Loop

Use Ctrl + C to stop/exit the loop

while true
do
    //command1
    //command2
done
#!/bin/bash
# https://dannyda.com
while true
do
	echo a
done
infinite while loop
infinite while loop

Infinite for Loop

Use Ctrl + C to stop/exit the loop

for (( ; ; ))
do
    //command1
    //command2
done

infinite for loop
infinite for loop

Options/Case/Switch/Select Case

case  $variable  in
     pattern1)
         command1
         command2
         …
         ….
     ;;
     pattern2)
         command1
         command2
         …
         ….
     ;;
     patternN)
         command1
         command2
         …
         ….
     ;;
     *)
         command1
         command2
         …
         ….
 esac
case  $variable  in
     pattern1|pattern2|pattern3)
         command1
         command2
         …
         ….
     ;;
     pattern4|pattern5|pattern6)
         command1
         command2
         …
         ….
     ;;
     pattern7|pattern8|patternN)
         command1
         command2
         …
         ….
     ;;
     *)
         command1
         command2
         …
         ….
 esac
esacThe EOF for case
)the end of the pattern
*)Else, if doesn’t match anything
;;The end of current case clause
#!/bin/bash
# https://dannyda.com
case $1 in
    a) echo 'Case a'
    ;;
    b) echo 'Case b'
    ;;
    3) echo 'Case 3'
    ;;
    *) echo 'Not Case a or Case b or Case 3'
    ;;
esac
Case
Case

Leave a Reply

Your email address will not be published. Required fields are marked *