Shell Scripting

Shell Scripting

⭐ Day 04 of #90DaysofDevOps

Hi Everyone, welcome to Day 04 of the #90DaysofDevops learning journey. In this, article we will cover Shell Scripting, and what is difference between #!/bin/bash and #!/bin/sh. How to write the first shell scripting Program. Let's get started...

🔸 Shell Scripting

Shell scripting is an important part of process automation in Linux. You can write a series of commands in a file and then execute them using scripting.

When a shell is used interactively, it displays a $ when it is waiting for a command from the user. This is called the shell prompt.

[username@host ~]$

If the shell is running as root, the prompt is changed to #. The superuser shell prompt looks like this:

[root@host ~]#

A shell script involves the following basic elements

  • Shell Keywords – if, else, break, etc.

  • Shell commands – cd, ls, echo, pwd, touch, etc.

  • Functions Control flow – if..then..else, case and shell loops, etc.

🔴The file extension of sh.

By naming conventions, bash scripts end with a .sh. However, bash scripts can run perfectly fine without the sh extension.

Scripts are also identified with a shebang. Shebang is a combination of bash # and bang ! followed the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

Below is an example of the shebang statement.

#! /bin/bash

🔴Execution rights

Scripts have execution rights for the user executing them.

An execution right is represented by x. In the example, a user has the rwx (read, write, execute) rights for the file devops_script.sh

chmod -x devops_script.sh

🔴File color

Executable scripts appear in a different color from the rest of the files and folders.

In my case, the scripts with execution rights appear in green.

🔸 #!/bin/bash and #!/bin/sh

/bin/sh and /bin/bash are both Unix/Linux shell interpreters. bash and share two different shells.

Bin/sh

In Linux, users use the programming language called “sh” to code into the shell. “Sh” is also known as “Bourne Shell,” It was developed to be used with UNIX systems defined using the POSIX standards.

As shell follows the POSIX standards, a script written in Shell can be used in every Linux system without any problems. The same can’t be said for Bash. As Bash added better extensions and features, it slowly went away from the POSIX standards, thus affecting its portability.

Bin/bash

“Bash” is similarly a shell programming language. It is also known as “Bourne Again Shell”. Today, Bash is the default programming language for the shells in Linux. Bash has the same abilities as Shell, but it developed other functions and better extensions over time.

Bash can be called an implementation of Shell developed for the GNU project. As mentioned before, it includes all the shell features, but it developed better extensions. Hence, it can also be called a superset of Shell, including ideas drawn from other shells such as the C shell.

We choose Bash as the superior shell language since it is a more recent and enhanced version of Shell. Bash vs Shell has several strong features and advantages. So, it is simple to choose Bash over Shell while writing our Shell scripts.

GNU Bash: #!/bin/bash; POSIX shell: #!/bin/sh

🔸 Create and run your first shell script

Let’s first create a new directory named script_program that will host all our bash scripts.

mkdir script_program

Now inside this script_program directory', create a new file named devops_script.sh using the cat command

cat > devops_script.sh

insert the following line in it by typing it in the terminal:

echo "I will complete the #90DaysofDevOps challenge"

Press Ctrl+d to save the text to the file and come out of the cat command.

You can also use a terminal-based text editor like 'vim' or 'nano'. If you are using a desktop Linux. Also, you can use visual studio code.

So, you are using the echo command to print "I will complete the #90DaysOofDevOps challenge". You can use this command in the terminal directly but in this task, you'll run this command through a shell script.

Now make the file devops_script.sh executable by using the chmod command as follows

chmod -x devops_script.sh

Finally, execute your first shell script by adding your preferred shell "bash" before devops_script.sh:

You can run the script in the following ways:

./devops_script.sh or bash devops_script.sh

bash devops_script.sh

You'll see "I will complete the #90DaysOofDevOps challenge" printed on the screen.

Here's a screenshot of all the steps you saw above:

observation, after executing the script with the help of bash devops_script.sh name green.

Task 1: Write a Shell Script to take user input, input from arguments, and print the variables.

In bash, we can take user input using the read command.

#!/bin/bash
echo "Enter your First Name: "
read first_name
echo "Hello $first_name"

read -p "Enter last_name: "last_name
echo "Your last name is $last_name"

To prompt the user with a custom message, use the -p flag.

read -p "Enter your age" variable_name

Task 2: Write an Example of If else in Shell Scripting by comparing 2 numbers.

#!/bin/bash

read -p "Enter first number= " num1
read -p "Enter Second number= " num2

if [ $num1 -eq $num2 ]; then
        echo "Both numbers are equal."
elif [ $num1 -gt $num2 ]; then
        echo "num1 is greater than num2."
else
        echo "num1 is lesser than num2."
fi

🎉 Task Done👍 Day 04 of #90daysofdevops 💯

In this article, we have covered the Day 04 task of how to take input from users and with the help of If else in Shell Scripting compared two numbers.

If you enjoyed this article please like it and share it with your friends and colleagues!

Thank you for reading🤓