Decision Making

In programming, Decision Making is one of the important concepts. The programmer provides one or more conditions for the execution of a block of code. If the conditions are satisfied then those block of codes only gets executed.

Two types of decision-making statements are used within shell scripting. They are –

1. If-else statement:

If else statement is a conditional statement. It can be used to execute two different codes based on whether the given condition is satisfied or not.

There are a couple of varieties present within the if-else statement. They are –

  • if-fi 
  • if-else-fi 
  • if-elif-else-fi 
  • nested if-else

The syntax for the simplest one will be –

Syntax of If-else statement:

if [ expression ]; then

    statements

fi

Example Script:

Name="Satyajit"
if [ "$Name" = "Satyajit" ]; then
  echo "His name is Satyajit. It is true."
fi

Output of if-else statement:

His name is Satyajit. It is true.

In the above example, during the condition checking the name matches and the condition becomes true. Hence, the block of code present within the if block gets executed. In case the name doesn’t match then will not have an output. Below is the terminal shell pictorial depiction after executing the following script – 

 

2. case-sac statement:

case-sac is basically working the same as switch statement in programming. Sometimes if we have to check multiple conditions, then it may get complicated using if statements. At those moments we can use a case-sac statement. The syntax will be –

Syntax of case-sac statement:

case $var in
   Pattern 1) Statement 1;;
   Pattern n) Statement n;;
esac

Example Script:

Name="Satyajit"
case "$Name" in
    #case 1
    "Rajib") echo "Profession : Software Engineer" ;;
    
    #case 2
    "Vikas") echo "Profession : Web Developer" ;;
    
    #case 3
    "Satyajit") echo "Profession : Technical Content Writer" ;;
esac

Output of case-sac statement:

Profession : Technical Content Writer

In the above example, the case-sac statement executed the statement which is a part of the matched pattern here the ‘Name’. Below is the terminal shell pictorial depiction after executing the following script – 

 

Bash Scripting – Introduction to Bash and Bash Scripting

Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to automate different types of tasks in a system. Developers can avoid doing repetitive tasks using bash scripting. 

Bash scripting supports variables, conditional statements, and loops just like programming languages. Below are some of the applications of Bash Scripts – 

Table of Content

  • Applications of Bash Scripts:
  • Advantages of Bash Scripts:
  • How to Write Bash Scripts?
  • File Names and Permissions
  • Variables
  • Input and Output
  • Functions
  • Decision Making
  • String and Numeric Comparisons

Similar Reads

Applications of Bash Scripts:

Manipulating files Executing routine tasks like Backup operation Automation...

Advantages of Bash Scripts:

It is simple. It helps to avoid doing repetitive tasks Easy to use Frequently performed tasks can be automated A sequence of commands can be run as a single command....

How to Write Bash Scripts?

To write a Bash Script we will follow the steps –...

File Names and Permissions

In the above example, we have saved the file using gfg.sh name and also provided execute permission using chmod command. Now, let’s understand why we have done that....

Variables

We can use variables in bash scripting. Below is a sample program to understand the usage of variables in Bash scripts....

Input and Output

Input & output are fundamental concepts for shell scripting. A script can take one or more inputs and can also produce zero or many outputs. It may even produce some errors.  Let’s understand this with an example –...

Functions

In programming, A function is a block of code that performs some tasks and it can be called multiple times for performing tasks. The simplest example of the use of function in Bash scripting can be given as –...

Decision Making

In programming, Decision Making is one of the important concepts. The programmer provides one or more conditions for the execution of a block of code. If the conditions are satisfied then those block of codes only gets executed....

String and Numeric Comparisons

The string comparison means in the shell scripting we can take decisions by doing comparisons within strings as well. Here is a descriptive table with all the operators –...

Conclusion

In this article we discuss Bash scripting which plays a vital role in automating tasks within the Linux environment. As a command-line interpreter, Bash offers simplicity and ease of use for developers to create scripts that streamline routine operations, manipulate files, and execute complex tasks. While Bash scripts can enhance productivity by avoiding repetitive actions and executing sequences of commands efficiently, they also come with considerations such as managing permissions, handling variables, and implementing decision-making structures. By mastering Bash scripting, users can harness its power to optimize their workflow and enhance system administration tasks on Linux platforms....