Day4#90DaysOfDevOps Challenge

Shell Scripting

Day4#90DaysOfDevOps Challenge

What is Shell Scripting for DevOps:

Linux kernel does not understand the human language so, the shell helps in converting the human language to its readable format. And it gives us a way to run several commands.

Because Linux is open source and has several contributors, there are various types of shell-

  1. Bourne shell - sh

  2. Korn shell - ksh

  3. Bourne again shell - bash - ADVANCED AND SECURE

  4. POSIX shell - sh

  5. C shell - csh

  6. TENEX/TOPS C shell (tcsh)

Shell Script is running a set of Linux commands together by keeping them in a file. It's a command-line interpreter and runs on a Linux Shell.

The following are some common scripting languages.

  1. Powershell

  2. Python

  3. Pearl

  4. Groovy

Why it is important?

  • Shell scripting is important for DevOps engineers as it helps in automation and during the software development and deployment DevOps engineers might have to create useful utilities using scripting.

  • Sometimes having a good knowledge of shell scripting can reduce the time and effort of writing a long Python script to do any task which can be done with a few commands of Linux.

What is #!/bin/bash? Can we write #!/bin/sh as well?

#! is called the shebang header, which is used to tell the script which shell it has to run with.

# - hash

! - bang

The shebang must be the first line of the script.

#!/bin/bash - This means the script needs to run with the Bash shell.

#!/bin/sh - Specifies that the script should be run with the default system shell, which may or may not be bash. On some systems, /bin/sh is a symlink to /bin/bash, while on others it may be a different shell altogether.

If our script uses any specific bash features then, we should use #!/bin/bash. And if no specific bash feature is used then we can use #!/bin/sh to ensure that it will run with the default system shell on any system.

Examples of shell scripting:

  1. Write a Shell Script which prints that I will complete the #90DaysofDevOps challenge =>

\>Create a new file for ex. challenge.sh

\>Add the below command to the script using Vim or nano editor.

#!/bin/bash

echo "I will complete the #90DaysofDevOps challenge"

\>Give execution permission to the file -

chmod +x challenge.sh

\>Execute the script-

bash challenge.sh or ./challenge.sh

No alt text provided for this image

  1. Write a Shell Script to take user input and print the variables =>

\>Use the below script-

#!/bin/bash

echo "What is your name"

read name

echo "Hello $name"

\>Here the read command is used to read user input which will be stored for variable - name.

\>And then the $name will be replaced and printed by the echo command.

No alt text provided for this image

Take input from arguments and print the variables =>

The argument will be passed during running the script and it can be used inside the script by $1, $2, etc.

ex.

$ cat myscript
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
$ ./myscript hello world
First arg: hello
Second arg: world

No alt text provided for this image

  1. Write an Example of If else in Shell Scripting by comparing 2 numbers =>

No alt text provided for this image

Note: There are multiple ways of running a script-

1. bash filename or sh filename

2. By specifying the path of the script and chmod.

This is a standalone method to run a bash script. We have to execute the script as an executable, we can run the script anywhere provided we have a bash shell somewhere in the environment. To make it executable we need to make sure we have the rights to run the file as an executable. And for that, we use -

\>chmod +x filename.sh

The +x command will make sure the file is executable by everyone in the environment.

Now use the below command to execute the script. It takes into consideration that you are in the same directory as the file/ bash script is in.

\>./filename.sh


Thank you for reading!!📘