Shell Scripting
Table of contents
- Shell Scripting for DevOps:
- Example: Automating Software Deployment with Shell Script:
- #!/bin/bash and #!/bin/sh:
- Shell Script to Print #90DaysOfDevOps Challenge:
- Shell Script to Take User Input and Arguments:
- Example of If-Else in Shell Scripting (Comparing Numbers):
- How Shell Scripting Empowers DevOps Automation:
Shell Scripting for DevOps:
Shell scripting is an effective DevOps technology that enables the automation of numerous tasks and the simplification of business processes in a Linux/Unix system. A key skill in automating infrastructure management, software deployments, and continuous integration/continuous deployment (CI/CD) pipelines, it lets DevOps experts carry out a series of instructions and accomplish tasks programmatically.
DevOps engineers may manage cloud resources, choreograph processes, configure servers, and interface with a variety of tools and services using shell scripts. Shell scripts provide flexibility and portability since they can readily be used on other systems without requiring significant changes.
Example: Automating Software Deployment with Shell Script:
#!/bin/bash
# Shell script to deploy a web application
echo "Starting deployment..."
git pull origin main
npm install
npm run build
pm2 restart app.js
echo "Deployment completed!"
#!/bin/bash and #!/bin/sh:
#!/bin/bash
is a shebang line that specifies the interpreter to execute the script. It indicates that the script should be interpreted and executed using the Bash shell, which is a commonly used shell in Linux.
Yes, you can also write #!/bin/sh
, which specifies that the script should be interpreted and executed using the system's default shell. On most Linux systems, /bin/sh
is a symbolic link to another shell (usually /bin/bash
), so using #!/bin/sh
might behave the same as #!/bin/bash
in many cases.
Shell Script to Print #90DaysOfDevOps Challenge:
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
Shell Script to Take User Input and Arguments:
#!/bin/bash
# Taking input from the user
echo "Enter your name:"
read name
echo "Hello, $name! Welcome to the DevOps journey."
# Taking input from arguments
echo "Hello, $1! Welcome to the DevOps journey."
To run the script, follow these steps:
Open a Vi editor on your system.
Save the script into a file named
welcome_script.sh
.Make the script file executable by running the following command in the terminal:
chmod +x welcome_script.sh
Now, you can run the script in two different ways:
a. Taking Input from the User:
Run the script using the following command:
./welcome_script.sh
The script will prompt you to enter your name. Type your name and press Enter.
The script will then greet you with a personalized message, such as "Hello, Shivam! Welcome to the DevOps journey."
b. Taking Input from Arguments:
Run the script using the following command with your name as an argument:
./welcome_script.sh Shivam
The script will display the greeting message using the provided argument, such as "Hello, Shivam! Welcome to the DevOps journey."
Note: In the first case (taking input from the user), the script uses the read
command to read input from the user during runtime. In the second case (taking input from arguments), the script uses the special variables $1
, which represents the first argument provided when executing the script. In this case, the value of $1
will be the name you provide after the script's name on the command line.
Example of If-Else in Shell Scripting (Comparing Numbers):
#!/bin/bash
# Compare two numbers using if-else
num1=10
num2=20
if [ $num1 -eq $num2 ]; then
echo "Both numbers are equal."
elif [ $num1 -lt $num2 ]; then
echo "num1 is less than num2."
else
echo "num1 is greater than num2."
fi
How Shell Scripting Empowers DevOps Automation:
In this post, we looked at the value of shell scripting in the context of DevOps and some of its practical uses. Shell scripts allow DevOps engineers to interface with the operating system and various tools programmatically by acting as strong command-line interpreters that carry out human-readable commands.
Shell scripting's ability to automate repetitive operations, which minimizes human participation and eliminates errors, is one of its main benefits. DevOps professionals may easily configure servers, manage cloud resources, and streamline software deployments by using effective and reusable scripts.
Thank you for taking out valuable time and effort to read it till the very end. Feedback is always appreciated.