#Day 5: Don't fear the Shell, Shell Scripting

What is Kernel?

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.

What is Shell

A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from a user and converts them into something that the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.

What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

What is Shell Scripting for DevOps.

Shell scripting for DevOps is like creating a set of instructions that a computer can follow to automate tasks related to managing software applications and infrastructure. Imagine you have a magical notebook where you write down steps for the computer to do, and it does them automatically whenever you need

In DevOps, which is about combining software development with IT operations, there are many repetitive tasks like deploying new code, configuring servers, or monitoring systems. Instead of doing these tasks manually every time, you can write a shell script, which is a series of commands written in a special programming language called a shell language (like Bash for Unix/Linux systems), to automate these tasks.

For example, if you want to deploy a new version of your software, instead of copying files one by one and restarting services manually, you can write a shell script that does all of that for you with just a single command. This saves time, reduces errors, and makes the whole process more efficient.

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

/#!/bin/bash

The line #!/bin/bash is called a shebang. It's a special directive at the beginning of a script file in the Linux operating system which interpreter to use to execute the script. In this case #!/bin/bash specifies that the script should be interpreted and executed by the bash shell, which is a commonly used shell on Unix-like systems.

#!/bin/sh

Yes, we can also use #!/bin/sh This specifies that the script should be interpreted and executed by the system default shell, which is often a ("Portable Operating System Interface for Unix") POSIX-compliant shell like bash.

What to chose

If you only need features that are available in the POSIX standard and you want your script to be more portable across different Unix-like systems, you might choose #!/bin/sh to ensure compatibility with the basic shell features provided by POSIX. However, if your script requires specific features or functionalities provided by Bash or any other shell, you should specify that shell in the shebang line (#!/bin/bash, #!/bin/zsh, etc.).

Basics of Shell Scripting.

  • Shebang: The first line of a shell script starts with #! followed by the path to the shell interpreter (#!/bin/bash). It specifies which shell should interpret the script.

  • Comments: Comments in shell scripts begin with #. They are used for documentation and explanations within the script.

  • Variables: Variables are used to store data or values. They can be defined using variable_name=value syntax. Variables can hold strings, numbers, or other data types.

  • Quoting: Quoting is used to preserve spaces, and special characters, and prevent word splitting. Single quotes (') preserve the literal value of each character enclosed within them, while double quotes (") allow variable expansion and command substitution.

  • Commands: Shell scripts can contain various shell commands, such as echo, ls, cd, grep, awk, sed, etc., to perform specific tasks.

  • Control Structures: Shell scripts support control structures like loops (for, while), conditional statements (if-then-else), and case statements (case).

4. Execution Permissions:

  • Before executing a shell script, you need to ensure that it has execution permissions (chmod +x script.sh). Or (chmod 455 script.sh) Otherwise, you won't be able to run the script.

5. Running Shell Scripts:

  • Shell scripts can be executed by typing the path to the script file (./script.sh) or by specifying the shell interpreter explicitly (bash script.sh).

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge.

#!/bin/bash 

#with echo command we can print the content on the terminal
echo "I will complete #90DaysOofDevOps challenge."

Write a Shell Script to take user input, input from arguments, and print the variables.

Let's create a script that takes your name and last name as input and prints it together.

Write an Example of If else in Shell Scripting by comparing 2 numbers.

Ok!! Let's compare the input with 100, If it's greater than 100 return no is big else no is small.

Quick Notes -

read Command:

  • The read command in shell scripting is used to read input from the user and store it into one or more variables.

  • Syntax: read variable_name

    Example:

      bashCopy codeecho "Enter your name:"
      read name
      echo "Hello, $name!"
    

$ (Variable Expansion):

  • The $ symbol is used for variable expansion in shell scripting. It retrieves the value stored in a variable.

"Thank you for joining me on this journey into DevOps! Your curiosity is the driving force behind this exploration. If you found value in these insights, I'm thrilled! Keep the DevOps flame alive, and stay tuned for more exciting adventures in the world of technology. Happy Learning! 🚀✨"

"Closing the gap between us—let's connect and make our virtual bond stronger on social media!

Click on -Umesh Salunkhe