Basics of Shell Scripting:

Basics of Shell Scripting:

Introduction:

This is Part 2 of my Linux series, Here I will explain the basics of shell script programming or shell scripting, and hopefully introduce some of the possibilities of simple but powerful programming available under the Bash shell. But before proceeding directly to Shell Script, let's dig more into Shell.

Shell:

  • Shell, is an interpretive environment within which you execute commands. It provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program’s output.

  • When you run the terminal, the Shell issues a command prompt (usually $), where you can type your input,(commands) which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.

  • The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage. Hence the name Shell.

Architecture of Linux

Types of Shell:

There are two categories of Shell and they have different sub-categories:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:

  • POSIX shell also is known as 'sh'

  • Korn Shell also known as 'ksh'

  • Bourne Again SHell also known as 'bash' (most popular)

2. The C shell: The prompt for this shell is %, and its subcategories are:

  • C shell also is known as 'csh'

  • Tops C shell also is known as 'tcsh'

What is Shell Scripting?

  • Shell Script is a file consisting of lists of commands executed sequentially. It helps in automating the day-to-day tasks of DevOps Engineer/Linux Admin and saves time. For example, if you want to schedule a backup of your file/logs then with the help of a shell script it can be automated to take a backup of the desired files at a specified time and there will be no manual intervention.

  • Shell Scripting provides automation, makes repetitive tasks, system monitoring easier to perform. It is easier to get started with it. System admins use shell scripting for routine backups and various other tasks.

Steps to writing Shell Scripts:

  1. Create a file using a vi/vim/nano editor with extension .sh

    • File Names can be max of 255 characters with includes underscore, dot, numbers, alphabets, etc.

    • File Names are case-sensitive.

    • You cannot use reserved keywords.

  2. Start the script with #! /bin/sh (Shebang along with absolute path to the bash interpreter)

  3. Write some code.

  4. Save the script file as: filename.sh

  5. Provide execute permission: chmod u+x filename.sh (Follow Part 1 for more detail)

  6. For executing the script type: bash filename.sh or ./filename.sh or sh filename.sh

Adding Comment(s) to the Shell Script:

  • This part of the code will be ignored by Interpreter.

  • These are mainly used to add information to the code so it will be easy to read and understand if required to make changes in the future.

  • These can be of two types:

    • Single Line Comment:

      - Represented by: hash (#)

    • Multi-Line Comment:

      - Represented by: <<<COMMENT

      Add your multi-line

      comments here

      COMMENT

How to Print anything in Shell Script:

1.1: Printing directly using echo

For printing anything over the shell script we use echo "....."

There are mainly two ways to use the Shell:

  • Interactive Shell

  • UnInteractive Shell

Interactive Shell:

The Bash shell is commonly used interactively: It lets you enter and edit commands, then executes them when you press the Return/Enter key. Many Unix-based and Unix-like operating systems use Bash as their default shell (notably Linux and macOS). The terminal automatically enters an interactive Bash shell process on startup. Output Hello World by typing the following:

echo "Hello Devops Engineers"

Non- Interactive Shell:

The Bash shell can also be run non-interactively from a script, making the shell require no human interaction. Interactive behavior and scripted behavior should be identical – an important design consideration of Unix V7 Bourne shell and transitively Bash. Therefore anything that can be done at the command line can be put in a script file for reuse. Follow these steps to create a Hello World script ( we have created the same in the previous AWS Blog 1 or you can refer below)

Let's revise how to create Shell Script🤞:

  1. Create and edit a file:

     vi <Scriptname.sh>
    

    (Hint: refer to the below image for the script part)

  2. Provide execute permission to the shell script:

     chmod u+x <Scriptname.sh>
    

    (Hint: Scroll down there is a detailed explanation on this under File Permission section)

  3. Execute the script (you can use any of the below syntax)

'ScriptName.sh>

sh <ScriptName.sh>

Hint: Below is the output of the script

Script1.sh

Output of the Script1.sh

There are few of the built in commands like alias, bind, unalias, builtin, caller, command, declare, echo, enable, help, logout, local, prinf, mapfile, red, source, type, typeset ulimit etc.

Read more here

Heading directly on hands-on:

1.2: Printing Output using a variable:

  • Create a new file called "hello_var.sh" with the following content and give it executable permissions.
touch hello_var.sh
chmod u+x hello_var.sh
  • Add the below entries in the file:
ubuntu@ip-172-31-93-20:~$ cat hello_var.sh
#! /bin/bash

#Defining the variable:
var1=DevOps_Engineers

echo "Hello $var1"
  • Run the script:
./hello_var.sh
  • Below is the output:

1.3: Printing Output with User Input:

  • Create a new file called "hello_user_input.sh" with the following content and give it executable permissions.
touch hello_user_input.sh
chmod u+x hello_user_input.sh
  • Add the below entries in the script file:
#! /bin/bash

echo "Enter your Name: "
read name

echo ""Hello $name, You are on your journey of becoming DevOps Engineer!!"
  • Run the script file:
./hello_user_input.sh
  • Below is the output:

1.4: Importance of Quoting in Strings:

Quoting is important for string expansion in bash. With these, you can control how the bash parses and expands your strings.

There are two types of quotes:

  • Weak: uses double quotes: "

  • Strong: uses single quotes: '

    If you want to bash to expand your argument, you can use Weak Quoting:

#!/usr/bin/env bash
world="World"
echo "Hello $world"
#> Hello World

If you don't want to bash to expand your argument, you can use Strong Quoting:

#!/usr/bin/env bash
world="World"
echo 'Hello $world'
#> Hello $world

You can also use escape to prevent expansion:

#!/usr/bin/env bash
world="World"
echo "Hello \$world"
#> Hello $world

1.5: Viewing information for Bash built-ins:

This will display the Bash help (manual) page for the specified built-in.

help <command>

To see a list of all built-ins with a short description, use below

help -d

1.6: Defining Variables:

  1. Variable names must start with a letter or underscore.

  2. Numbers can be used anywhere else.

  3. Do not use special characters such as @,#,%,$

  4. Case Sensitive.

  5. Allowed: VARIABLE, VAR1234able, var_name, VAR.

  6. Not allowed: 1var, %name, $myvar, var@NAME, myvar-1 .

  7. To reference a variable, prepend $ to the name of the variable

    Example: $PATH, $LD_LIBRARY_PATH, $myvar etc.

1.7: Handling Named Arguments:

#!/bin/bash
create=false
destroy=false
while (( $# > 1 )); do case $1 in
 --create) create="$2";;
 --destroy) destroy="$2";;
 *) break;
