Skip to main content

Command Palette

Search for a command to run...

Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

Published
3 min readView as Markdown
Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

What is Shell Scripting?

  • A shell script is a text file that contains a sequence of commands for a UNIX-based operating system. It is called a shell script because it combines a sequence of commands, that would otherwise have to be typed into the keyboard one at a time, into a single script.

  • It's a way to automate repetitive tasks, making your life easier and more productive.

Why is Shell Scripting important in DevOps?

  • In the realm of DevOps, where speed and efficiency are key, shell scripting is the secret weapon!

  • Shell scripting for DevOps enables automation of deployment, configuration management, CI/CD pipelines, monitoring, logging, and performance optimization tasks. Shell scripting languages, such as Bash, are used for automating tasks in DevOps workflows.

  • With shell scripting, you can save time, reduce errors, and focus on the truly important tasks at hand!

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

  • The line "#!/bin/bash" is like the enchanting spell that tells the computer to use the Bash interpreter, unlocking a world of powerful features for shell scripting!

  • Yes, We can also write "#!/bin/sh" to use the default shell interpreter available on Unix-based systems.

  • However, keep in mind that it might limit access to some advanced features. So, if you want the full magical experience, go for "#!/bin/bash" and unlock the true potential of shell scripting!

Write a Shell Script which prints

I will complete #90DaysOofDevOps challenge.

create a file with extension .sh like 90days.sh

open file with any editor and write their

#!/bin/bash

echo "I will complete the #90DaysOfDevOps challenge!"

Save the file, open your terminal, and execute the following command to make the script executable

chmod +x 90days.sh

🔮 Finally, run the script using:

./90days.sh

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

Create a new file called user_input.sh and add the following captivating script:

#!/bin/bash

echo "Enter your name:"
read name

echo "Your name is: $name"

echo "Arguments provided: $@"

This script uses its magic to prompt you to enter your name, reads the input, and then prints it along with any command-line arguments you provide. Here's how you can cast the spell:

./user_input.sh lokesh kumar hello

The output will mesmerize you:

Your name is: lokesh kumar
Arguments provided: hello

Using If-Else Statements in Shell Scripting

Conditional statements, such as if-else, give your script the power to make decisions. Let's compare two numbers and conjure a message based on the comparison:

Create a new file called number_comparison.sh and add the following spellbinding script:

#!/bin/bash
number1=10
number2=20

if [ $number1 -gt $number2 ]
then
  echo "Number 1 is greater than Number 2"
elif [ $number1 -lt $number2 ]
then
  echo "Number 1 is less than Number 2"
else
  echo "Number 1 is equal to Number 2"
fi

In this enchanting script, we compare two numbers and conjure a message based on the result. Cast the spell by running:

./number_comparison.sh

The output will mesmerize you:

Number 1 is less than Number 2

With if-else statements, your script becomes a magical oracle that makes decisions based on the conditions you set!

More from this blog

Lokesh Kumar's blog

27 posts