Basic Linux Shell Scripting for DevOps Engineers
Day 4 : #90DaysOfDevOps Challange
Table of contents
No headings in the article.
Task 1. Explain in your own words and examples, what is Shell Scripting for DevOps. Shell scripting is a programming language that is used to automate repetitive tasks in the command-line interface of an operating system. In the context of DevOps, shell scripting is used to automate and streamline the deployment and maintenance of software applications and infrastructure. In DevOps, shell scripting is used to automate tasks such as: 1)Building and deploying code to different environments 2)Configuring servers and applications 3)Monitoring system performance and generating alerts 4)Managing logs and backups 4)Automating testing and quality assurance Here's an example: Let's say you are a DevOps engineer responsible for deploying a web application to multiple servers. Instead of manually logging into each server and running the deployment commands, you can write a shell script that automates the deployment process. This script can be run on each server, and it will pull the latest code from the repository, install dependencies, and restart the web server. Shell scripting can save a significant amount of time and effort for DevOps engineers, who often have to perform repetitive and time-consuming tasks. Additionally, shell scripts can be easily version controlled and shared among team members, making it easier to maintain and improve DevOps processes over time.
TASK 2. What is #!/bin/bash?
can we write #!/bin/sh
as well?
#!/bin/bash is called a shebang or hashbang. It is the first line in a shell script and tells the operating system which interpreter to use for executing the script. In this case, it specifies that the script should be executed using the Bash shell. Yes, you can also use #!/bin/sh in the shebang line. sh is a shell that is available on most Unix-like operating systems, including Linux, macOS, and BSD. It is a simpler shell compared to Bash and may not have all the features and capabilities of Bash. However, it is a more portable shell and may be preferred in situations where the script needs to be executed on different operating systems or where a minimal shell environment is desired. It's worth noting that the shebang line must point to an interpreter that is present on the system where the script will be executed. If you specify an interpreter that is not installed, the script will fail to execute.
TASK 3. Write a Shell Script to take user input, input from arguments and print the variables. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#!/bin/bash
#take input from the user echo "Please enter your name:" read name
#take input from command-line arguments age=$1 city=$2
#print the variables echo "Name: $name" echo "Age: $age" echo "City: $city" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX In this script, the read command is used to take input from the user and store it in the variable name. The variables age and city are assigned the values of the first and second command-line arguments, respectively. To run this script, save it in a file (e.g., myscript.sh) and make it executable using the command chmod +x myscript.sh. Then, you can execute the script and pass in command-line arguments like this:
XXXXXXXXXXXXXXXXXXXXXXXX ./myscript.sh 30 "New York" XXXXXXXXXXXXXXXXXXXXXXXX This will set the age variable to 30 and the city variable to "New York". The script will then print out the values of all three variables.
Task 4. Write an Example of If else in Shell Scripting by comparing 2 numbers Here's an example of if else statement in shell scripting that compares two numbers: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#!/bin/bash
#Assign two numbers to variables number1=5 number2=10
#Compare the two numbers using if-else statement if [ $number1 -gt $number2 ] then echo "$number1 is greater than $number2" else echo "$number1 is less than or equal to $number2" fi XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX In this script, the variables number1 and number2 are assigned the values of 5 and 10, respectively. The if statement compares the two numbers using the -gt operator, which means "greater than". If number1 is greater than number2, the script prints a message saying that number1 is greater than number2. Otherwise, it prints a message saying that number1 is less than or equal to number2. Running this script will output: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 5 is less than or equal to 10 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX To change the values of the numbers being compared, you can modify the number1 and number2 variables in the script.