#Day4:- 90 Days DevOps Challenge @ Basic Linux Shell Scripting for DevOps Engineers.

#Day4:- 90 Days DevOps Challenge @ Basic Linux Shell Scripting for DevOps Engineers.

Mastering Linux Shell Scripting: A 90-Day DevOps Challenge ๐Ÿง๐Ÿ’ป๐Ÿš€

ยท

5 min read

๐Ÿ’ป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

ย