#Day4:- 90 Days DevOps Challenge @ Basic Linux Shell Scripting for DevOps Engineers.
Mastering Linux Shell Scripting: A 90-Day DevOps Challenge ๐ง๐ป๐
Table of contents
- ๐ปWhat Is Kernel ๐?
- ๐๐What Is Shell๐?
- ๐What is Linux Shell Scripting?๐งโ๐ป
- ๐งบ๐๏ธWhat is #!/bin/bash? can we write #!/bin/sh as well?
- ๐ฅ ๐งโ๐ปWrite a Shell Script which printsโ I 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.
๐ปWhat Is Kernel ๐?
The kernel is the core component of an operating system, acting as the bridge between software and hardware. ๐๐ ๏ธ
Think of it as the conductor of an orchestra, coordinating tasks and managing resources. ๐ต๐ฉโ๐ป
Example: In Linux, the kernel manages memory, processes, device drivers, and provides essential functionalities for programs to run efficiently. ๐ง๐ก
So, next time you use your computer or smartphone, remember that the kernel is the unsung hero making it all possible! ๐ฆธโโ๏ธ๐ป.
๐๐What Is Shell๐?
๐ Shell is a command-line interface that acts as an intermediary between the user and the operating system. It allows users to interact with the computer by entering commands. Think of it as the "shell" that surrounds the operating system, providing access to various utilities and services.
Example: Suppose you want to list all the files in a directory. In the shell, you can use the "ls" command:
Command: ls
Result: ๐๐ (Displays a list of files and directories in the current location)
With the shell, you can perform various tasks, from navigating the file system to running complex scripts, making it a powerful tool for both beginners and advanced users. ๐๐ง
๐What is Linux Shell Scripting?๐งโ๐ป
Linux Shell Scripting is a way of automating tasks and executing commands in the Linux operating system using scripts written in the shell language. ๐ง๐ป๐
Example: Let's say you want to create a script to automate the process of backing up your important files. You can create a shell script that copies the files to a backup directory:
#!/bin/bash
# This is a simple backup script
backup_dir="/home/user/backup"
source_dir="/home/user/documents"
cp -r $source_dir $backup_dir
In this example, the script copies all files from the "documents" directory to the "backup" directory. Running the script automates the backup process and saves you time! โณ๐พ
Linux Shell Scripting makes complex tasks simpler, enhances productivity, and empowers DevOps engineers to streamline workflows efficiently. Happy scripting! ๐คโจ
๐งบ๐๏ธWhat is #!/bin/bash? can we write #!/bin/sh as well?
๐ #!/bin/bash is called a shebang or hashbang in Linux. It's a special line at the beginning of a shell script that tells the system which interpreter to use to run the script. In this case, it specifies that the script should be executed using the Bash shell.
๐ค Yes, you can also use #!/bin/sh as the shebang line. This tells the system to use the default system shell to run the script, which is often Bash but can be another shell like Dash or Bourne shell (sh).
Example: Let's say you have a shell script named "myscript.sh". If you use #!/bin/bash as the shebang, the script will be executed with the Bash shell, like this:
#!/bin/bash
echo "Hello, World!"
If you use #!/bin/sh as the shebang, the script will be executed with the default system shell, which might be Bash or another compatible shell:
#!/bin/sh
echo "Hello, World!"
Both shebangs will produce the same output: "Hello, World!".
๐ฅ
๐งโ๐ปWrite a Shell Script which printsโ I will complete #90DaysOofDevOps challenge.
Simple shell script that prints the statement "I will complete #90DaysOfDevOps challenge".
#!/bin/bash
# Shell Script for 90DaysOfDevOps Challenge
echo "I will complete #90DaysOfDevOps challenge! ๐๐ช"
Save the script with a ".sh" extension, for example, "challenge_script.sh". Then, make it executable using the command: chmod +x challenge_script.sh
Run the script by typing: ./challenge_script.sh
The output will be:
I will complete #90DaysOfDevOps challenge! ๐๐ช
The above script uses the echo
command to print the specified statement! ๐๐ง๐ป.
๐ธ๏ธโจWrite a Shell Script to take user input๐ค, input from arguments and print๐จ๏ธ the variables.๐ฅ๐จ๏ธ๐จ๏ธ
๐๐ฌ Shell Scripting Fun: Print Variables! ๐๐
It is a simple code to take user input and input from command-line arguments, then print the variables with a touch of magic! โจ๐ก.
Open any text editor e.g.:- Vi, Vim etc.
In my case, I am using Vi editor. open the editor by typing Vi "File name".
Just type the below script in the file and save it by pressing ESC and then wq.
Here:- W is for save, q for quit.
#!/bin/bash
# Taking user input
read -p "Enter your name: " name
echo "Hello, $name! ๐"
# Input from command-line arguments
echo "You provided $# arguments. Here they are:"
# Loop through command-line arguments and print them
for arg in "$@"
do
echo "Argument: $arg ๐"
done
Save the above code in a file, let's say "variable.sh". To run the script,You have to give executable permission to the respective file as you have given in previous file and after that open the terminal, navigate to the script's directory, and execute the following command:
Output๐จ๏ธ:
๐๐Here, 1 2 3 4 are the arguments๐๐ฅ.
๐งญโจ๏ธWrite an Example of If else in Shell Scripting by comparing 2๏ธโฃnumbers.
Below is the script where we are using if else
in Shell Scripting to compare two numbers:
#!/bin/bash
# Variables
number1=10
number2=20
# If-Else Statement
if [ $number1 -gt $number2 ]; then
echo "Number 1 is greater than Number 2! ๐"
elif [ $number1 -lt $number2 ]; then
echo "Number 2 is greater than Number 1! ๐"
else
echo "Both numbers are equal! โจ"
fi
In this above script example, we have two variables, number1
and number2
, with values 10 and 20, respectively. The script compares the two numbers using the if-else
statement. If number1
is greater than number2
, it prints "Number 1 is greater than Number 2! ๐". If number1
is less than number2
, it prints "Number 2 is greater than Number 1! ๐". If both numbers are equal, it prints "Both numbers are equal! โจ".
You can modify the values of number1
and number2
to experiment with different comparisons! ๐งช๐
Thank you all for joining me on this exciting DevOps journey! ๐๐
I hope you found value in the blog and gained valuable insights into the world of DevOps. Remember, the key to success lies in continuous learning and collaboration. Let's keep pushing boundaries and elevating our skills as DevOps engineers. Feel free to reach out if you have any questions or need further assistance๐ by following me on Hashnode, LinkedIn (linkedin.com/in/mahesh-verma-441178250), and GitHub (https://github.com/maheshverma123)..
Happy DevOps adventures ahead! ๐๐ #DevOpsEngineer #ContinuousLearning #TechEnthusiast