What & why Shell Scripting?

What & why Shell Scripting?

When administrating the server. we need to execute lots of commands. Instead of executing lots of commands one by one. we can save the commands in a file and execute that file. Such a file is called a Shell script. The extension of this file would be (.sh).

In a nutshell, we can say "Shell scripting is the process of writing a sequence of commands for a shell to execute".

The shebang is the character "#!" that appears at the beginning of a script in Unix-like operating systems. It is followed by the path to the interpreter that should be used to execute the script. For example, #!/bin/bash indicates that the script should be executed using the Bash shell interpreter located at /bin/bash.

Day 4 - Basic Linux Shell Scripting for DevOps Engineers.

What is a Shell interpreter and their type?

✔️Shell interpreter is equal to the Shell.

✔️Types of Shell interpreters:👇

  1. Bourne Shell (sh): The Bourne Shell, named after its developer Stephen Bourne, is one of the original Unix shells. It is lightweight and efficient but lacks some of the features found in more modern shells.

  2. Bash (Bourne Again Shell): Bash is an enhanced version of the Bourne Shell and is the default shell for most Linux distributions and macOS. It includes additional features such as command-line editing, history, and job control.

  3. C Shell (csh)

  4. Korn Shell (ksh)

  5. Z Shell (zsh)

  6. Fish Shell: Fish (Friendly Interactive Shell).

Each shell interpreter has its own syntax, features, and capabilities, but they all serve the same fundamental purpose of providing a command-line interface for interacting with the operating system.

Location of Shell interpreter in Linux:-

The shell interpreters are executable files located in directories. In Linux, everything is considered a file, including directories. So, when we refer to the location of a shell interpreter like /bin/bash, we're specifying the path to an executable file named bash located within the /bin directory.

Note that when we say that a shell interpreter is located in a directory, we mean that its executable file is located within that directory.

  1. Bourne Shell (sh):

    • Location: /bin/sh
  2. Bash (Bourne Again Shell):

    • Location: /bin/bash
  3. C Shell (csh):

    • Location: /bin/csh
  4. Korn Shell (ksh):

    • Location: /bin/ksh
  5. Z Shell (zsh):

    • Location: /bin/zsh
  6. Fish Shell:

    • Location: /usr/bin/fish

Note:-Actual locations may vary depending on the distribution and the system's configuration.

We can check the location of a specific shell interpreter on our system by using the which command followed by the name of the shell. For example:-

which bash

Benefits of Shell Scripting?

1)Avoid repetitive work.

2)Keep history in configuration.

3)Share the instructions.

4)Logic and bulk operation.

5)Automate all-over work.

Some examples to start with Shell Scripting:-

  1. Basic Syntax:

    • Basic structure of a shell script.👇

    • Variables and data types.👇

Example:

    #!/bin/bash

    # Declare a variable
    greeting="Hello, World!"

    # Print the variable
    echo $greeting
  1. Control Structures:

    • If statements

    • For loops

    • While loops

Example:

    #!/bin/bash

    # If statement
    if [ $1 -eq 1 ]; then
        echo "One"
    else
        echo "Not One"
    fi

    # For loop
    for i in {1..5}; do
        echo "Number: $i"
    done

    # While loop
    count=0
    while [ $count -lt 5 ]; do
        echo "Count: $count"
        ((count++))
    done
  1. Functions:

    • Declaring and calling functions.

Example:

    #!/bin/bash

    # Define a function
    say_hello() {
        echo "Hello, $1!"
    }

    # Call the function
    say_hello "Alice"
  1. Input/Output:

    • Reading command-line arguments.

    • Reading user input.

    • Redirecting input and output.

Example:

    #!/bin/bash

    # Read command-line arguments
    echo "Argument 1: $1"

    # Read user input
    read -p "Enter your name: " name
    echo "Hello, $name!"

    # Redirecting input and output
    ls > file_list.txt

Nugget-The -p "Enter your age: " option specifies the prompt message to be displayed to the user. When the read command is executed with this option, the prompt message "Enter your age: " is displayed on the same line as the input cursor. This makes the script more user-friendly by providing a clear indication of what input is expected from the user. Also name is a variable that stores the user input (their name) obtained from the read command.When we want to access the value stored in the name variable, we use $name.

  1. Error Handling:

    • Handling errors with exit codes.

    • Trap command for signal handling.

Example:

    #!/bin/bash

    # Error handling
    rm non_existing_file || echo "File does not exist!"

    # Trap command
    trap 'echo "Script interrupted."' INT
    sleep 10
  1. Advanced Topics:

    • Arrays

    • String manipulation

    • Regular expressions

Example:

    #!/bin/bash

    # Arrays
    my_array=("apple" "banana" "cherry")
    echo "First element: ${my_array[0]}"

    # String manipulation
    my_string="Hello, World!"
    echo ${my_string:0:5}

    # Regular expressions
    if [[ "abcdef" =~ ^a.*$ ]]; then
        echo "Matches!"
    fi