Shell scripting is a powerful tool in Linux that allows users to automate tasks and execute commands more efficiently. Two essential concepts in shell scripting are looping and conditional execution. In this article, we will explore how to effectively use these concepts in your scripts.
Looping
Looping allows us to repeat a set of commands multiple times. There are different types of loops available in shell scripting, including for
, while
, and until
loops.
For Loop
The for
loop is useful when you know the number of iterations in advance or when you want to loop through a list of items. Here's the syntax for a basic for
loop:
for variable in list
do
# Commands to be executed
done
Let's say we want to print numbers from 1 to 5 using a for
loop:
for num in 1 2 3 4 5
do
echo $num
done
This will output:
1
2
3
4
5
You can also use the seq
command to generate a sequence of numbers:
for num in $(seq 1 5)
do
echo $num
done
While Loop
The while
loop allows you to execute a block of commands as long as a certain condition is true. The condition is checked before executing the loop. Here's the syntax for a basic while
loop:
while condition
do
# Commands to be executed
done
Let's see an example of using a while
loop to print numbers from 1 to 5:
num=1
while [ $num -le 5 ]
do
echo $num
num=$((num+1))
done
This will produce the same output as the for
loop example.
Until Loop
The until
loop is similar to the while
loop but checks the condition before executing the loop. It keeps executing the commands until the condition becomes true. The syntax is as follows:
until condition
do
# Commands to be executed
done
Let's modify the previous example to use an until
loop instead of a while
loop:
num=1
until [ $num -gt 5 ]
do
echo $num
num=$((num+1))
done
Again, this will result in the same output.
Conditional Execution
Conditional execution allows you to execute commands based on certain conditions. The if
statement is commonly used for conditional execution.
If Statement
The if
statement checks a condition, and if it evaluates to true, the associated commands are executed. Here's the syntax for a basic if
statement:
if condition
then
# Commands to be executed if condition is true
else
# Commands to be executed if condition is false
fi
Let's say we want to check if a number is even or odd using an if
statement:
num=6
if [ $((num % 2)) -eq 0 ]
then
echo "$num is even"
else
echo "$num is odd"
fi
This will output:
6 is even
Case Statement
The case
statement is used when we have multiple conditions to check for. It's similar to a switch-case statement in other programming languages. Here's the syntax for a case
statement:
case variable in
pattern1)
# Commands to be executed if pattern1 matches
;;
pattern2)
# Commands to be executed if pattern2 matches
;;
pattern3)
# Commands to be executed if pattern3 matches
;;
*)
# Commands to be executed if no pattern matches
;;
esac
Let's see an example of using a case
statement to check the day of the week:
day=$(date +%u)
case $day in
1)
echo "Monday"
;;
2)
echo "Tuesday"
;;
3)
echo "Wednesday"
;;
4)
echo "Thursday"
;;
5)
echo "Friday"
;;
6)
echo "Saturday"
;;
7)
echo "Sunday"
;;
*)
echo "Invalid day"
;;
esac
This will display the corresponding day of the week based on the current date.
Conclusion
Looping and conditional execution are fundamental concepts in shell scripting. By using loops, you can repeat a set of commands for a specific number of iterations or until a condition becomes false. Conditional execution allows you to execute commands based on certain conditions. Understanding and applying these concepts will greatly enhance your ability to write effective shell scripts in Linux.
本文来自极简博客,作者:代码与诗歌,转载请注明原文链接:Linux Shell Scripting: Looping and Conditional Execution