Day 4: 90DaysOfChallenge

Day 4: 90DaysOfChallenge

Basic Linux Shell Scripting for DevOps Engineers

  • What is Linux Shell Scripting for DevOps?

Shell scripting for DevOps refers to the practice of writing scripts using shell programming languages (such as Bash, PowerShell, or Python) to automate and streamline various tasks in the DevOps workflow. Shell scripts play a crucial role in the DevOps world as they enable automation, consistency, and efficiency in tasks such as deployment, configuration management, provisioning, monitoring, and more. The most common shell is called the "Bash" shell which comes by default with the computer. Bash stands for "Bourne Again Shell".

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

    The #!/bin/bash is called a "shebang" (also known as a hashbang), and it is a special directive used at the beginning of a script to specify that Bash shell should be used to execute the script. Bash is a popular shell, and it provides powerful features and capabilities for scripting.

    We can use #!/bin/sh instead of #!/bin/bash because the script does not rely on any Bash-specific features.

    #!/bin/sh specifies that the script uses the system's default shell, which is typically the Bourne shell or a compatible shell like Dash. The Bourne shell is simpler and more basic compared to Bash, and it may have fewer features and capabilities.

    However, using #!/bin/bash or #!/bin/sh in the script depends on the specific requirements and compatibility of the target environment. If the script requires a specific feature related to Bash, then it's preferable to use #!/bin/bash to ensure compatibility. But if the script is intended to be more portable and compatible across various Unix-like systems, then #!/bin/sh should be used to stick to the core shell functionality.

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

Firstly, we will create a bash file using nano which is a text editor and store the file name with .sh extension. We give nano test-shellScripts.sh and hit enter. This takes us to the nano editor where we give #!/bin/bash to specify that bash will be used for scripting. Then we use echo command to display the content echo "I will complete #90DaysOfDevOpsChallenge". Then, we save and exit from the editor. We then use bash test-shellScripts.sh to check the content of the file test-shellScripts.sh.

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

    To take user input - read command is used to take input from the user and store it in a variable. The variable is called by using a dollar sign followed by opening curly braces, variable name and then closing burly braces. Ex- ${variableName}.

    In the below example, name is the variable preceded by the read command to take input from the user. The variable is then called in the last line by echo "Hi ${name}" command.

    Input from arguments and print the variables- Firstly we pass the arguments after the filename and call the argument in bash file by using $ sign followed by index. Ex- if only one argument is passed, then we call in bash using "$1", if two argument is passed, then we call using "$1" and "$2" and so on.

    Here, bash test-shellScripts.sh Riya is the command used where 'Riya' is passed as an argument and the content shown above is displayed below.

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

    Below is the example where read -p is used to output the prompt string before reading user input and then the variables are declared num1 and num2. They are used in the if condition using ${num1} and ${num2} where -gt is used to compare the greater number in between the first and second number. ./ Operator followed by the shell filename is used to execute the shell script.

    Happy Learning!

    ~Shilpi