What is Shell scripting?
In the world of DevOps, Shell scripting helps us automate tasks on the computer, making our lives easier!
Imagine Shell scripting as a set of instructions written in a special language that tells your computer what to do automatically. It's like giving your computer a recipe to follow.
🌟 Example: Let's say you have a folder with lots of files, and you want to organize them by moving all the images 📷 to an "Images" folder and the documents 📄 to a "Documents" folder.
Instead of manually moving each file, you can write a simple Shell script that will do it for you! The script will tell the computer to look for image files (like .png or .jpg) and move them to the "Images" folder. Similarly, it will find document files (like .docx or .pdf) and move them to the "Documents" folder. 🗂️
With just a few lines of code, you save a lot of time and effort! 🕒💪
That's the magic of Shell scripting in DevOps! It allows you to automate repetitive tasks, making your work more efficient and enjoyable.
What is #!/bin/bash?
The line #!/bin/bash
or #!/bin/sh
is written📝 at the beginning of a script and tells the computer🖥️ which program should be used to understand and run the script.
Think of it like a recipe. You have a set of instructions that need to be followed to prepare a dish🍽️. The shebang( #!
) line is like the first line of the recipe that tells you which chef's👨🍳 instructions to follow.
In this case, #!/bin/bash
means that the script should be executed using a program called "Bash". Bash is a popular program that understands and runs scripts written in a language called shell scripting.
Alternatively, #!/bin/sh
means that the script can be executed using the default system shell, which is a simpler version of Bash called the Bourne shell.
So, both lines serve the same purpose of telling the computer🖥️ how to run the script, but #!/bin/bash
specifies a specific program (Bash), while #!/bin/sh
uses the default shell on the system.
And you can use any of these commands while executing the program.
bash <file_name.sh>
or
sh <file_name.sh>
Let's understand Shell scripting a bit more better with few examples.
Write a Shell Script that prints
I will complete #90DaysOofDevOps challenge
command:nano <
filename.sh
>
The command
nano <
filename.sh
>
is used to open a text editor called Nano and create or edit a shell script file with the given<
filename.sh
>
#!/bin/bash
echo "I will complete #90daysofdevops challenge"
The echo
command is a simple command used in Linux and other operating systems to display text or messages on the screen. It is similar to the idea of "printing" something out.
To exit nano, all you need to do is to press CTRL
+ X
If you have any changes that have not been saved, you’ll be prompted to save the changes before you quit the editor.
Don’t forget:
Typing
nano
gets you into the Nano editor in WSLCTRL
+O
saves a Nano fileCTRL
+X
exits Nano
ubuntu@ip-000-31-92-000:~/Episodes$ bash 90daysofdevops.sh
I will complete #90daysofdevops challenge
Write a Shell Script to take user input, input from arguments, and print the variables.
- How to get input from the user?
-> “read” is the Linux command and “my_var” is the variable, that will store the input value.
- What is meant by Argument?
-> Arguments passed to a script are processed in the same order in which they’re sent. The indexing of the arguments starts at one, and the first argument can be accessed inside the script using $1. Similarly, the second argument can be accessed using $2, and so on.
ubuntu@ip-000-31-92-000:~/Episodes$ nano 90daysofdevops.sh
ubuntu@ip-000-31-92-000:~/Episodes$ ./90daysofdevops.sh Episodes
Teacher: How many task did you complete in 390daysofdevops challenge?
4
Episodes: I have completed 4 tasks.
Write an Example of If else in Shell Scripting by comparing 2 numbers
command:
nano <
filename.sh
>
The command
nano <
filename.sh
>
is used to open a text editor called Nano and create or edit a shell script file with the given<
filename.sh
>
Here's a breakdown of how
if-else
works in shell scripts:- The
if
statement checks a condition. If the condition is true (evaluates to a non-zero exit status), the commands within thethen
block are executed. If the condition is false (evaluates to a zero exit status), the commands within theelse
block are executed (if anelse
block is provided).
- The
The fi
keyword is used to mark the end of the if-else
construct. It indicates the end of the conditional block.
ubuntu@ip-000-31-92-000:~/Episodes$ bash if_else.sh 3
The two numbers are not equal
ubuntu@ip-000-31-92-000:~/Episodes$ bash if_else.sh 4
The two numbers are equal.