Shell Scripting Basics

Tejashree Salvi
6 min readMay 14, 2023

Day 4/90 with Shubham Londhe

As a DevOps engineer, you might be always looking for automating tasks, and streamline workflows. Here comes the shell scripting into the picture.

Pre-requisite for Shell Scripting: Linux Basics.

Let’s get started!

What is Kernel?

Linux kernel is responsible for managing system resources such as the CPU, memory, and input/output devices. When you start Firefox, the kernel interacts with the hardware to allocate system resources to the program and ensure that it runs smoothly.

What is Shell?

Shell is a program that provides a user interface to interact with the Linux OS. It allows the user to enter commands eg: ls (list the files), the shell interprets these commands and runs on behalf of the user.

Types of Shell in Linux

  1. Bash (Bourne-Again SHell): This is the most commonly used shell in Linux and is the default shell for most distributions.
  2. Zsh (Z SHell): similar to Bash, but offers additional features and customization options.
  3. Ksh (Korn SHell): compatible with both the Bash and C shells, and offers advanced scripting capabilities.
  4. Csh (C SHell): syntax similar to the C programming language and is popular among developers.
  5. Tcsh (TENEX C SHell): an improved version of the C shell, with additional features and improvements.

What is Linux Shell Scripting?

  • Shell Scripting is a process of writing scripts/programs that can be run on Linux CLI. It is written in shell language such as Bash which is used to automate repetitive tasks and many more.
  • Shell scripting is interpreted and not compiled. When a script is executed, the shell reads the scripts line by line, rather than compiling it at once and converting it into binary. This helps to modify the scripts without re-compiling. However, Shell scripts may run more slowly than compiled programs.
  • It is used to perform a variety of DevOps tasks, such as automating deployments, managing infrastructure, and deploying the code as well as monitoring.

Enough of theory, Let’s do some hands-on :)

Let’s create a shell script for automating the process of creating a new directory and changing it into the directory.

#!/bin/bash

# create a new directory
mkdir scripts

#change to new directory
cd scripts
  • Save the file with .sh the extension. eg “change_dir.sh”
  • Check the permission of the script file. It should have to execute permission chmod 777 change_dir.sh
  • To execute the script ./change_dir.sh

What is Shebang?

In above code #!/bin/bash is called a Shebang.

  • Shebang is a special character in a script that tells the OS which interpreter to be used for executing a script. We will be using Bash Interpreter.
  • It starts with #! followed by the path of the interpreter. /bin/bash is the path to the interpreter executable file.

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

#!/bin/bash

echo "I will complete #90DaysOofDevOps challenge"

Here’s what the script does:

  • #!/bin/bash : is a Shebang that specifies the interpreter ie BASH
  • echo : used to print the message.

To run the script:

  • Save the code into a file my_script.sh
  • Make it executable: chmod +x my_script.sh
  • then execute the script ./my_script.sh

Input and Output in Shell Scripting

User Input:

#!/bin/bash
echo "Enter your name?"
read name

echo "Hi $name, Thanks for reading the blog :)"

Here’s what the script does:

  • read name : command reads the user’s input and saves it to the $name variable.
  • echo : command outputs a message that includes the user’s name.

To run the script:

  • Save the code into a file user_input.sh
  • Make it executable: chmod +x user_input.sh
  • then execute the script ./user_input.sh

Command Line Argument:

Command Line Arguments are the parameters passed to the script while executing it.

  • Arguments are passed as a string and can be accessed using positional arguments.
  • $0 is used to store the file name, The first argument is accessed using $1 the second argument is accessed using $2 , and so on.
  • $# is used to get the total number of arguments.
#!/bin/bash

echo "The file name is: $0"
echo "Name: $1"
echo "Age: $2"
echo "Follow for more such content. $1"
echo "The total number of arguments is: $#"

Control Structures

Control structure helps to execute the conditional execution of code.

The most used control structure in Shell Scripting is :

  1. If-else Statements
  2. For-loops

If-else Statements

The if-else statement allows for the conditional execution of code.

The Syntax for If-else Statements

if [ condition ]
then
# Code to be executed if condition is true
else
# Code to be executed if condition is false
fi

Write a program using If else in Shell Scripting by comparing 3 numbers

#!/bin/bash

# Declare the a, b, c Find the greater number among them.
a=10
b=5
c=20

echo "Let's find the greatest number!"
echo "a = $a, b = $b, c = $c"

if [[ $a -gt $b && $a -gt $c ]]
then
echo "A = $a is greater"
elif [[ $b -gt $a && $b -gt $c ]]
then
echo "B = $b is greater"
else
echo "C = $c is greater"
fi

Here’s what the script does:

  • The if statement checks if the variable a is greater than both b and c using the -gt comparison operator, which stands for greater than. If this condition is true, it prints “A = $a is greater”.
  • If the first condition is false, the code checks the next condition using the elif statement, which checks if variable bis greater than both a and c. If this condition is true, it prints “B = $b is greater”.
  • If both the first and second conditions are false, the code executes the else block and prints “C = $c is greater”.

For loops

For loops allow for the repeated execution of code for a specific number of times

The Syntax for For Loops:

for variable in values
do
# Code to be executed
done

Write a program for displaying 1 to 10 numbers using For loops

#!/bin/bash
echo "For loop to print 1 to 10 numbers!"
for i in {1..10}
do
echo "Value of i is: $i"
done

Functions

Functions are small blocks of code that are easier to read and perform specific tasks.

#!/bin/bash

# Define a function for adding two numbers
add_numbers() {
sum=$(($1 + $2))
echo "The sum of $1 and $2 is: $sum"
}

# Call the function and pass two numbers as arguments
add_numbers 10 20

--

--