Learn Linux Shell Scripting from Scratch

Learn Linux Shell Scripting from Scratch

In the world of computer programming, shell scripting is a powerful tool that enables developers to automate tasks and make their work more efficient. Shell scripting is a scripting language that is used to create a series of commands that can be executed in sequence by a command-line interpreter.

In this article, we will explore what shell scripting is, how it works, and some practical applications.

Shell Script ----> Script ----> Shell -----> Kernel -----> Execution of task.

What is Script in Linux?

In Linux, a script refers to a file containing a series of commands that can be executed by the operating system's command interpreter, or shell.

What is Shell?

Shell is a special program that provides a command-line interface (CLI)/ terminal so that users can interact with the operating system through special text commands. A shell allows users to run commands, execute programs, and manipulate files and directories on the system.

What is Kernel?

A kernel is a central component of an operating system that manages system resources and provides a bridge between software applications and the computer's hardware. The kernel allows programs and users to interact with the computer's resources such as the CPU, memory, disk drives, and input/output devices.

What is Shell Scripting?

Shell scripting is the process of writing a series of commands for a shell interpreter to execute. Shell scripts are essentially small programs that automate tasks such as launching applications, managing files, and manipulating system settings.

How Does Shell Scripting Work?

Shell scripts are written in a language that is specific to the shell interpreter being used. We can use different scripting languages such as Bash, Zsh, Csh, Ksh, Fish, etc. The most commonly used shell interpreter is the Bourne-Again SHell (BASH), which is the default shell for most Linux and Unix distributions.

A shell script consists of a series of commands that are executed in sequence. The script can also contain variables, loops, conditionals, and other programming constructs to make it more powerful and flexible. When a shell script is executed, the shell interpreter reads the commands and executes them one by one.

Practical Applications of Shell Scripting

Shell scripting has many practical applications, including:

  1. System Administration: Shell scripts can be used to automate system administration tasks, such as backing up data, creating user accounts, and configuring system settings.

  2. Web Development: Shell scripts can be used to automate tasks related to web development, such as deploying web applications and managing servers.

  3. Data Processing: Shell scripts can be used to automate data processing tasks, such as sorting and filtering large data sets.

  4. DevOps: Shell scripts are an essential tool for DevOps engineers who need to automate the deployment and configuration of software systems.

Let's start with our first Linux shell script--->

  1. Create a text file with .sh extension to make it a shell script.

    vim sample.sh

    #!/bin/bash-----------> This is called the 'Shebang' line which is written first in the script. This line specifies the name of the shell interpreter that is going to be used to execute this shell.

    echo--------> Is a keyword to print anything into terminal during script execution.

    Now press Esc + :wq to save and quit from the text editor.

  2. Make this script executable by changing its file permissions.

    You can see after making this file executable by the user that; the file name fonts are 'Green' means this file can be executed.

  3. Now execute our script by typing ./sample.sh and hit enter.

    • ./ followed by name of the script will execute the script.

    • bash + name of the script will also execute it because the bash interpreter can execute this bash script.

This is how simple it is to write a shell script!

Now let's understand how to use variables, accept inputs and arguments from the user--->

  • sleep 3 -----> Pauses script execution for 3 seconds.

  • read ------> Accepts input from the user during execution and stores it into a variable name in this case.

  • $name or ${name} ---> This $ + variable name is used to call the variable.

  • $1 -----> $ + Number is used to call the argument provided by the user at the time of running the script. You can pass n number of arguments and call them starting with 1, 2, 3.....as per their order.

  • You may have noticed how the argument "Contributor" is passed at the time of running the script.

How to use conditional expressions in a shell script?

  1. Use if_else condition

    Syntax:

    if [ condition ]

    then

    commands

    else

    commands

    fi

  2. Use for_loop condition

    Syntax:

    for ( condition )

    do

    commands

    commands

    done

    [For loop will run until the condition is true]

  3. Use while_loop condition

    Syntax:

    while ( condition )

    do

    commands

    commands

    done

    [While loop will run until the condition becomes false]

    [Anything written after '#' is a comment are it will not be executed during runtime]

  4. Use switch_case conditions

    Syntax:

    case $expression in

    pattern1)

    command

    ;;

    pattern2)

    command

    ;;

    pattern..n)

    command

    ;;

    *)

    command

    ;;

    esac

    [Given expression will be matched with each pattern and after the match, the respective command will be executed. if no match then last case *) will be executed.]

    [ech0 -e : where -e will allow escape sequences to work inside string]

    [ Convert the user input string to uppercase

    word=${word^^} ]

    [ Convert the user input string to lowercase

    word=${word,,} ]

Let's write a shell script that checks the count of shell scripts present inside the current directory.

You can also write functions inside the shell script and that will work fine as you do in other scripting languages like python, perl and ruby.

Click here to learn more about Linux Shell Scripting

Summary:

In this blog, Shell scripting is a powerful tool for automating tasks and making work more efficient. Shell scripting has many practical applications, including system administration, web development, data processing, and DevOps. If you are a DevOps guy, learning shell scripting can be an invaluable skill that can help you automate tasks and streamline your work.

Stay tuned for my next blog on "Shell Scripting for DevOps". I will keep sharing my learnings and knowledge here with you.

Let's learn together! I appreciate any comments or suggestions you may have to improve my Linux blog.

Thank you,

Chaitannyaa Gaikwad

Did you find this article valuable?

Support Chaitannyaa Gaikwad by becoming a sponsor. Any amount is appreciated!