Exploring the if then else
if command
then
commands
else
commands
fi
if command
then
commands
elif command2
then
commands
fi
Functions
- A reusable block of code is called as function
- Lets write a reusable code to get the distribution
#!/bin/bash
# function definition to get distribution
get_distribution() {
distribution=""
# Check if /etc/os-release file exists
if [ -r /etc/os-release ]; then
distribution="$(. /etc/os-release && echo "$ID")"
fi
echo $distribution
}
# usage
echo $(get_distribution)
Like this:
Like Loading...