Basic Linux Shell Scripting for DevOps Engineers

Basic Linux Shell Scripting for DevOps Engineers

What is Shell Scripting?

Shell Scripting is a form of computer programming that entails writing scripts or sequences of commands within a shell, a command-line interface used to interact with an operating system. Shell scripts can accomplish various tasks, such as creating and managing files and directories, executing system commands, manipulating data, implementing conditionals and loops, processing user input, and more.

What is #!/bin/bash? Can we write #!/bin/sh as well?

The #!/bin/bash is called a shebang or hashbang. It's a special instruction at the beginning of a script file that tells the operating system which interpreter to use for executing the script. In this case, it specifies that the script should be executed using the Bash shell.

bash (Bourne-Again SHell) and sh (Bourne SHell) are two different shells. Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different.

We can also use #!/bin/sh in a script file to specify that it should be executed using the default shell interpreter. In this case, it specifies the Bourne shell (/bin/sh), which is a popular Unix shell. However, the behavior of the script may vary depending on the system and the shell environment.

It's important to note that different shells have different features, syntax, and commands, so it's best to use the shebang that corresponds to the shell features and commands that your script requires.

How to Write a Shell Script Program?

Here are some steps to follow when creating a shell script:

  1. Create a new file: Create a new file using any editor of your choice and save it with a ".sh" extension, indicating that it is a shell script.

  2. Add shebang line: Add a shebang line at the beginning of the script, which tells the system which interpreter to use to run the script. For Bash, the shebang line is "#!/bin/bash".

  3. Write the script: Write the script, which consists of a sequence of commands that you want to execute. You can use any shell command, including built-in commands, system commands, and other scripts.

  4. Save the script: Save the script and exit the text editor.

  5. Make the script executable: Make the script executable by running the command "chmod +x scriptname.sh". This allows you to run the script as a program.

  6. Run the script: Run the script by typing "./scriptname.sh" in the command prompt, where "scriptname.sh" is the name of your script file.

Write a shell script that prints Hello world

#!/bin/bash

echo "Hello World"
  1. #!/bin/bash: This is the shebang line, specifying that the script should be executed using the Bash shell.

  2. echo "Hello World": The echo command prints the text within the quotes, "Hello World", to the console

1) - Write a shell script that takes user input, input from arguments, and displays the variables.

#!/bin/bash

# Taking input from arguments
arg1=$1
arg2=$2

# Taking user input
read -p "Enter value 1: " user_input1
read -p "Enter value 2: " user_input2

# Displaying the variables
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"
echo "User Input 1: $user_input1"
echo "User Input 2: $user_input2"
  1. #!/bin/bash: This is the shebang line, specifying that the script should be executed using the Bash shell.

  2. arg1=$1 and arg2=$2: These lines store the first and second command-line arguments in the variables arg1 and arg2.

  3. read -p "Enter value 1: " user_input1 and read -p "Enter value 2: " user_input2: These lines prompt the user to enter two values, which are stored in the variables user_input1 and user_input2.

  4. The echo commands display the values of the variables arg1, arg2, user_input1, and user_input2.

  5. To provide arguments to a shell script, you can pass them directly on the command line after the script name. When executing the script, use the format ./scriptname.sh arg1_value arg2_value, where arg1_value and arg2_value are the values you want to pass as arguments. These values will be accessible in the script as $1 and $2 respectively.

     ./scriptname.sh arg1_value arg2_value
    

2) - Example of "If-Else" and "If-Elif" in Shell Scripting

#!/bin/bash

number=5

if [[ $number -lt 0 ]]; then
    echo "Number is negative"
elif [[ $number -eq 0 ]]; then
    echo "Number is zero"
else
    echo "Number is positive"
fi

In shell scripts, the "if, else if (elif), else" statement has the following structure:

  1. if [[ condition ]]: This evaluates the first condition. If true, the commands after the then keyword execute.

  2. then: This keyword indicates the start of the commands to execute if the first condition is true.

  3. <commands>: The commands executed when the first condition is true.

  4. elif [[ condition ]]: This evaluates thesecond condition if the first condition is false. If true, the commands after the then keyword execute.

  5. then: This keyword indicates the start of the commands to execute if the second condition is true.

  6. <commands>: The commands executed when the second condition is true.

  7. else: This keyword indicates the start of the commands to execute if all conditions are false.

  8. <commands>: The commands executed when all conditions are false.

  9. fi: This keyword ends the "if, else if, else" statement.

Summary

In conclusion, shell scripting is a powerful tool for automating tasks and managing systems. By understanding the basics of shell scripting, including shebang lines, user input, command-line arguments, and conditional statements, you can create efficient and versatile scripts to streamline your work and improve productivity.