Linux Bash Shell Scripting

Basics about shell scripting

What is Bash Scripting?

Bash scripting is used in DevOps to automate the task and reduce the time consumption of repetitive tasks.

Example: Creating a Virtual Environment using Bash scripting

Now Setup the VM (EC2) for the Bash Scripting

What is #!/bin/bash? can we write #!/bin/sh as well?

The #! sequence is known as the shebang and is used to specify the interpreter for the script. The /bin/bash part specifies that the script should be interpreted using the bash shell. you can use #!/bin/sh instead, which specifies that the script should be interpreted using the sh shell. Though syntax and features might be different in the sh shell compared to the bash shell.

First Script

Checking System information

#!/bin/bash
echo "I will complete the #90DaysOofDevOps challenge"
echo
echo "Uptime of the System is : "
uptime
echo "Memory Utilization : "
free -m
echo "Disk Utilization :"
df -h

Now it's time to run the script

To run the script you will have to give the execution permission

chmod +x firstscript.sh

Enter the following command in the terminal to run the script

# To run the script
./scriptname
# . represent the current directory

Second Script

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

#!/bin/bash

# Prompt the user for input
echo "Enter a value: "
read userInput

# Get the first argument passed to the script
arg1=$1
agr2=$2
# Print the user input and argument
echo "User input: $userInput"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"

Save the script file and run the script

Output

Third Script

Write an Example of If else in Shell Scripting by comparing 2 numbers

#!/bin/bash

# Prompt the user for two numbers and store them in variables
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2

# Compare the two numbers using an if-else statement
if [ $num1 -gt $num2 ]
then
    echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]
then
    echo "$num1 is less than $num2"
else
    echo "The two numbers are equal"
fi

Save the Script and run the script

Output

Forth Script

Deploying Web Template on EC2 Instance

  • Create EC2 Instance (Amazon Linux Preferred ) or a VM box

  • In Security Group Access to Port 22 and Port 80 should be allowed

Variable

A variable is a temporary store for a piece of data.

There are two actions we can perform using variables:

  • Setting a value for a variable

  • Reading or using the value for a variable

#Storing a variable 
Skill = "Cloud Computing"
#To print
echo $Skill

Command Line Arguments

Command line arguments are values passed to a command or script when it is executed. In Bash, the arguments are passed as strings separated by spaces after the command or script name. For example, consider the following command:

$ ls -l /home/user/Documents

In this command, ls is the command, -l is the first argument, and /home/user/Documents is the second argument.

To access the command line arguments in a Bash script, you can use the variables $1, $2, $3, etc., where $1 refers to the first argument, $2 refers to the second argument, and so on. You can also use the special variable $0 to refer to the script name itself.

For example, consider the following Bash script:

#!/bin/bash

echo "The script name is $0"
echo "The first argument is $1"
echo "The second argument is $2"

If you run this script with the command ./script.sh arg1 arg2, it will output:

Note : ) 0 is always the name of the script

Command Line Argument is $0 to $9

Task: Deploy the template using Command LIne Arguments

System Variables

There are a few other variables that the system sets for you to use as well.

$0 - The name of the Bash Script.

$1 - $9 - The first 9 arguments to the Bash script.

$# - How many arguments were passed to the Bash Script?

$@ - All arguments supplied to the bash script

$? - The exit status of the most recently run process

$$ - The process ID of the current script

$USER - The username of the user running the script.

$HOSTNAME - The hostname of the machine the script running on.

$SECONDS - The number of seconds since the script was started.

$RANDOM - Returns a different random number each time is it referred to.

$LINENO - Returns the current line number in the Bash Script.

Quotes

Storing a single word in a variable works fine without quotes but if we want to store a sentence and also want to store special characters like $,%,@ etc our normal variable assignment will not work.

Virus="Covid19"
echo "Due to $Virus virus country has suffered $10 Billion Loss."

Output

If you look at the output the given output is not correct so as it is it treats $10 as a special character. so whenever we have this situation we can put backward \ ahead of the $ then it will not treat this as a special character.

Command Substitution

In Bash scripting, command substitution can be done using the backtick (` ` ) or the $() syntax. The syntax for command substitution is:

#Storing Variable
Up="uptime"
#it will only store the uptime command in Up variable but it will not execute it.
echo $Up

#For the execution we hae to use backtick
Up=`uptime`
echo $Up
#it will execute the command

Checking the Free Memory of the VM

Free_Ram=`free -m |grep Mem |awk '{print $4}'`
echo $Free_Ram

Output

Checking Number of Files in System

num_files=$(ls | wc -l)

Output

Task: Printing System Information

Exporting Variables

Did you find this article valuable?

Support Know Cloud by becoming a sponsor. Any amount is appreciated!