๐ Day 4 - Basic Linux Shell Scripting ๐ง๐ป
Table of contents
- What is kernel๐ง
- What is Shell ๐
- What is Linux Shell Scripting? ๐
- Explain in your own words and examples, what is Shell Scripting for DevOps.
- What is#!/bin/bash?Can we write#!/bin/shas well?
- Write a Shell Script which printsI will complete #90DaysOofDevOps challenge.
- Write a Shell Script to take user input, input from arguments and print the variables.?
- Write an Example of If else in Shell Scripting by comparing 2 numbers.
Joining the #90DaysofDevops challenge ๐ under the guidance of Shubham Londhe, alongside the #TrainWithShubham community! ๐โจ
What is kernel๐ง
The kernel is a computer program that is the core of a computerโs operating system, with complete control over everything in the system.
A kernel is a crucial part of a computer's operating system (OS) that acts as an intermediator between the hardware and the user-level applications. It plays a central role in managing key functions such as system calls, disk operations, and memory utilization.
The kernel is the first component that loads into memory during the boot process and remains active until the system is shut down. Essentially, it is the core element necessary for the proper functioning of an OS on hardware. The kernel is responsible for coordinating CPU cores, allocating memory, and overseeing various hardware-related tasks.
Now, think of the shell as your friendly assistant. It's the link between you (the user) and the boss (the kernel). You tell the shell what you want to do, and it makes sure the boss understands.
What is Shell ๐
A shell is special user program which provide an interface to user to use operating system services. Shell accept human readable commands from user and convert them into something which kernel can understand. It is a command language interpreter that execute commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal.
What is Linux Shell Scripting? ๐
A shell script is a computer program designed to be run by a linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
๐Day 04 #90DaysOfDevOps Challenge๐ ๏ธ๐ก
Welcome to the #90DaysofDevOps challenge led by Shubham Londhe! ๐Today marks Day 4 of my journey with the #TrainWithShubham Community. ๐๐จโ๐ป
Day 4 TASKS:
Explain in your own words and examples, what is Shell Scripting for DevOps.
Shell scripting for DevOps involves writing scripts to automate tasks and manage systems. These scripts help in performing repetitive tasks, managing configurations, deploying applications, and monitoring systems. By using shell scripts, DevOps engineers can streamline workflows, reduce manual errors, and improve efficiency.
In DevOps, shell scripting is used to automate a wide range of tasks, such as configuring servers, deploying applications, running tests, and monitoring system performance. These tasks can be executed on a single machine or across multiple machines in a distributed environment.
For example, a DevOps engineer might use shell scripting to create a script that automatically deploys a new version of a web application to a production server. The script would include commands to stop the existing application, download the new version, update any configuration files, and restart the application. The script could be run manually or scheduled to run automatically on a regular basis.
Here are some key aspects of Shell Scripting in the context of DevOps:
Automation of Deployment and Configuration:
Shell scripts are used to automate the deployment of applications and configuration of servers. For example, a script can be created to install dependencies, set up environment variables, and deploy the latest version of an application.
# Example: Deploying a web application #!/bin/bash echo "Updating code from version control..." git pull echo "Installing dependencies..." npm install echo "Restarting the application server..." systemctl restart my-todo-app
Continuous Integration/Continuous Deployment (CI/CD):
Shell scripts are commonly used in CI/CD pipelines to automate building, testing, and deploying applications. They integrate with CI/CD tools to execute these tasks automatically when code changes are pushed.
# Example: CI/CD pipeline script #!/bin/bash echo "Building the application..." ./build.sh echo "Running tests..." ./run_tests.sh echo "Deploying to production..." ./deploy_prod.sh
Infrastructure as Code (IaC):
Shell scripts help implement Infrastructure as Code principles, allowing DevOps teams to define and manage infrastructure using code. This includes provisioning and configuring servers, networks, and other infrastructure components.
# Example: Provisioning a virtual machine
#!/bin/bash
echo "Creating a new virtual machine..."
virt-install --name my-vim --memory 2048 --disk size=20 --cdrom my-os.iso
What is
#!/bin/bash?
Can we write#!/bin/sh
as well?The
#!/bin/bash
and#!/bin/sh
are examples of shebang lines used in Unix-like operating systems to specify the interpreter that should be used to execute the script. Here's a more detailed explanation of each:Shebang Line (
#!
)The shebang line (
#!
) at the beginning of a script indicates an interpreter for the script's execution.It is followed by the path to the interpreter executable.
#!/bin/bash
#!/bin/bash
specifies that the script should be run using the Bash shell.Bash (Bourne Again SHell) is a widely used command language interpreter that is compatible with the Bourne shell (sh) but includes many enhancements and features.
#!/bin/sh
#!/bin/sh
specifies that the script should be run using the Bourne shell or a compatible shell.Historically,
/bin/sh
referred to the original Bourne shell, but on many modern systems,/bin/sh
is a symlink to another shell likedash
(on Debian-based systems) orbash
(on some other systems) that operates in a mode compatible with the traditional Bourne shell.
Differences Between #!/bin/bash
and #!/bin/sh
Compatibility: Scripts written with
#!/bin/sh
are usually more portable and can run on any system with a Bourne-compatible shell. However, they may lack some of the advanced features found in Bash.Features:
#!/bin/bash
allows you to use Bash-specific features and extensions, which may not be available in/bin/sh
.
Example
Here's a simple example script that prints "Hello, World!" using both shebangs:
- Using
#!/bin/bash
:
bashCopy code#!/bin/bash
echo "Hello, World!"
- Using
#!/bin/sh
:
shCopy code#!/bin/sh
echo "Hello, World!"
In this simple example, both scripts would behave the same. However, for more complex scripts, the differences in available features and behaviors between bash
and sh
could lead to different results.
Practical Usage
If your script uses advanced features specific to Bash (such as arrays,
[[
for conditional expressions, or extended globbing), you should use#!/bin/bash
.If your script adheres to POSIX standards and you aim for maximum compatibility across different Unix-like systems, you should use
#!/bin/sh
.
Conclusion
Both #!/bin/bash
and #!/bin/sh
can be used, but the choice depends on the script's requirements for compatibility and features. Using #!/bin/bash
provides access to more advanced Bash features, while #!/bin/sh
ensures greater portability across different systems.
Write a Shell Script which prints
I will complete #90DaysOofDevOps challenge.
Step-by-Step Guide to Writing and Running a Shell Script which prints I will complete #90DaysOofDevOps challenge.
Open a terminal.
Create and open a new script file with
vim
:Write the script in
vim
:- Press
i
to enter Insert mode.
- Press
Press
Esc
to exit Insert mode.Type
:wq
and pressEnter
to save and exit.
- Make the script executable:
Run the script:
Write a Shell Script to take user input, input from arguments and print the variables.?
Create and open a new file named
rhythm1.sh
withvim
:Write the Script in
vim
:Press
i
to enter Insert mode.Type the script provided below and Press
Esc
to exit Insert mode.Type
:wq
and pressEnter
to save and exitvim
.Make the Script Executable:
Run the Script:
Execute: ./rhythm1.sharg1_value arg2_value
./rhythm1.sh Devops 90DaysofDevops
Write an Example of If else in Shell Scripting by comparing 2 numbers.
Create File : comapare2num.sh:
Give permission for execution:
chmod +x comapare2num.sh
Write the If-Else script in bash shell:
#!/bin/bash
# Prompt the user to enter two numbers
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
# Compare the numbers
if [ "$num1" -gt "$num2" ]; then
echo "$num1 is greater than $num2"
else
echo "$num1 is not greater than $num2"
fi
Run the Script:
./comapare2num.sh
This script compares two numbers (num1
and num2
) and prints a message based on the comparison result using an if-else statement. Save this script in a file, for example, comapare2num.sh
, and give execute permissions using chmod +x compare_
numbers.sh
. Run the script, and it will prompt you to enter two numbers, then compare and print the results based on the if-else conditions.
Thank you for taking the time to read this blog. I hope you found valuable insights! If you enjoyed the content, please consider giving it a like, sharing it, and following for more insightful posts in the future. Your support means a lot! Looking forward to sharing more knowledge with you! ๐
๐A special thanks to Shubham Londhe #TrainWithShubham and the DevOps community for organizing this fantastic initiative. Let's learn, grow, and make a difference through DevOps!
#TrainWithShubham #90daysofdevops #automation #Devops #Scaling #Infrastructure #ContinuousLearning #DevOpsJourney #CommunityDriven #LinkedInPost #shubhamlondhe #devopsengineer #awsdevops #AWS #Azure #AzureDevops
Let's Connect..!
๐LinkedIn
๐Twitter
๐GitHub
:)Happy Learning...
Thank you for reading! ๐
Subscribe to my newsletter
Read articles from Rhythm Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rhythm Mishra
Rhythm Mishra
๐ ๏ธ DevOps Engineer ๐ ๏ธ with a focus on streamlining deployments ๐ and fostering collaboration ๐ค between development and operations teams. Specializing in automating pipelines โ๏ธ and implementing scalable cloud solutions โ๏ธ. Committed to lifelong learning ๐ and sharing practical insights into DevOps challenges and solutions ๐ก.