Scripting Classroomnotes 08/Feb/2023

Exploring the if then else

  • If the else
if command
then
    commands
else
    commands
fi
  • if then elif
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)

  • refer here for function

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner