The Simplified Path to Learning Linux Shell Scripting: A Beginner-Friendly Guide

The Simplified Path to Learning Linux Shell Scripting: A Beginner-Friendly Guide

Like knowing how a house is constructed, understanding Linux architecture is important to know how Linux and users interact. How do they work? It helps the operation, management, maintenance, and optimization of your machine. It's necessary for Tech careers and improving the performance of your machine. so let's get started with the basics of Linux Architecture.

🌟Components of Linux Architecture

1.Hardware

2.Kernel

3.Shell

4.System Libraries / Utilities / Applications

Figure: Linux Architecture

💻 Hardware: The hardware layer is like the body of our computer, including important parts like CPU 🧠, RAM , and HDD 💽.

🐧 Kernel: The Linux Kernel is like the heart of the Linux Operating System. It connects the hardware with the software and manages memory, deciding which tasks 📊 get to use the CPU 🔄.

🐚 Shell: The Shell is your friendly coding buddy 🤖 that talks to the Kernel. You can tell it what to do and it makes things happen. It's like the middleman between you and your computer, helping you run commands and apps 🚀

📚System Libraries: System libraries are collections of precompiled functions and code that provide essential programming interfaces and functionality to applications running on the operating system

You can find a path in Linux : /usr/lib , /usr/local/lib , /usr/lib/x86_64-linux-gnu

🔧 System Libraries / Utilities / Applications: Utilities are a collection of software tools and programs that help manage and maintain various aspects of the operating system. These utilities are essential for system administration and day-to-day usage of the Linux system.

You can find a path in Linux: /usr/bin, /usr/sbin, /usr/local/bin and /usr/local/sbin

Examples:

Text Editors: - vim, nano

Package Management Utilities: - apt, yum

Process Management Utilities: ps, top, kill, nice

Have you wondered about how we code in Linux for automation? Don't worry about , I will clarify the following way so that you can understand.

Shell Scripting📃

What is Shell Scripting ? and why do you need it?

Shell scripting is a way to tell your computer what to do using simple, step-by-step instructions written in a special language. It's similar to writing a script for your computer to follow so that it can conduct things like organizing files or launching programs automatically.

Have you ever looked at a computer script and wondered about the unclear symbols at the start, such as "#!/bin/bash"? It might look to be some sort of hidden code, so don't worry! Today, we'll sort out this technical mystery and learn what it implies, as well as whether we may use something like "#!/bin/sh" instead.

Sh (Shell): It is similar to the bash. There are various shells available, such as Bash, Zsh, Dash, and more. Each shell has its own set of features and capabilities.

🐚 Bash (Bourne-Again Shell): Bash is a powerful and popular shell, or command-line interface, found on most Unix-based systems, including Linux. It allows you to interact with your computer through text commands, making it a handy tool for automation and managing files.

📜 Script Language: In our digital world, different scripting languages exist, and each has its own set of rules and commands. "#!/bin/bash" specifically says(For an example), "I will complete #90DaysOofDevOps challenge"

Step 1: Create a new shell script file touch script1.sh

touch script1.sh

Step 2: Open the file in a text editor (vim) by

vim script1.sh
#!/bin/bash
echo I will complete 90DaysOfDevOps challenge

Step 3: Make the script executable

chmod +x script1.sh

Step 4: Run the script to display the content

./script1.sh

Shell Script to take user input, input from arguments and print the variables

Now we will Write a Shell Script to take user input, input from arguments and print the variables.

vim script2.sh

👉Taking User Input

#!/bin/bash

# Prompt the user for input
echo "Enter your name: "
read userInput

echo Your Name is $userInput
chmod +x script2.sh
./script2.sh

Taking command-line arguments & printing it as a variable

So I am editing the file script2.sh

#!/bin/bash

# Prompt the user for input
echo "Enter your name: "
read userInput

# Check if at least two command line arguments are provided
if [ $# -ge 2 ]; then
    argInput1="$1"
    argInput2="$2"
else
    argInput1="No argument provided"
    argInput2="No second argument provided"
fi

# Print the variables
echo "User input: $userInput"
echo "Argument input 1: $argInput1"
echo "Argument input 2: $argInput2"

Arguments are passed by the command line you can get an idea by following the way

./script2.sh arg1_here arg2_here

You can then run the script, providing arguments if desired, and it will display the user input and command-line arguments as we have done above.

Comparing Two Numbers by shell scripting 🙄

Now, by comparing two numbers, we will write an example of If else in Shell Scripting.

vim compare_num.sh
#!/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" -eq "$num2" ]; then
    echo "The numbers are equal."
elif [ "$num1" -lt "$num2" ]; then
    echo "The 1st number ($num1) is less than the 2nd number ($num2)."
else
    echo "The 1st number ($num1) is greater than the 2nd number ($num2)."
fi

Save the code and run it

./compare_num.sh

In a nutshell, you now understand the fundamentals of Shell Scripting for DevOps. It all comes down to automating processes and making them do what you want. Have fun scripting!

I hope you enjoy the blog post!

If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊

Did you find this article valuable?

Support Vyankateshwar Taikar's blog by becoming a sponsor. Any amount is appreciated!