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
Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a comment

Turn on JavaScript to leave a comment.

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

Please turn AdBlock off
Plugin for Social Media by Acurax Wordpress Design Studio
Exit mobile version
%%footer%%