Day 04: Mastering Linux: The Power of Shell Scripting

CloudDeepDive
5 min readApr 22

--

This is the #Day04 of the #90DaysofDevOps challenge!

Introduction:

Linux is an operating system that is used extensively in the IT industry. One of the reasons for its popularity is its ability to be customized to meet the specific needs of a user or organization. Linux shell scripting is one of the most powerful ways to customize Linux and automate repetitive tasks.

In this blog, we'll discuss Linux shell scripting, including the concepts of kernel, shell, and shell scripting.

Kernel:

The kernel is the core of the Linux operating system. It is responsible for managing the system's resources, such as memory and processing power, and communicating with the computer's hardware components. The kernel is loaded into memory when the computer is started, and it remains there until the system is shut down.

Shell:

A shell is a command-line interface that allows users to interact with the Linux operating system. The shell provides a user-friendly interface that accepts commands entered by the user and executes them. The shell is responsible for interpreting the commands and passing them to the kernel for execution.

There are several types of shells available in Linux, including:

  • Bash: The Bourne-Again SHell (Bash) is the most commonly used shell in Linux. It is the default shell for most Linux distributions.
  • Zsh: Z shell (zsh) is a powerful and feature-rich shell that is gaining popularity among Linux users.
  • Fish: Fish is a user-friendly shell that is designed to be easy to use for beginners.

Shell Scripting:

Shell scripting is the process of creating scripts that automate tasks in Linux. Shell scripts are text files that contain a series of commands that are executed in sequence by the shell. Shell scripts can be used to perform a wide range of tasks, from simple file manipulation to complex system administration tasks.

Shell scripting can be used to:

  1. Automate repetitive tasks: Shell scripting can be used to automate repetitive tasks, such as backing up files, renaming files, or updating software packages.
  2. Perform system administration tasks: Shell scripting can be used to perform system administration tasks, such as managing user accounts, configuring network settings, and monitoring system performance.
  3. Manage files and directories: Shell scripting can be used to manage files and directories, such as copying files, moving files, and deleting files.

Creating a Shell Script:

To create a shell script, you will need to create a text file that contains a series of commands that you want to execute. The file should have a .sh extension to indicate that it is a shell script.

Here is an example of a simple shell script that creates a backup of a file:

#!/bin/bash
cp /path/to/file /path/to/backup/file

In this example, the script starts with the "#!/bin/bash" line, which tells the shell that this is a Bash script. The next line uses the "cp" command to copy the specified file to a backup location.

Once you have created the shell script, you can execute it by typing the name of the script in the terminal. For example, if the script is named "backup.sh", you would type:

$ ./backup.sh

Here are some examples:

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

#!/bin/bash
# This script demonstrates how to take input from user and arguments

# Take user input
echo "Please enter your name:"
read name
echo "Your name is: $name"

# Take input from arguments
echo "The first argument is: $1"
echo "The second argument is: $2"

In this example, the script prompts the user to enter their name and then prints it out. It also takes two arguments from the command line and prints them out.

Example 2: If else statement in Shell Scripting by comparing 2 numbers:

#!/bin/bash
# This script demonstrates how to use if-else statement in shell scripting

echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2

if [ $num1 -gt $num2 ]
then
echo "$num1 is greater than $num2"
elif [ $num1 -eq $num2 ]
then
echo "$num1 is equal to $num2"
else
echo "$num1 is less than $num2"
fi

In this example, the script takes two numbers as input from the user and then compares them using an if-else statement. If the first number is greater than the second number, it prints out a message saying so. If the two numbers are equal, it prints out a different message. If the first number is less than the second number, it prints out yet another message.

Conclusion:

In conclusion, Linux shell scripting is a powerful tool for customizing and automating tasks in the Linux operating system. With the ability to create shell scripts, users can automate repetitive tasks, perform system administration tasks, and manage files and directories. Understanding the concepts of kernel, shell, and shell scripting is essential for anyone who wants to use Linux effectively.

This is the #Day04 of the #90DaysofDevOps challenge! Hope you found this blog informative and useful. If so, please give it a clap👏 and share it with others who might benefit from it.

Don’t forget to subscribe to our “CloudDeepDive” space for more exciting content on DevOps and cloud technologies.

Thanks for reading!

Next Blog: Day 05: Day 05: Understanding Continuous Integrations(CI) & Continuous Delivery/Deployment(CD)

--

--

CloudDeepDive

Hey Folks! Let's Deep Dive the clouds 🌨️ with me. #Cloud #DevOps #CloudComputing