Skip to main content

Command Palette

Search for a command to run...

Day 4 - Basic Linux Shell Scripting For DevOps Engineer🌈

Published
4 min read
Day 4 - Basic Linux Shell Scripting For DevOps Engineer🌈

Table of content

  • Introduction⚡

  • What is shell scripting for DevOps?📰

  • what is #!/bin/bash? can we write #!/bin/sh as well ?🧧

  • Writing a shell script to print a message🖨📺

  • Taking user input variables📥

  • Using If-Else statements in shell scripting💻

  • conclusion✨

Introduction

Welcome to Day 4 of the #90daysofdevopschallange!😊Today,we will learn about basics of linux shell scripting and its use case for the DevOps engineer. Shell script is nothing but set of commands executed by shell.📚 It automate the task, configuration management makes task easier for devops engineer. Let's go to the basics of shell scripting📰

What is shell scripting for DevOps?📰

Shell scripting is a technique in which a programmer writes a series of commands for the shell to execute📰. In the context of DevOps, shell scripting plays a crucial role in automating repetitive tasks and streamlining the deployment and management of applications.🍁

Shell scripting is used to automate repetitive tasks🧧 and streamline the development and deployment process.💻 This can include tasks such as setting up and configuring development environments, building and testing code, deploying code to various environments, and monitoring and maintaining systems.🧧

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

The “#!/bin/bash” is called a shebang and it is a special type of comment that is used to specify the interpreter that should be used to execute a script.

In this case, “/bin/bash” specifies the Bash shell as the interpreter. When a script is executed, the shell looks at the first line of the script file, and checks for the shebang. If it finds it, the shell uses the interpreter specified after the shebang to execute the script. In this case, the script is executed by /bin/bash, which is a common shell on Linux and Unix systems.

Yes, you can use “#!/bin/sh” instead of “#!/bin/bash” to specify that the script should be executed by the sh shell.

Writing a shell script to print a message🖨📝

Here’s an example of a simple shell script that will print the message “I will complete #90DaysOfDevOps challenge” to the console:

This script uses the “echo” command to output the message to the console. The shebang line at the top of the script specifies that Bash should be used as the interpreter.📃

To run this script, you can save it to a file with a “.sh” extension (e.g. “new.sh”), make it executable with the command “chmod +x new.sh”, and then execute it with “./new.sh” or by specifying the full path to the script.📝

Taking user input variables📥📒

Here’s an example of a shell script that takes user input, input from arguments, and prints the variables:🖨

This script prompts the user to enter their name, reads the input into the variable “name”, and then assigns the first argument📒. It then uses the “echo” command to print the values of these variables to the console, along with some descriptive text.🌈

Using If-Else statements in shell scripting💻

Here’s an example of using if-else statements in shell scripting to compare two numbers:⚡

In this example, we assign the values 20 and 10 to the variables “num1” and “num2”, respectively. We then use if-else statements to compare these two numbers.🆘

First, we check if num1 is greater than num2 using the condition [ $num1 -gt $num2 ]. If this condition is true, we print the message “$num1 is greater than $num2”.⚡

If the first condition is false, we move on to the next condition using elif [ $num1 -lt $num2 ]. If this condition is true, we print the message “$num1 is less than $num2”.

If both of these conditions are false, we move on to the final else statement, which is executed when num1 is equal to num2. In this case, we print the message “$num1 is equal to $num2”.✳

To execute this script, you can save it to a file with a “.sh” extension (e.g. “task.sh”), make it executable with the command “chmod 777 task.sh”, and then run it using “./task.sh”. The output will be the message “$num1 is greater than $num2”, so 20 is greater than 10.🌈

Conclusion

You can use shell script to automate,customize DevOps tasks. you can create new shell scripts to make easier your work and unlock the journey of DevOps!⚡

Stay connected with me for latest blog related to aws ☁ Devops and linux📝

Thank you for reading🙏 Keep learning and keep growing!🌈✳