A Beginner’s Guide to Shell Scripting

Shell scripting is a powerful way to automate tasks in Linux. With shell scripts, you can write a set of instructions once and run them as many times as needed. This guide will introduce you to the basics of shell scripting using the most popular shell: Bash (Bourne Again Shell).

Getting Started with Shell Scripting

  1. Create a Directory for Your Scripts

    First, let’s create a directory to keep your scripts organized:

     mkdir scripts
    
  2. Navigate to Your Scripts Directory

    Move into the directory you just created:

     cd scripts/
    
  3. Create Your First Shell Script

    Open a new file using Vim (a text editor). We’ll name this script hello.sh:

     vim hello.sh
    

    Add the following content to hello.sh:

     echo "hello raees"
    

    Save and exit Vim by typing :wq and pressing Enter.

  4. Run Your Script

    To see the output of your script, run:

     bash hello.sh
    

Editing and Enhancing Your Script

  1. Create Another Script

    Now, let’s create another script named chatur.sh:

     vim chatur.sh
    

    Add the following content to chatur.sh:

     #! /bin/bash
     echo "hello raees"
     echo "hello rancho"
     echo "ajj date kya hai?"
     echo "today date: $(date)"
     echo "Disk: $(df -h) & RAM: $(free -h)"
    

    Here’s what each command does:

    • $(date): Shows the current date.

    • $(df -h): Displays disk space usage.

    • $(free -h): Displays RAM usage.

Save and exit Vim by typing :wq and pressing Enter.

  1. Make Your Script Executable

    Change the permissions of chatur.sh to make it executable:

     chmod 700 chatur.sh
    

    Now, you can run the script:

     ./chatur.sh
    

Understanding Variables and Input

  1. Create a Script with Variables

    Let’s create a script named raees.sh:

     vim raees.sh
    

    Add the following content to raees.sh:

     #! /bin/bash
     echo "hello"
     echo "today is $(date)"
     echo "RAM is $(free -h)"
     echo "Disk space is $(df -h)"
    
     rank=1
     echo "Rancho ki rank thi $rank"
     echo "chatur ki rank kya thi?"
     read rank
     echo "chatur ki rank thi $rank"
    

    Here’s what each part does:

    • rank=1: Sets a variable named rank with a value of 1.

    • read rank: Prompts the user to enter a value for rank.

Save and exit Vim by typing :wq and pressing Enter.

  1. Make raees.sh Executable

    Change the permissions of raees.sh to make it executable:

     chmod 700 raees.sh
    

    Run the script:

     ./raees.sh
    

Using Arguments

  1. Pass Arguments to Your Script

    You can pass arguments to your script. Modify raees.sh to include:

     #! /bin/bash
     echo "Rancho ki rank thi $3"
     echo "raju ki rank $1"
    

    Save and exit Vim by typing :wq and pressing Enter.

  2. Run Your Script with Arguments

    Pass arguments when running the script:

     ./raees.sh 29 30 1 2
    

    Here:

    • $1 refers to the first argument (29).

    • $3 refers to the third argument (1).

Conclusion

Shell scripting is a powerful tool that can help you automate tasks and manage system operations efficiently. By practicing with simple scripts and understanding how to use commands, variables, and arguments, you can start building more complex scripts tailored to your needs.

Hopefully, this guide gives you a solid start in your shell scripting journey!