esac; shift 2
done
$create && echo "will create... create = $create"
$destroy && echo "will destroy... destroy = $destroy"
# how to run
# chmod +x script.sh
# ./script.sh --deploy true --uglify false

Navigating Directories:

2.1: Moving with Absolute Directory:

  • We use the absolute path when we specify the path to a file or folder, starting with the root directory.

  • Path begins with the root (/), followed by the separators to the other directories in the middle before you reach the target file.

  • Use "pwd" to find the absolute path of the current file/folder

Example: To change to an absolutely specified directory, use the entire name, starting with a slash /, thus:

cd /home/username/directory/abc

2.2: Moving with Relative Directory Path:

  • Unlike the absolute path relative paths start from the present working directory. Thus, the relative path changes depending on your current directory.

  • The target folder is in the current directory, meaning we can directly reference it without needing to add the root directory.

  • The relative path allows using single dot (.) and double dot (..) to specify paths forward or backward in the current directory. These dots are not visible unless you use them to access the target file

      cd ..
    

    Example: You are at username location and you want to goto abc location which is inside directory folder. So insteadof giving whole path as

      cd /home/username/directory/abc
    

    we can directly use:

      cd directory/abc
    

    which is a relative path.

2.3: Change to the Home Directory:

  • The default directory is the home directory ($HOME, typically /home/username), so cd without any directory takes you there

      cd
    

    Or you could be more explicit:

      cd $HOME
    
  • A shortcut for the home directory is ~, so that could be used as well.

      cd ~
    

2.4: Change to the Directory of the Script

There are two types of Bash scripts, i.e..,

  1. System tools that operate from the current working directory

  2. Project tools that modify files relative to their place in the files system For the second type of script, it is useful to change to the directory where the script is stored. This can be done with the following command:

     cd "$(dirname "$(readlink -f "$0")")"
    

    This has 3 commands in it:

    • readlink -f "$0": determines the path to the current script ($0)

    • dirname: converts the path to script to the path to its directory

    • cd: changes the current work directory to the directory it receives from dirname.

Thanks for reading till the end, I hope you got some knowledge. Here is a BONUS to make you smile #Linux_Memes:

Meme Overflow on Twitter: "Delete all files except the newest 3 in bash  script https://t.co/TlMnZfxJZ0 #bash #ls #purge #linux  https://t.co/2Rh6n4WUii" / Twitter

If you like my work, Let's connect and collaborate😃. I am available on the below platforms and very much active there:

Linkedin
GitHub
Blogs

Did you find this article valuable?

Support Varsha Verma by becoming a sponsor. Any amount is appreciated!