Day 4: Basic Linux Shell Scripting for DevOps Engineers
Table of contents
Task 1: Explain in your own words and with examples what Shell Scripting means for DevOps
Shell Scripting in DevOps is automating repetitive tasks using commands for the Linux shell to make workflows faster and more reliable.
Automation: Shell script handles many issues like configuring servers or deploying applications instead of doing them manually. So, automation saves time and reduces errors.
System Admin Tasks: Scripts can be set to check disk space or CPU usage or run any backup without human interference, thus making system administration
Pipelines: Scripts maintain a streamlined testing, building, and deploying process in continuous integration/deployment; thus, development can go much faster and more efficiently in simple words - shell scripting is one of the keys to boosting efficiency in DevOps!
Task 2: What is #!/bin/bash? Can We Use #!/bin/sh?
In shell scripting, the line #!/bin/bash is called the shebang. It tells the system which interpreter to use to execute the script.
#!/bin/bash
: Executes the script with the Bash shell, which has advanced features such as arrays and functions π οΈ.
#!/bin/sh
: Executes the script with the Bourne shell, which is simpler and more POSIX-compliant but less feature-rich than Bash π§.
You can use either depending on your compatibility requirements. For most modern systems, Bash is preferred.
#!/bin/bash
echo "This script uses /bin/bash as the interpreter."
#!/bin/sh
echo "This script uses /bin/sh as the interpreter."
Task 3: Write a Shell Script that prints I will complete #90DaysOfDevOps challenge
.
#!/bin/bash
echo "I will complete the #90DaysOfDevOps challenge"
Task 4: Write a Script That Takes User Input and Command-Line Arguments
This task demonstrates how to capture user input and pass arguments to a script π. Below is the script I wrote for this:
#!/bin/bash
# Get user input
read -p "Enter your name: " name
# Access command-line arguments
echo "Hello, $name! You provided the following arguments:"
echo "$@"
# Example usage:
# ./script.sh arg1 arg2 "arg with spaces"
Explanation:
read -p "Enter your name: " name
: This line prompts the user to enter their name and stores the input in thename
variable.echo "$@"
: This line prints all the command-line arguments passed to the script.$1
,$2
, etc.: You can access individual arguments using these variables. For example,$1
represents the first argument,$2
the second, and so on.
Task 5: Provide an Example of an If-Else Statement
Control structures like if-else are essential for logic in scripts. Hereβs an example that compares two numbers and outputs the result:
Copy
Copy
#!/bin/bash
age=18
if [ $age -ge 18 ]; then
echo "You are eligible to vote."
else
echo "You are not eligible to vote."
fi
This script checks if the variable age
is greater than or equal to 18. If it is, the script prints "You are eligible to vote.". Otherwise, it prints "You are not eligible to vote."
Conclusion
Shell scripting is an invaluable tool in the DevOps toolkit π οΈ. Whether you're automating routine tasks, deploying applications, or configuring systems, mastering shell scripting will significantly boost your productivity π.
By automating manual tasks, we can focus on more important aspects of our work, reduce errors, and speed up deployments π.