Shell Scripting

Shell Scripting

·

9 min read

Shell Script Intro

  • A shell script is a computer program written in the shell programming language that consists of a series of commands to be executed by the shell.

  • Simply talking it is a method of scripting in a shell, or command-line interface for automating tasks that could also be performed manually. It involves writing commands and logic in a script file that can be executed by the shell to perform various tasks

    Shell Script Workflow


Use Cases

a) Automation of repetitive tasks: Shell scripts can be used to automate repetitive or complex tasks, making it easier to manage and maintain systems.

b) System administration: Shell scripts can be used to perform various system administration tasks, such as setting up a new user, managing disk space, or monitoring system performance.

c) Improved efficiency: Shell scripts can be written to run faster than manual methods, providing improved efficiency and reducing the amount of time spent on repetitive tasks.

d) Improved accuracy: By automating tasks, shell scripts can help reduce the chance of human error, leading to improved accuracy and reliability.

e) Portability: Shell scripts can be run on various operating systems, including Linux, Unix, and macOS, making it a portable scripting language.

f) Integration with other tools: Shell scripts can be integrated with other tools and applications, providing the ability to create complex workflows and automations.

g) Cost-effective: Shell scripting is a cost-effective method of automation, as it does not require specialized software or hardware to run.


Years of Evolution & Types of Shell Scripting

1971: The Unix operating system was introduced, which included the first shell, called the Thompson shell.

1979:The Bourne shell, sh, was introduced as the standard shell for Unix, and became widely adopted as the default shell for Unix-based operating systems.

1987:The C shell, csh, was introduced as an alternative to the Bourne shell, and was designed to have a more C-like syntax.

1989:The Korn shell, ksh, was introduced, which combined features of the Bourne and C shells and added new features such as arrays and job control.

1991:The bash shell, short for Bourne-Again shell, was introduced as a free software replacement for the original Bourne shell. It became the default shell for many Linux distributions and is still widely used today.

1995: The Z shell, zsh, was introduced, which added new features such as advanced auto-completion, spelling correction, and customizable prompts.

In the years since, shell scripting has continued to evolve and improve, with the introduction of new shells and the addition of new features and capabilities.

Today, shell scripting is an essential tool for system administrators, developers, and data scientists, and is used in a wide range of applications and industries.


Shell Scripting Basics

A shell script is a plain text file that contains a series of shell commands, functions, and logic that can be executed in a shell environment. Here's the basic syntax and structure of a shell script:

#!/bin/bash
# Comment describing the purpose of the script
# Command 1
# Command 2
# Command 3
# ...

The first line of the script, #!/bin/bash, specifies the shell interpreter to be used for executing the script. The rest of the script consists of comments, preceded by the # symbol, and commands to be executed by the shell.

Each command in the script is executed in sequence, one after the other, until the end of the script is reached. A command can be a built-in shell command, a command from a Unix-based system, or a custom function or script.

In addition to commands, shell scripts can also include logic, such as conditional statements and loops, to control the flow of execution and make decisions based on the results of commands.

To make a shell script executable, it must have the executable permission set using the chmod command. Once the script is executable, it can be run by calling the script file in the terminal or by using the ./ operator.

Let's learn a bit about bash Scripting

Bash (AKA Bourne Again Shell) is a type of interpreter that processes shell commands. A shell interpreter takes commands in plain text format and calls Operating System services to do something. For example, the ls command lists the files and folders in a directory.

A shell scripting is writing a program for the shell to execute and a shell script is a file or program that the shell will execute.

Bash (bash) is one of many available (yet the most commonly used) Unix shells. and is a replacement/improvement of the original Bourne shell (sh).

Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash.

In practice, however, "shell script" and "bash script" are often used interchangeably, unless the shell in question is not Bash.


echo

The echo command in shell scripting is used to display text or the value of variables on the screen. It is a simple way to print output in a shell script.

Writing 1st script

#!/bin/bash
echo "Happy Learning"

To execute a shell script in Unix-like operating systems (such as Linux), you typically use ./ followed by the name of the script.

We see we do not have the permissions to execute the script. Time to add the necessary permission.

We executed our first shell script.

read

The read command in shell scripting is used to read input from the user and store it in a variable. The input can be entered by the user through the keyboard or redirected from a file.

#! /bin/bash
echo "Enter your name"
read username
echo "Welcome $username"


Common operations in Shell Scripting

Variables

In shell scripting, variables are used to store data values. They are essential for storing information that can be reused throughout the script.

#!/bin/bash
myVar = "Hello People. Smile"
#To access the value of a variable, you prepend a dollar sign $ to the variable name.
echo "$myVar"

It's generally a good practice to quote variables to prevent word splitting and pathname expansion. This is particularly important when dealing with user input or filenames that contain spaces.

