Course Content
Linux Essentials for DevOps Automation
The "Linux Essentials for DevOps Automation" training provides a comprehensive understanding of Linux fundamentals tailored to DevOps practices. By mastering these concepts, participants can effectively leverage Linux in DevOps environments to automate tasks, manage infrastructure, and deploy applications with efficiency and reliability.
0/15
Linux Essentials for DevOps Automation

Flow control in shell programming involves directing the execution of commands based on conditions or looping constructs.

 

Here’s an overview of flow control constructs in shell scripting:

 

1. Conditional Statements:

 

Conditional statements allow you to execute commands based on the evaluation of conditions.

 

if-else Statement:
if condition; then
# Commands to execute if condition is true
else
# Commands to execute if condition is false
fi

 

Example:
if [[ "$age" -ge 18 ]]; then
echo "You are an adult."
else
echo "You are a minor."
fi
 
 
[root@uadev ~]# bash if-else.sh
You are a minor.
[root@uadev ~]
 
 
 

 
Case Statement:

 

The case statement allows you to match a value against multiple patterns and execute commands accordingly.

 

case "$variable" in
pattern1)
# Commands for pattern1
;;
pattern2)
# Commands for pattern2
;;
*)
# Default commands
;;
esac
 

Example:

[root@uadev ~]# cat case.sh
case "$1" in
"yes")
echo "You chose yes."
;;
"no")
echo "You chose no."
;;
*)
echo "Invalid choice."
;;
esac
[root@uadev ~]#

 

[root@uadev ~]# ./case.sh no
You chose no.
[root@uadev ~]#
[root@uadev ~]#
[root@uadev ~]# ./case.sh yes
You chose yes.
[root@uadev ~]#
[root@uadev ~]#
[root@uadev ~]# ./case.sh NO
Invalid choice.
[root@uadev ~]#

 

 


 

2. Looping Constructs:

 

Looping constructs allow you to repeat a block of commands multiple times.

 

for Loop:

for item in list; do # Commands to execute for each item in the list done

 

Example:

[root@uadev tmp]# for i in test* ; do ls $i; done
test
test1
test2
test4
[root@uadev tmp]#

 

[root@uadev tmp]# for i in test* ; do ls -lrt $i;chmod 666 $i;ls -lrt $i;echo "---------------"; done
-rw-r--r--. 1 devnixops devnixops 22 Jun 5 21:56 test
-rw-rw-rw-. 1 devnixops devnixops 22 Jun 5 21:56 test
---------------
-rw-r--r--. 1 devnixops devnixops 26 Jun 5 21:58 test1
-rw-rw-rw-. 1 devnixops devnixops 26 Jun 5 21:58 test1
---------------
-rw-r--r--. 1 devnixops devnixops 26 Jun 5 21:59 test2
-rw-rw-rw-. 1 devnixops devnixops 26 Jun 5 21:59 test2
---------------
-rw-r--r--. 1 devnixops devnixops 82 Jun 5 22:06 test4
-rw-rw-rw-. 1 devnixops devnixops 82 Jun 5 22:06 test4
---------------
[root@uadev tmp]#

 


 

while Loop:

while condition; do
# Commands to execute as long as condition is true
done

 

Example:

[root@uadev tmp]# cat while.sh 
#!/bin/bash
counter=1
while [[ "$counter" -le "$1" ]]; do
echo "Count: $counter"
((counter++))
done
[root@uadev tmp]#

 

[root@uadev tmp]# ./while.sh 5
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
[root@uadev tmp]# ./while.sh 10
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Count: 6
Count: 7
Count: 8
Count: 9
Count: 10
[root@uadev tmp]#

 

 


 

until Loop:

until condition; do
# Commands to execute until condition becomes true
done

 

Example:

[root@uadev tmp]# cat until.sh 
#!/bin/bash
response="no"
until [ "$response" = "yes" ]; do
read -p "Do you want to continue? (yes/no): " response
done
[root@uadev tmp]#

 

[root@uadev tmp]# ./until.sh 
Do you want to continue? (yes/no): no
Do you want to continue? (yes/no): no
Do you want to continue? (yes/no): no
Do you want to continue? (yes/no): no
Do you want to continue? (yes/no): no
Do you want to continue? (yes/no): yes
[root@uadev tmp]#

 

Flow control constructs in shell scripting provide the flexibility to execute commands conditionally or repeatedly, allowing you to automate tasks and create complex workflows. By mastering these constructs, you can write efficient and powerful shell scripts to automate various tasks in a Unix-like environment. Practice and experimentation are key to becoming proficient in using flow control in shell programming.