bg

Mastering Shell Scripting- Cheat script to automation


Posted on September 7, 2023
Tricks
Shell

Intro

Bash is a powerful scripting language used in the Linux terminal to automate tasks and perform various operations. Here are some essential commands and concepts to help you get started with Bash scripting.

Creating a script:

Create a file name whatever you want. But, make sure it ends with .sh. Open that file in your favorite text editor and write the first line to begin your shell script journey.


_10
#! /bin/bash

The #! /bin/bash line is called the shebang or hashbang, and it is used to specify the interpreter to be used for executing the script. In this case, #!/bin/bash indicates that the script should be interpreted and executed using the Bash shell.

Running the script:

Open your terminal and go to the directory where you created the script file. Then run the below command to execute the script


_10
./my_script.sh

At this stage, you won't see any output since we have not written anything. Let's add some fun stuff.

Display statements:

You can use the echo command to print statements or messages in the terminal. For example:


_10
echo Hello World

You will see Hello World in your terminal

Variables:

You can assign values to variables in Bash using the assignment operator (=). Variable names are usually uppercase by convention and can contain letters, numbers, and underscores. Here's an example:


_10
NAME="Kite"
_10
echo "My name is $NAME"

User input:

To read user input in Bash, you can use the read command. It prompts you to enter a value and stores it in a variable. For example:


_10
read -p "Enter your name: " NAME
_10
echo "Hello $NAME, nice to meet you!s"

Gives you the following output.


_10
Enter your name: Kite
_10
Hello Kite, nice to meet you!s

Conditional Statements:

Bash supports various conditional statements to control the flow of your script. Here are a few examples:

Simple If Statement:


_10
if [ "$NAME" == "Kite" ]
_10
then
_10
echo "Your name is Kite"
_10
fi

If Statement with Else:


_10
if [ "$NAME" == "Kite" ]
_10
then
_10
echo "Your name is Kite"
_10
else
_10
echo "Your name is not Kite"
_10
fi

Else If Statement:


_10
if [ "$NAME" == "Kite" ]
_10
then
_10
echo "Your name is Kite"
_10
elif [ "$NAME" == "Jack" ]
_10
then
_10
echo "Your name is Jack"
_10
else
_10
echo "Your name is NOT Kite or Jack"
_10
fi

Below is an example of comparing two numbers using -gt operator in the shell script


_10
NUM1=3
_10
NUM2=5
_10
if [ "$NUM1" -gt "$NUM2" ]
_10
then
_10
echo "$NUM1 is greater than $NUM2"
_10
else
_10
echo "$NUM1 is less than $NUM2"
_10
fi

© Ari1009. All rights reserved.