Skip to main content

Command Palette

Search for a command to run...

Day 4 - Basic Linux Shell Scripting

Published
5 min readView as Markdown
Day 4 - Basic Linux Shell Scripting
T

Hey there! 👋

I'm Tushar Ranjan, a DevOps Engineer passionate. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps.

🛠️ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space.

🌐 Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!

What is Kernel?

The kernel is the central part of a computer's operating system, acting as the core that has full control over everything in the system. It is responsible for:

  • Managing Hardware Resources: It controls the CPU, memory, and input/output devices.

  • Providing Essential Services: These include process scheduling, memory management, and device drivers.

What is Shell?

A shell is a command-line interface that allows users to interact with an operating system. It takes human-readable commands from users and translates them into instructions the kernel can understand.

The most common shell used in Linux and Unix-based operating systems is the Bourne-Again Shell (bash). Bash is a free software command-line interface designed for Unix-like operating systems.

There are several types of shells, including:

  • Bourne Again Shell (bash): The most widely used shell in Linux and Unix systems.

  • C Shell (csh): Known for its C-like syntax, often used for scripting.

  • Korn Shell (ksh): Combines features of the Bourne Shell and the C Shell, suitable for both scripting and interactive use.

  • Z Shell (zsh): Offers advanced features like improved auto-completion, spell checking, and more customizable options.

Each shell type has its own set of features, syntax, and capabilities. Some shells are better suited for specific tasks, while others provide advanced features for power users.

What is Terminal?

The terminal is a program that provides a text-based interface for users to interact with the operating system. It allows users to enter commands and view their output directly. Through the terminal, users can run large scripts to automate and perform complex tasks efficiently. Essentially, the terminal serves as a gateway to access the shell, enabling powerful command-line operations and automation.

What is a Shell Script?

A shell script, also known as a bash script, is a file containing a series of commands that are executed by the bash program line by line. These scripts are typically used to automate repetitive tasks, making system administration and development processes more efficient.

Shell scripts end with a .sh extension.

However, you can identify a shell script with a "Shebang". Shebang is a combination of bash(#) and bang(!) followed with the bash shell path i.e., #!/bin/bash. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

For executing, shell script you can write:

  1. sh filename.sh

  2. ./filename.sh

#!/bin/bash and #!/bin/sh

So, You may find any of these two in shell scripts. Let's see if they are similar or what is the difference.

bash and sh are two different shells. Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different.

A script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell.

#!/bin/sh is a symbolic link to #!/bin/bash, which means it executes the script using the Bourne shell or a compatible shell, with path /bin/sh.

First Shell Program

Creating and Running a Shell Script

  • Description: A shell script is a file containing a series of commands.

  • Example:

    1. Create a script file:

       ubuntu@ip-172-31-10-117:~/test$ vi first_script.sh
      
    2. Add the following content:

       #!/bin/bash
       echo "Hello, World!"
      
    3. Save and close the file.

    4. Make the script executable:

       ubuntu@ip-172-31-10-117:~/test$ chmod +x first_script.sh
      
    5. Run the script:

       ubuntu@ip-172-31-10-117:~/test$ ./first_script.sh
      

Output:

    Hello, World!

User Input Using read Command

read Command

  • Description: Waits for user input and stores it in a variable.

  • Examples:

    • read variable: Waits for user input and stores it in variable.

        ubuntu@ip-172-31-10-117:~/test$ cat first_script.sh
        echo "Enter your name:"
        read name
        echo "Hello, $name!"
      
    • read -p "Message" variable: Displays a message and waits for input.

        ubuntu@ip-172-31-10-117:~/test$ cat first_script.sh
        read -p "Enter your age: " age
        echo "You are $age years old."
      
    • read -s -p "Password: " password: Reads input without displaying it on the screen.

        ubuntu@ip-172-31-10-117:~/test$ cat first_script.sh
        bashCopy coderead -s -p "Password: " password
        echo "Your password is $password"
      

Shell Script with Input Arguments

Using Arguments in Scripts

  • Description: Access arguments passed to the script using $1, $2, etc.

  • Example:

    1. Create a script file:

       ubuntu@ip-172-31-10-117:~/test$ vi script_with_args.sh
      
    2. Add the following content:

       ubuntu@ip-172-31-10-117:~/test$ vi script_with_args.sh
       #!/bin/bash
       echo "First argument: $1"
       echo "Second argument: $2"
       echo "Script name: $0"
      
    3. Save and close the file.

    4. Make the script executable:

       ubuntu@ip-172-31-10-117:~/test$ chmod +x script_with_args.sh
      
    5. Run the script with arguments:

       ubuntu@ip-172-31-10-117:~/test$ ./script_with_args.sh arg1 arg2
      

Output:

    First argument: arg1
    Second argument: arg2
    Script name: ./script_with_args.sh

Using Variables in Shell Script

Variables in Shell Scripts

  • Description: Variables store data that can be used later in the script.

  • Example:

      bashCopy code#!/bin/bash
      name="Tushar"
      echo "My name is $name."
    

If-Else in Shell Scripting for Comparing Two Numbers

If-Else Statement

  • Description: Conditional statements to perform actions based on conditions.

  • Example:

      ubuntu@ip-172-31-10-117:~/test$ vi script.sh
      #!/bin/bash
      read -p "Enter first number: " num1
      read -p "Enter second number: " num2
    
      if [ "$num1" -gt "$num2" ]; then
        echo "$num1 is greater than $num2"
      elif [ "$num1" -lt "$num2" ]; then
        echo "$num1 is less than $num2"
      else
        echo "$num1 is equal to $num2"
      fi
    

For Loop in Shell Scripting

For Loop

  • Description: Repeats a series of commands for each item in a list.

  • Example:

      ubuntu@ip-172-31-10-117:~/test$ vi script.sh
      #!/bin/bash
      for i in 1 2 3 4 5
      do
        echo "Number: $i"
      done
    

Conclusion

Mastering basic Linux shell scripting is crucial for DevOps engineers. Shell scripting enables the automation of repetitive tasks, efficient management of system resources, and effective monitoring of system performance. These capabilities help DevOps professionals save valuable time and boost productivity.

With regular practice, anyone can become proficient in Linux shell scripting, significantly enhancing their DevOps skills and making their workflows more efficient and reliable.

~ Tushar Ranjan🙂

More from this blog

Tushar Ranjan's blog

72 posts