0% found this document useful (0 votes)
3 views36 pages

2 Conditionals Loops

The document provides an overview of conditional statements and loops in Bash scripting, including syntax and examples for 'if-then', 'case', 'for', 'while', and 'until' loops. It also covers the use of mathematical expressions, command-line argument handling, and the 'break' and 'continue' commands. Additionally, it includes practice questions to reinforce the concepts discussed.

Uploaded by

menoftech9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

2 Conditionals Loops

The document provides an overview of conditional statements and loops in Bash scripting, including syntax and examples for 'if-then', 'case', 'for', 'while', and 'until' loops. It also covers the use of mathematical expressions, command-line argument handling, and the 'break' and 'continue' commands. Additionally, it includes practice questions to reinforce the concepts discussed.

Uploaded by

menoftech9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Conditional

Statements
and Loops
in Bash
G. Anushiya Rachel
Shiv Nadar University,
Chennai
Conditional Statements

2
if-then
• Syntax1: • Syntax 3:
if command if command
then then
commands
commands
fi
elif
• Syntax 2:
then
if command
then commands
commands fi
else
commands
fi

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 3


Example

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 4


Examples

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 5


Testing a condition
• Syntax:
if [ condition ] Note: space after [
then
commands
fi

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 6


Example
Note: Floating-point
numbers cannot be used

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 7


String Comparison

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 8


Examples

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 9


Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 10
Checking Files

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 11


Double Parenthesis for Math Expression
• if (( expression )); then statements; fi
• The expression can be any mathematical assignment or comparison.
• Additional operators available with double parenthesis:

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 12


Example
val1=10
if (( $val1 ** 2 > 90 ))
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi

• Note [ [ expression ] ] can be used for advanced string comparisons using regular
expressions.

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 13


$# and exit commands
• $# returns the number of command-line arguments
• To check and exit a program in the event of incorrect number of arguments:
if [ $# -ne 3 ]
then
echo “Enter 3 arguments”
exit
fi

• exit is used to abort a shell script/exit the shell


• Can be used provide a desired exit status code
• Example: exit 5

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 14


Examples

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 15


What if exit code exceeds 255?

If the assigned exit status code


exceeds 255, then the code is
made to fit between 0 and 255
by performing modulo with 256.

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 16


case command
• Instead of using if-else to check the contents of same variable, use case.
• Syntax:
case variable in
pattern1 | pattern2) commands;*
pattern3) commands;*
** default commands;*
esac

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 17


Example
case $USER in
rich | barbara)
echo "Welcome, $USER"
echo "Please enjoy your visit";*
testing)
echo "Special testing account";*
jessica)
echo "Do not forget to log off when you're done";*
**
echo "Sorry, you are not allowed here";*
esac

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 18


Loops

19
for loop
• Syntax:
Try to use this list of words instead:
for var in list I don’t know if this’ll work
do
Use \ to escape the single quotes.
commands
done
• Example:
for test in Alabama Alaska Arizona Arkansas California
Colorado
do
echo The next word is $test
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 20


Example

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 21


Reading list from variable, command
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut“ # Appends Connecticut to the string/list
for state in $list
do
echo "Have you ever visited $state?"
done

for state in $(cat file)


do
echo "Visit beautiful $state"
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 22


Changing the Field Separator
• By default, the bash shell uses spaces, newlines, and tab spaces as field separators.
• This can be changed by temporarily changing the environment variable IFS in the script.
• IFS = $’\n’ # to recognize only new line as field separator
• IFS = : # set field separator to :
• IFS = $’\n’:;” # set a list of field separators

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 23


Iterate through files in a directory
for file in /home/rich/test/*
do
if [ -d "$file" ]
then
echo $file is a directory“
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 24


Combining lists
for file in /home/rich/.b* /home/rich/testfile
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
else
echo "$file doesn't exist"
fi
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 25


C-Style for loop
• Syntax:
for (( variable assignment ; condition ; iteration process ))
• Example: for (( a = 1; a < 10; a++ ))
• Notice these changes from standard bash
• The assignment of the variable value can contain spaces.
• The variable in the condition isn’t preceded with a dollar sign.
• The equation for the iteration process doesn’t use the expr command format.

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 26


Examples

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 27


while
• Syntax:
while command
do
commands
done
• Format for command is the same as that in if-then statements.
• Example:
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done
Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 28
Multi-command while
• You can also use multiple test commands in a while command. However, the loop ends
only based on the last test command.
• Each test command must be on a separate line or separated by ;
• Example:
var1=10
while echo $var1
[ $var1 -ge 0 ]
do
echo "This is inside the loop"
var1=$[ $var1 - 1 ]
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 29


until
• Syntax:
until commands
do
commands
done
• Example:
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 30


break and continue
• break – to escape a loop
• continue - stop processing commands inside of a loop but not terminate the loop
completely
• Example:
for var1 in 1 2 3 4 5 6 7 8 9 10
do
if [ $var1 -eq 5 ]
then
break
fi
echo "Iteration number: $var1"
done
echo "The for loop is completed"
Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 31
Breaking out of an inner loop
for (( a = 1; a < 4; a+* ))
do
echo "Outer loop: $a"
for (( b = 1; b < 100; b+* ))
do
if [ $b -eq 5 ]
then
break
fi
echo " Inner loop: $b"
done
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 32


Breaking out of an outer loop (break n)
for (( a = 1; a < 4; a+* ))
do
echo "Outer loop: $a"
for (( b = 1; b < 100; b+* ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " Inner loop: $b"
done
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 33


continue – Example
for (( var1 = 1; var1 < 15; var1+* ))
do
if [ $var1 -gt 5 ] &* [ $var1 -lt 10 ]
then
continue
fi
echo "Iteration number: $var1"
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 34


continue n
for (( a = 1; a <* 5; a+* ))
do
echo "Iteration $a:"
for (( b = 1; b < 3; b+* ))
do
if [ $a -gt 2 ] &* [ $a -lt 4 ]
then
continue 2
fi
var3=$[ $a * $b ]
echo " The result of $a * $b is $var3"
done
done

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 35


Practice Questions
• Check if a number is even or odd using case.
• Check if filetype is .txt, or .sh, or .py
• Print squares of the numbers 5, 10, 12, 2, 4 using a for loop.
• Print the first line of each txt file in a directory using a for loop.
• Read numbers from a user until the sum exceeds 100. (Use while)
• Print prime numbers less than N. (Get N from the user. Use until)

Shell Scripting - G. Anushiya Rachel, Shiv Nadar University, Chennai 36

You might also like