Table of contents
- What is Shell Scripting?
- Why is Shell Scripting Important for DevOps?
- Commenting in Shell Scripting
- Example Use Cases for Shell Scripting in DevOps:
- What is #!/bin/bash?
- Can We Write #!/bin/sh?
- When to Use #!/bin/bash or #!/bin/sh?
- Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.
- Write a Shell Script that takes user input, input from arguments, and prints the variables.
- Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.
- FAQs
Shell scripting in DevOps is like writing a set of instructions or commands that tell the computer what tasks to perform, all bundled in a single script. Instead of typing each command one by one, a shell script allows you to automate repetitive tasks, making life easier for system administrators and DevOps engineers.
What is Shell Scripting?
Shell scripting involves creating a script (a text file) with a series of Linux/Unix commands, executed by the shell (the command-line interface of an OS). The most common shell is bash, but there are others like sh, zsh, and fish.
In DevOps, shell scripts are incredibly useful for:
Automating tasks like setting up environments, deploying applications, and running tests.
Configuring systems such as servers, databases, or cloud environments.
Managing large systems by performing tasks across multiple servers at once.
Why is Shell Scripting Important for DevOps?
DevOps is all about automating repetitive processes to speed up the development cycle, improve collaboration, and ensure more reliable software delivery. Shell scripting plays a critical role in automating these tasks and creating seamless workflows.
Commenting in Shell Scripting
1. Single-line comments
A single-line comment starts with a hashtag symbol with no white spaces (#) and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment.
The shell script is commented out prefixing # character for single-line comment. Single-line comments are useful for brief explanations of code snippets or specific lines.
2. Multi-line comments
Multi-line comment is a piece of text enclosed in a delimiter (”) on each end of the comment. Again there should be no white space between delimiter (‘ ‘). They are useful when the comment text does not fit into one line; therefore need to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment.
#!/bin/bash
# this is single line comment
'
this is multi-line comment
'
<<< commment
this is a also multi-line comment
with above defined header as "comment"
comment
Example Use Cases for Shell Scripting in DevOps:
Automating Deployments: You can write a shell script to automatically deploy a web application to a server. Instead of manually copying files, restarting services, and configuring permissions, the script will handle everything for you.
Example:
#!/bin/bash # This script deploys a web app # Copy files to the web server directory cp -r /home/user/myapp /var/www/html # Restart the web server to apply changes systemctl restart apache2 echo "Deployment complete!"
Environment Setup: When a new developer joins the team or you're setting up a new server, you can create a script that installs all the necessary dependencies, configures the environment, and sets up databases or services.
Example:
#!/bin/bash # Setting up a Node.js environment # Update system sudo apt-get update # Install Node.js sudo apt-get install -y nodejs # Install npm (Node package manager) sudo apt-get install -y npm echo "Node.js environment setup complete!"
CI/CD Pipelines: In continuous integration/continuous delivery (CI/CD) pipelines, shell scripts can automate tasks like running tests, building code, and deploying applications across environments (development, staging, production).
Example:
#!/bin/bash # Running tests and deploying an app # Run unit tests npm run test # If tests pass, deploy the app if [ $? -eq 0 ]; then echo "Tests passed, deploying..." ./deploy.sh else echo "Tests failed, aborting!" fi
Key Benefits of Shell Scripting in DevOps:
Automation: Eliminates manual effort, reducing human error and speeding up repetitive tasks.
Consistency: Every time the script runs, it performs the task the exact same way, ensuring consistency across deployments and configurations.
Efficiency: A script can handle complex, multi-step tasks in seconds, compared to manual execution that could take minutes or hours.
Scalability: You can use a single shell script to perform actions across multiple servers or environments.
What is #!/bin/bash
?
The #!/bin/bash
at the beginning of a script is called a shebang (or hashbang). It tells the system which interpreter (shell) should be used to execute the commands in the script. When the script is run, the operating system looks at this line to determine which program will interpret the following lines of code.
#!
: This is the actual shebang./bin/bash
: This is the path to the Bash shell (Bourne Again Shell), which is the most common shell used in Linux and macOS environments.
So, when you write #!/bin/bash
, you are instructing the system to use the Bash shell to interpret and run your script.
Can We Write #!/bin/sh
?
Yes, you can write #!/bin/sh
as well. Here's the difference:
/bin/sh
: This refers to the Bourne shell (or a Bourne-compatible shell). It is often a symbolic link to another shell-likebash
,dash
, orksh
depending on the system. Scripts written with#!/bin/sh
are expected to be POSIX-compliant and may lack some of the extended features found in Bash.
Difference between #!/bin/bash
and #!/bin/sh
:
/bin/bash
: Points explicitly to the Bash shell, which includes many features not available in the POSIXsh
, like[[ ... ]]
conditionals,for (( ... ))
loops, and array support./bin/sh
: Refers to a more basic, POSIX-compliant shell, which ensures higher portability across different systems, but may lack some of the extended features of Bash.
Example:
If your script uses Bash-specific features like arrays, #!/bin/bash
is necessary. Otherwise, if you're writing simple POSIX-compliant scripts, #!/bin/sh
is more portable.
Example of a Bash-specific script (requires #!/bin/bash
):
#!/bin/bash
# This script uses Bash arrays
fruits=("Apple" "Mango" "Banana")
echo ${fruits[1]} # Outputs 'Mango'
Example of a POSIX-compliant script (can be run with #!/bin/sh
):
#!/bin/sh
# This script is POSIX-compliant and doesn't use Bash-specific features
echo "Hello, World!"
When to Use #!/bin/bash
or #!/bin/sh
?
Use
#!/bin/bash
if you're using Bash-specific features and don't need portability across systems that might not have Bash.Use
#!/bin/sh
if you want your script to be more portable and run across different Unix-like systems, including those that might not have Bash installed.
Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.
Open a text editor and save the script with a .sh
extension, for example: devops_challenge.sh
#!/bin/bash
# Script to print a motivational message
echo "I will complete #90DaysOfDevOps challenge."
Make the script executable by running the following command:
chmod +x devops_challenge.sh
Run the script:
./devops_challenge.sh
Write a Shell Script that takes user input, input from arguments, and prints the variables.
Shell Script:
#!/bin/bash
# Script to take user input, input from arguments, and print them
# Check if arguments were passed to the script
if [ $# -eq 0 ]; then # start of if statement and -eq => equal to
echo "No arguments provided. Please provide arguments."
exit 1
fi # end of if statement
# Take input from the user
echo "Please enter your name:"
read user_input
# Input from arguments
arg1=$1 # this is called arguments and are taken from the user input command
arg2=$2
# Print the variables
echo "User input (name): $user_input"
echo "First argument: $arg1"
echo "Second argument: $arg2"
Explanation:
$#
checks if the number of arguments is zero (meaning no arguments were passed).read user_input
takes user input and stores it in the variableuser_input
.$1
and$2
capture the first and second arguments passed to the script.The script then prints the user input and the arguments.
Steps to Run the Script:
Save the script as
input_script.sh
.Make the script executable:
chmod +x input_script.sh
Run the script with arguments:
./input_script.sh argument1 argument2
Example Output:
$ ./input_script.sh DevOps Linux
Please enter your name:
Harshit
User input (name): Harshit
First argument: DevOps
Second argument: Linux
Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.
Shell Script:
#!/bin/bash
# Script to compare two numbers using If-Else
# Take two numbers as input from the user
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
# Compare the two numbers
if [ $num1 -gt $num2 ]; then
echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
echo "$num1 is less than $num2"
else
echo "$num1 is equal to $num2"
fi
Explanation:
if [ $num1 -gt $num2 ]
: Checks ifnum1
is greater thannum2
using the-gt
operator.elif [ $num1 -lt $num2 ]
: Checks ifnum1
is less thannum2
using the-lt
operator.else
: Executes if the two numbers are equal.
Operators Used:
-gt
: Greater than-lt
: Less than-eq
: Equal to (used inside the else block in this case)
Steps to Run the Script:
Save the script as
compare_numbers.sh
.Make it executable:
chmod +x compare_numbers.sh
Run the script:
./compare_numbers.sh
Example Output:
Enter the first number:
15
Enter the second number:
10
15 is greater than 10
FAQs
- What is kernel?
The core component of the Linux operation system (OS) that acts as the interface between the computer hardware and its process.
TRICK
A → Application
S → Software
K → Kernel
H → Hardware
- What is Shell?
A shell is a program that allow a user to interact with operating system (OS). It acts as an intermediary between the user and the kernel which manage the core services of OS.