For example:

echo "$myVar"

Variable Types:

  • Local Variables: These are variables defined within a shell script and are not available outside the script.

  • Environment Variables: These are variables that are available to any child process of the shell. You can set them using export.

  • Special Variables: These are predefined variables that hold special values. For example, $0 holds the name of the shell script, $1, $2, etc., hold the positional parameters, and $# holds the number of positional parameters.

String

In shell scripting, strings are sequences of characters that are used to represent text. Strings can be assigned to variables, passed as arguments to commands, and used in various ways throughout a script.

Some operations with String

a) Concatenation

#!/bin/bash
var1="Sujan"
var2="Magar"
concat="$var1 $var2"
echo "$concat"

The ${string/pattern/replacement} syntax you provided is used in bash to replace the first occurrence of pattern in string with replacement. If pattern is not found in string, string remains unchanged.

To replace all occurrences of pattern, you can use ${string//pattern/replacement}.

#!/bin/bash
name="Zayn Javadd  Malik"
name1="Zayn Javadd Malik. Very famous Javadd singer of England"

echo "Yo man ta mero Nepali ho - ${name/Malik/Nepali}"

#If you want all replacements to be reflected in the final output, you should apply them sequentially to the same variable
var=${name1/Malik/Nepali}
var=${var/England/Nepal}
var=${var//Javadd/Bahadur}

echo "$var"

Command Substitution

Command substitution is a feature in shell scripting that allows the output of a command to be substituted in place of the command itself. In other words, it allows you to capture the output of a command and use it in your script as a string value.

There are two forms of command substitution (or Shell execution) in shell scripting: a) Backquotes (``)

  • This form of command substitution uses backquotes (``) to enclose the command that you want to substitute. The output of the command will be the result of the substitution. For example:
#!/bin/bash 
result=`ls -la`
echo "$result"

This will run the ls -l command and capture its output, which will then be stored in the result variable.

b) Dollar Sign and Parentheses ($())

  • This form of command substitution is similar to backquotes, but uses a dollar sign and parentheses instead. The syntax is $(command).

  • For example:

      #!/bin/bash 
      result=$(ls -l) 
      echo "$result"
    
  • This will run the ls -l command and capture its output, which will then be stored in the result variable.

Both forms of command substitution are widely used in shell scripts to capture the output of commands and use it for further processing or as input to other commands.

Pipe |

A pipe in shell scripting is a feature that allows you to connect two or more commands together and pass the output of one command as the input to another command. It is represented by the | symbol.

#!/bin/bash
echo "Without pipe and grep"
ls -la
echo "With pipe and grep"
ls -la | grep ".jpg"

The command ls -la | grep ".jpg" lists all files in the current directory (including hidden files) using ls -la and then filters the output using grep to show only the lines that contain the string ".jpg". This will display a list of files in the current directory that have a ".jpg" extension.

Conditional statements

Conditional statements are a fundamental part of shell scripting and allow you to control the flow of your scripts based on the outcome of certain conditions. The most commonly used conditional statements in shell scripting are if, if-else, and if-elif-else.

#!/bin/bash
echo "Enter a number to check if it is greater or smaller than 10"
read num
if [ $num -gt 10 ]; then
    echo "The number is greater than 10"
elif [ $num -eq 10 ]; then
    echo "The number is equal to 10"
else
    echo "The number is less than 10"
fi

#!/bin/bash
echo "Enter a number to check odd/even"
read num
if [ $((num%2)) -eq 0 ]; then
    echo "It is even number"
else
    echo "It is odd number"
fi

Loop

Loops are a key aspect of shell scripting and allow you to repeat a set of commands multiple times based on certain conditions. The two most commonly used loops in shell scripting are for loops and while loops.

#Basic for-loop demo
#!/bin/bash
for i in 1 2 3 4 5; do
    echo $i
done

#Ranges demo
#!/bin/bash
for i in {1..10};do
    echo $i
done

#ranges with step size
#!/bin/bash
for i in {1..10..2};do
    echo $i
done

#while-loop demo
#!/bin/bash
num=1
while [ $num -le 10 ]
do 
    echo $num
    num=$((num+1))
done

#read lines
#!/bin/bash
cat file.txt | while read line; do
echo $line
done

Functions

Functions are a way to organize and reuse code in shell scripting. A function is a self-contained block of code that can be called from other parts of your script. Functions can take arguments and return values, which allows you to pass data to and from the function.

#!/bin/bash
hello(){
echo "Hello World"
}
#calling function
hello

In shell scripting, $1, $2, $3, and so on, are used to denote the first, second, third, and subsequent arguments passed to a shell script or a function within the script.

#!/bin/bash
add(){
        store=$(($1+$2))
        echo "$store"
}
sum=$(add 2 3)
echo "$sum"