#90DaysOfDevOps - Day 4: Linux Basic Shell Scripting

#90DaysOfDevOps - Day 4: Linux Basic Shell Scripting

ยท

7 min read

Introduction

Today we learn the basics of Linux scripting. Linux scripting refers to the process of writing and executing scripts on the Linux operating system. A script is a set of commands written in a scripting language that automates various tasks and processes on the Linux system. Scripting in Linux is an essential skill for system administrators, developers, and power users, as it allows them to automate repetitive tasks, customize system behavior, and efficiently manage Linux environments. Before we move further, I would like to give some information on a few terminologies.

What is Kernel?

The kernel is like the brain of the Linux operating system. It manages all the important tasks, like memory, running programs, and communication with hardware. It makes sure everything works together smoothly.

What is Shell?

Think of the shell as the interface to talk to the kernel. It's like a command interpreter. You can give it commands, and it will pass them to the kernel, which then carries out those commands. In the given figure we can see different users interacting with Kernel via Shell using Linux commands.

There are various kinds of shells the most common or widely used is the Bash shell (Bourne Again SHell). The other shells include CSH (C SHell), KSH (Korn SHell), etc.

What is a Terminal?

The terminal is like a window to the shell. It provides a place where you can type commands and see the responses from the system. It's where you can directly talk to the shell and tell it what to do.

What is shell scripting?

Linux scripting is about writing and using scripts on Linux to make tasks easier. Scripts are sets of commands that can do things automatically on a Linux system. It's a valuable skill for system admins, developers, and advanced users because it saves time, customizes the system, and helps manage Linux better. With Linux scripting, you can get things done faster and have more control over your computer.

Basic shell script

  1 #!/bin/sh
  2 
  3 #print "This is my first script"
  4 echo "This is my first script"

line 1:
The script always starts with a shebang line #!/bin/sh. The shebang is a special line that tells the operating system which interpreter to use when executing the script. It starts with #! followed by the path to the interpreter.

In this script #!/bin/sh tells the interpreter to use the system shell interpreter /bin/sh. The other shebangs are
#!/bin/bash use the bash shell interpreter
#!/bin/zsh use the Z shell interpreter
#!/bin/ksh use the Korn shell interpreter
#!/bin/dash use the Dash shell interpreter
#!/usr/bin/env perl here the system looks for the perl interpreter whose path is defined in the /usr/bin/env

line 3:
The script supports comments. It starts with # for single-line comment and we can use : ' for a multiline comment.

Note: The multiline comment should start with : ' and end with ' . The spacing should be correct.

: '
This is a multiline 
comment
 '

line 4:
We can use any Linux commands that are executed when the script is executed.
Here we use the echo command to print This is my first script

How to execute the script?

The script is created with '.sh' extension. We can use the sh <script_name>.sh or bash <script_name>.sh command to run the script. We can also change the permission of the script to executable by owner, group, and others and then use the ./<srcipt_name>.sh command to run the script.

The script executes in a sequential manner, line by line. If any error occurs while executing the script, it continues until the last command is executed.

ubuntu@~/scripts$: bash print.sh 
This is my first script
ubuntu@~/scripts$: chmod +x print.sh 
ubuntu@~/scripts$: ls -al print.sh 
-rwxrwxr-x 1 ubuntu ubuntu 116 Jul 31 18:02 print.sh
ubuntu@~/scripts$: ./print.sh 
This is my first script

Script to read input from the user

In computer programming, reading input from the user is essential for interactive applications. It lets programs get data or instructions directly from users while the program is running. This makes applications more versatile, responsive, and personalized because they can adapt based on user input.

  1 #!/bin/sh
  2 
  3 echo "Please enter your name : "
  4 
  5 #wait for user to enter data
  6 read name
  7 
  8 #print
  9 echo "Hi $name. Welcome to the world of Linux scripting."

line 3:
We print a user-friendly message asking the user to enter his/her name.

line 6:
We use the read command to wait for the user to input his/her name. The value entered is assigned to the name variable. Variables are used to store values.

line 9:
We print a message with the name variable value. To use the value of the variable we use $name.

Output:

ubuntu@~/scripts$: vim userinput.sh
ubuntu@~/scripts$: chmod +x userinput.sh 
ubuntu@~/scripts$: ./userinput.sh 
Please enter your name : 
Praveen
Hi Praveen. Welcome to the world of Linux scripting.

Passing arguments to script

Arguments are simple parameters that are given while running the command to execute the script, and the values of these arguments are passed on to the script during script execution.

  1 #!/bin/sh
  2 
  3 #first argument source directory
  4 source=$1
  5 #second argument destination directory
  6 destination=$2
  7 
  8 cp -r $source $destination
  9 
 10 echo "$source directory copied to $destination directory successfully."

This script will copy the source directory to the destination directory. The source directory and destination directory are passed as arguments to the script.

line 4:
The arguments that are passed to the script are numbered with $1 $2 $3 .... $n n being the total number of arguments. Here we are expecting the user to pass the source directory as the first argument $1 . We assign the source directory to the source variable.

line 6:
We are expecting the user to pass the destination directory as the second argument $2 . We assign the destination directory to the destination variable.

line 8:
We use the cp command with -r option to copy the source to destination directory.

line 10:
We inform the user with a message that the source directory has been copied to the destination directory.

Output:

ubuntu@~/scripts$: ./copydir.sh /home/ubuntu/scripts/ /home/ubuntu/scripts_temp/
/home/ubuntu/scripts/ directory copied to /home/ubuntu/scripts_temp/ directory successfully.
ubuntu@~/scripts$: ls /home/ubuntu/
scripts  scripts_temp

We are passing two arguments /home/ubuntu/scripts/ as the source and /home/ubuntu/scripts_temp/ as the destination directory to the copydir.sh script.

If...else condition

if-else statement is used to perform the operations based on some specific condition. If the given condition is true, then the code inside the if block is executed, otherwise else block code is executed. It specifies an order in which the statements are to be executed.

  1 #!/bin/sh
  2 
  3 #first argument, first number
  4 num1=$1
  5 
  6 #second argument, second number
  7 num2=$2
  8 
  9 #compare the two numbers
 10 if [ $num1 -gt $num2 ]
 11 then
 12         echo "$num1 is greater than $num2"
 13 elif [ $num1 -lt $num2 ]
 14 then
 15         echo "$num1 is less than $num2"
 16 else
 17         echo "$num1 is equal to $num2"
 18 fi

This script compares two numbers and displays whether the first number is greater than, less than, or equal to the second number.

line 4:
Assign the first argument $1 i.e. first number to num1 variable.

line 7:
Assign the second argument $2 i.e. second number to num2 variable.

line 10:
Use if statement to first compare whether $num1 is greater than $num2.

line 12:
If the condition is true then display $num1 is greater than $num2.

line 13:
If the condition is false then use elif (nester if..else) to again compare whether $num1 is less than $num2.

line 15:
If the condition is true then display $num1 is less than $num2.

line 16:
If all the conditions evaluate to false then we can use else statement to process final false condition.

line 17:
If all the conditions evaluate to false then display $num1 is equal to $num2.

Output:

ubuntu@~/scripts$: ./compare_numbers.sh 1 2
1 is less than 2
ubuntu@~/scripts$: ./compare_numbers.sh 2 1
2 is greater than 1
ubuntu@~/scripts$: ./compare_numbers.sh 2 2
2 is equal to 2

What is the importance of shell scripting for a DevOps engineer?

As a DevOps engineer, mastering basic Linux shell scripting is crucial for automating tasks, managing infrastructure, and deploying applications. Shell scripting allows you to combine commands, create loops, and perform conditional operations in a batch-like manner.

Conclusion

In conclusion, Linux scripting is the process of writing and executing scripts on the Linux operating system. It involves using commands in a scripting language to automate tasks and manage the system efficiently. Learning Linux scripting is essential for system administrators, developers, and power users, as it saves time, customizes system behavior, and provides more control over the Linux environment. Mastering basic shell scripting is crucial for DevOps engineers, as it allows them to automate tasks, manage infrastructure, and deploy applications effectively.

"๐ŸŒฑ Keep learning, and spread the knowledge to inspire others. ๐Ÿš€๐Ÿ’ก"

Go back to the main page

ย