KandZ – Tuts

We like to help…!

Linux CLI 59 🐧 shell scripts flow control and if

a - flow control in shell scripts
flow control is an essential aspect that allows you to manage the execution of commands...
based on certain conditions or loops.
It helps in structuring the script to perform different tasks under varying circumstances.
Shell scripting supports several flow control constructs, such as if, for, while, and until loops.
Key Flow Control Constructs → if, If-Else and Elif (Else-If) statements
Loops → for, while and until loops

b - if, else and elif in shell scripts
If statements are fundamental components for controlling the flow of execution in shell scripts.
They allow parts of your script to run only if certain conditions are met, making your scripts more dynamic and responsive.
basic syntax
if [ condition ]; then
Commands to be executed if the condition is true
elif [ condition ]; then
Commands to be executed if the condition is true
else
Commands to be executes if the condition is false
fi
read -p "Enter a number: " num → prompts the user to enter a number.
if [ $num -lt 0 ]; then → checks if num is less than 0
elif [ $num -eq 0 ]; then → cheks if num is equal to 0
else → if all false then execute commands from else

c - operators in shell scripts
Operators in shell scripts are essential for performing comparisons, logical operations, and arithmetic calculations
Types of Operators:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Logical Operators
4. String Operators
Arithmetic Operators:
→ Addition
→ Subtraction
→ Multiplication
/ → Division
% → Modulo (remainder)
Comparison (Relational) Operators:
-eq → Equal to
-ne → Not equal to
-gt → Greater than
-ge → Greater than or equal to
-lt → Less than
-le → Less than or equal to
Logical Operators
&& → Logical AND
\|\| → Logical OR
! → Logical NOT
String Operators
= → Equal to
!= → Not equal to
-z → Null string (length zero)
-n → Non-null string
→ Less than in ASCII order
→ Greater than in ASCII order

Leave a Reply