Day4 of 90DaysOfDevops : Basic Shell Script

Day4 of 90DaysOfDevops : Basic Shell Script


1. What Does Shell Scripting Mean for DevOps?

Shell scripting is the practice of writing scripts in a Unix/Linux shell, such as Bash, to automate tasks. For DevOps, shell scripting is a powerful tool because it helps automate system tasks, manage configurations, deploy applications, and perform routine maintenance. Automating these tasks reduces human error and increases efficiency, which is crucial in a DevOps workflow where continuous integration and continuous delivery (CI/CD) are key.

Examples of Shell Scripting Use Cases in DevOps:

  • Automating deployments: You can write scripts to automate the deployment of applications to various environments (development, staging, production).
  • Backup and recovery: Automating backups of important data or databases using shell scripts.
  • Configuration management: Use scripts to configure servers with necessary software, users, or permissions.


2. What is #!/bin/bash? Can We Write #!/bin/sh as Well?

The line #!/bin/bash or #!/bin/sh is called a shebang (#!), which indicates the script interpreter that will execute the script. It tells the operating system which interpreter to use to run the script.

  • #!/bin/bash: This shebang specifies that the script should be run using the Bash shell, which is a widely-used Unix shell with advanced features and syntax.
  • #!/bin/sh: This uses the sh shell, which is more basic and standardized across Unix systems. Many systems link sh to Bash, but the features available may be more limited compared to using #!/bin/bash.

Example Use Case Differences:

  • #!/bin/bash allows for Bash-specific features, like [[ ]] for advanced conditional expressions.
  • #!/bin/sh is more portable and may be used if you want your script to run on different Unix-based systems without requiring Bash-specific features.


3. Shell Script to Print "I will complete #90DaysOfDevOps challenge"

Here's a simple script to do that:

#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"        

To run the script:

  1. Save it in a file with .sh extension, e.g., motivation.sh.
  2. Make it executable: chmod +x motivation.sh.
  3. Run it: ./motivation.sh.


4. Shell Script to Take User Input, Arguments, and Print Variables

This script takes input directly from the user and also from command-line arguments:

#!/bin/bash

echo "Enter your name:"
read user_name

arg1=$1
arg2=$2

echo "Hello, $user_name!"
echo "First argument: $arg1"
echo "Second argument: $arg2"
        

Example Usage:

  1. Save it in a file with .sh extension, e.g., input_example.sh.
  2. Make it executable: chmod +x input_example.sh.
  3. Run the script with arguments: ./input_example.sh DevOps Automation.
  4. The script will prompt you for your name and print all the inputs.


5. Example of an If-Else Statement in Shell Scripting

Here’s how you can use an if-else statement to compare two numbers:

#!/bin/bash
# This script compares two numbers

num1=10
num2=20

if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
else
    echo "$num1 is not greater than $num2"
fi        

Explanation:

  • The -gt operator checks if num1 is greater than num2.
  • Other comparison operators include -lt (less than), -eq (equal), -ne (not equal).

To test the script:

  1. Save it with (.sh) extention, e.g example.sh
  2. Make it executable: chmod +x example.sh.
  3. Run it: ./example.sh.


Thank you for reading.

To view or add a comment, sign in

More articles by DALSANIYA VIVEK

Others also viewed

Explore content categories