Bash
Bash Examples: If, Else If, Else
March 1, 2023
This guide is part of the "Snippets" series. This series is focused on providing simple and accessible tutorials on various topics relating to development!
Bash is a commonly used command-line interface for Unix-based systems. It provides a variety of tools and utilities for managing and automating tasks on your computer. The IF, ELIF, and ELSE statements are used in Bash to perform conditional operations based on the evaluation of a certain condition.
Here's an example of how to use the IF
statement:
if [ condition ]; then
# do something
fi
The condition
in this statement is the test that will be evaluated to either true or false. If the condition is true, then the commands inside the if
block will be executed. If the condition is false, the commands inside the if
block will be skipped.
For example, let's say you want to print a message if a variable called num
is greater than 10. Here's how you could write that in Bash:
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
fi
In this example, the -gt
flag is used to compare the variable num
to the value 10
. If num
is greater than 10, then the message "The number is greater than 10." will be printed to the console.
Now, let's say you want to perform a different action if the num
variable is less than or equal to 10. In that case, you can use the ELIF
statement:
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
elif [ $num -le 10 ]; then
echo "The number is less than or equal to 10."
fi
In this example, the elif
statement is used to perform a different action if the if
condition is false. If num
is less than or equal to 10, then the message "The number is less than or equal to 10." will be printed to the console.
Finally, you can use the ELSE
statement to perform a default action if none of the conditions in the if
or elif
statements are true:
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
elif [ $num -le 10 ]; then
echo "The number is less than or equal to 10."
else
echo "The number is not a valid input."
fi
In this example, the else
statement is used to perform a default action if the if
and elif
conditions are both false. If num
is not a valid input, then the message "The number is not a valid input." will be printed to the console.
Checking if a file exists
if [ -f myfile.txt ]; then
echo "The file exists."
else
echo "The file does not exist."
fi
Checking if a number is positive, negative, or zero
read -p "Enter a number: " num
if [ $num -gt 0 ]; then
echo "The number is positive."
elif [ $num -lt 0 ]; then
echo "The number is negative."
else
echo "The number is zero."
fi
Checking if a string is empty or not
read -p "Enter a string: " str
if [ -z "$str" ]; then
echo "The string is empty."
else
echo "The string is not empty."
fi
Checking if a directory exists and creating it if necessary
if [ -d mydirectory ]; then
echo "The directory exists."
else
mkdir mydirectory
echo "The directory has been created."
fi
How to run a Bash file
To save and run a Bash file, you need to create a file with the .sh
extension and then make it executable. Here's how you can do it:
- Open a text editor (such as Vim, Nano, or Sublime Text) and create a new file.
- Add the Bash commands that you want to run in the file. For example, you could add the following commands:
#!/bin/bash
echo "Hello, World!"
This script will print the message "Hello, World!" to the console.
- Save the file with a
.sh
extension. For example, you could save the file asmyscript.sh
. - Make the file executable by running the following command in the terminal:
chmod +x myscript.sh
This command sets the executable bit on the file, allowing you to run it as a script.
- Run the script by typing the following command in the terminal:
./myscript.sh
This command runs the Bash script and prints the output to the console.
The console output will depend on the commands in your Bash script. In the example above, the output would be:
Hello, World!
If your script contains multiple commands, each command's output will be printed to the console as it is executed.
That's the basic process for saving and running a Bash file.