Day 4: Shell Scripting (Part I)

Day 4: Shell Scripting (Part I)

This blog explains shell scripting and basic shell scripting commands used in Linux.

Shell Scripting

Linux shell scripting: It is used to write scripts that can be run by a Linux shell, such as bash (Bourne Again Shell). These scripts automate tasks, perform system administration tasks, and facilitate the interaction between users and the operating system. The program written in shell is called scripts. These programs are written in human readable form that take input from keyboard, Later these programs are converted in to binaries and transfer to the Kernel. So, the shell is a command-line interpreter between human and Kernel. These scripting files are saved with '.sh' extension.

Importance of Shell Scripting for DevOps Engineer

In day to day activities in DevOps, there are numerous programs that we need to run. To make it efficiently, shell scripting comes into play. Here we can write a number of scripts and save them as a file to be run we needed. Moreover, crone jobs are used to schedule any particular job without keeping track the date and time of the job run manually.

It is also used to automate repetitive system tasks, such as monitoring system resources, backing up files and managing user accounts. By turning a number of commands into a script, s DevOps engineer can save time, increase efficiency with accuracy, and simplify complex tasks.

Shell Scripting Basic Commands

To start a shell script we always write the first line '#!/bin/bash' as a comment that is also known as Shebang which ensures that the following scripts run over Bash.

Bash is derived from sh, which means that Bash supports features of sh but provides more extensions on top of that. Though, most of the commands work similarly as in sh.

Program-1: A Shell Script that prints "I will complete #90DaysOfDevOps challenge"

$vim myscript1.sh: create a script file

$echo: used to display the content written

File Permissions

By default a file is always has read permission but not executable. To make a file executable, one needs to change its permission. There are 3 types of file permissions Read, Write and Execute. As shown below:

'-' : in the beginning, indicates a file

'd' : in the beginning, indicates a directory

In numeric, for read use '4', for write use '2', for execute use '1' and '0' stands for no permission. Now we can make different combination as per the permission we need.

for permission 'rwx' : 4+2+1=7. So, above permission can be written as '777'.

By default a file always has the permission 'rw-rw-r--', which means it can be read or write by owner and group but only read by others. A file that is not executable will always displays in white color.

To change the permission of file, we need to change its mode by using the command 'chmod' as shown below. An executable file always displays in green color.

$chmod +x <filename>

$ ./myscript1.sh: execute the file myscript.sh

Program-2: Shell Script that takes user input, input from arguments, and prints the variables

$1: will take input from user as argument and $read also does same

$ ./myscript1.sh $1 $2 $3: will run the script with 3 arguments

Program-3: If-Else statement in Shell Scripting for comparing two numbers

$ if [condition]

then

{if statement true}

else

{if statement false}

fi

$ <<com ...... com : for multiline comment

The above program will compare two numbers passed as an argument and print the result.

Here we hands on the basic shell scripting commands in Linux. Hope you find it helpful. In the next blog we will learn Advance Shell scripting. So stay tuned.

Thanks for reading my blog. Hope you find it interesting and helpful. - Neha Bhardwaj