Day4 of 90DaysOfDevops : Basic Shell Script
1. What Does Shell Scripting Mean for DevOps?
Shell scripting is the practice of writing scripts in a Unix/Linux shell, such as Bash, to automate tasks. For DevOps, shell scripting is a powerful tool because it helps automate system tasks, manage configurations, deploy applications, and perform routine maintenance. Automating these tasks reduces human error and increases efficiency, which is crucial in a DevOps workflow where continuous integration and continuous delivery (CI/CD) are key.
Examples of Shell Scripting Use Cases in DevOps:
2. What is #!/bin/bash? Can We Write #!/bin/sh as Well?
The line #!/bin/bash or #!/bin/sh is called a shebang (#!), which indicates the script interpreter that will execute the script. It tells the operating system which interpreter to use to run the script.
Example Use Case Differences:
3. Shell Script to Print "I will complete #90DaysOfDevOps challenge"
Here's a simple script to do that:
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
To run the script:
Recommended by LinkedIn
4. Shell Script to Take User Input, Arguments, and Print Variables
This script takes input directly from the user and also from command-line arguments:
#!/bin/bash
echo "Enter your name:"
read user_name
arg1=$1
arg2=$2
echo "Hello, $user_name!"
echo "First argument: $arg1"
echo "Second argument: $arg2"
Example Usage:
5. Example of an If-Else Statement in Shell Scripting
Here’s how you can use an if-else statement to compare two numbers:
#!/bin/bash
# This script compares two numbers
num1=10
num2=20
if [ $num1 -gt $num2 ]; then
echo "$num1 is greater than $num2"
else
echo "$num1 is not greater than $num2"
fi
Explanation:
To test the script:
Thank you for reading.
Very informative