Understanding the power of Shell Scripting in DevOps
Shell scripting is like a magic wand for DevOps professionals.
It allows you to execute a series of commands effortlessly, automating tasks and streamlining workflows. Imagine having a script, say "my_file.sh," that runs multiple commands in one go. Here's a simple example:
#!/bin/bash
touch devops.txt
echo "I'm learning DevOps and on day04 of my 90DaysOfDevOps journey" > devops.txt
cat devops.txt
Running this script will create a file, write a message, and display it—all in one swift motion.
The Shebang Mystery: #!/bin/bash vs. #!/bin/sh
The shebang (#!) at the start of a script tells the system which shell to use. Use #!/bin/bash
for scripts that require advanced Bash features, like arrays. Opt for #!/bin/sh
for basic, more portable scripts. Choose wisely based on your script's needs!
Challenge Accepted: A Simple Shell Script
Create a script named "devOps_challenge.sh" with the following content:
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
Save and run it to see your commitment in action!
Interactive Scripting: User Input and Arguments
Here's how to create a script that takes user input and command-line arguments:
#!/bin/bash
read -p "Enter your name: " user_name
arg1=$1
arg2=$2
echo "User input: $user_name"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"
Decision Making: If-Else in Shell Scripting
Compare two numbers using an if-else statement:
#!/bin/bash
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
if [ $num1 -gt $num2 ]; then
echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
echo "$num1 is less than $num2"
else
echo "$num1 is equal to $num2"
fi
Shell scripting is a powerful tool in the DevOps toolkit, enabling automation and efficiency. Embrace it to enhance your DevOps journey!
Subscribe to my newsletter
Read articles from Anirban Banerjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by