Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Create Simple Shell Scripts: Conditionally Execute Code (use of: if, test, [], etc.)
In Linux, you can conditionally execute code using commands such as if
, test
, and []
. Conditional execution allows you to run specific code blocks based on certain conditions.
Using Conditions with Tests
The if
statement is used for conditional branching in Linux shell scripting. Here's the basic syntax of an if
statement:
if [ condition ]
then
# code to execute if the condition is true
else
# code to execute if the condition is false
fi
In the above syntax, the condition
is an expression that evaluates to either true
or false
. If the condition is true
, the code block inside the then
section will be executed. Otherwise, if the condition is false
, the code block inside the else
section will be executed.
The else
section is not required so if you do not need to have an action take place when the if
statement resolves to false, then do not add an else
block.
You can use the test
command or square brackets []
to evaluate conditions.
Check if the /etc/passwd
file exists using both test
and []
methods
if test -f /etc/passwd
then
echo "file.txt exists and is a regular file"
fi
if [ -f /etc/passwd ]
then
echo "file.txt exists and is a regular file"
fi
The -f
option is used to check if file.txt
exists and is a regular file. If the condition is `true``, the message "/etc/passwd exists and is a regular file" will be displayed.
You can also combine conditions using logical operators such as -a
(logical AND) and -o
(logical OR) within the test
command or []
.
Check if the var
variable is greater than 10 AND less than 20:
var=15
if [ "$var" -gt 10 -a "$var" -lt 20 ]
then
echo "The value of var is between 10 and 20."
else
echo "The value of var is NOT between 10 and 20."
fi
Change the value of var
to fully test the condition.
Check if the var
variable is less than 10 OR greater than 20:
var=8
if [ "$var" -lt 10 -o "$var" -gt 20 ]
then
echo "The value of var is NOT between 10 and 20."
else
echo "The value of var is between 10 and 20."
fi
Change the value of var
to fully test the condition.
Exit Codes
If a command returns with a zero exit status, the command succeeded. If the command returns a non-zero exit code, the command failed.
Some commands may still have succeeded with a non-zero exit code. Always consult the man pages if unsure.
To check the exit code, run echo $?
immediately after the command.
Checking the success of the command grep
by checking for bash
in /etc/passwd
:
grep bash /etc/passwd
echo $?
The output of the echo
command should return 0
as bash
was found in the /etc/passwd
file.
Checking the failure of the command grep
by checking for trash
in /etc/passwd
:
grep trash /etc/passwd
echo $?
The output of the echo
command should return 1
as trash
was NOT found in the /etc/passwd
file.
Using Conditions with Commands
In addition to using conditions with tests, you can also use conditions with commands.
if command
then
# code to execute if the command succeeds (returns a zero exit status)
else
# code to execute if the command fails (returns a non-zero exit status)
fi
Attempt to update the timestamp on the /etc/passwd
file:
if touch /etc/passwd 2>/dev/null
then
echo "Updated timestamp"
else
echo "Failed to update timestamp"
fi
If performed as a normal user, the command will fail, printing to screen "Failed to update timestamp". Switch to the root
user with sudo -i
and try running again to receive the message "Updated timestamp". Type exit
when done to return to your non-root account.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.