~:Shell Scripting Tutorial:~

~:Shell Scripting Tutorial:~

Script for echoing a message:

#!/bin/bash
echo "Hello! how are you?"
echo "hey! I'm fine."

Script for creation of dir & files under the dir:

#!/bin/bash
mkdir new_folder
cd /home/ubuntu/shell_script/new_folder
echo "this is the new file" > new_file.txt

Script for Date & Time:

#!/bin/bash
date | awk '{print $1,$4}'

#!/bin/bash

echo "The date is:"     
date | awk '{print $3,$2,$6}'

echo "The time is:"
date | awk '{print $1,$4}'

Variable in shell scripting:

#!/bin/bash

name="Santosh"
echo "My name is : $name"

#!/bin/bash

cloud="AWS"
env="dev"
echo "I am working with : $cloud and environment is $env."

#!/bin/bash

cloud="AWS"
env="dev"
echo "I am working with : $cloud and environment is $env"
echo "-----------Welcome to $cloud -------------------"
echo "We are using $SHELL"
echo "------------------"
echo "We are at $HOME and time is" && date

#!/bin/bash

cloud="AWS"
env="dev"
echo "I am working with : $cloud and environment is $env"

echo "-----------Welcome to $cloud -------------------"

echo "We are using $SHELL"

echo "------------------"

day=$(date)
echo "We are at $HOME and today is $day"
#HOME is predefined variable & date is a function.

File search script(using if statement):

#!/bin/bash

echo "Enter the file name"

read filename

if [ -f $filename ]
then
        echo "File $filename exist"
else
        echo "File $filename does not exist"
fi

Passing single argument with command:

#!/bin/bash

if [ -f $1 ]
then
        echo "File $1 exist."
else
        echo "File $1 not exist."
fi

Passing 2 argument with command:

#!/bin/bash

if [ -f $1 ]
then
        echo "File $1 exist."
else
        echo "File $1 not exist."
fi

echo "This is 2nd arguent $2"

For loop:

#!/bin/bash

for (( i=1; i<=5; i++))

do 
        echo "$i"
done

Taking input from user:

#!/bin/bash
echo "Enter the no:"
read num


echo "numbers in ascending order:"

for (( i=1; i<=$num; i++ ))
do
        echo "$i"
done

Passing as argument:

#!/bin/bash
echo "Enter the no:" $1


echo "numbers in ascending order:"

for (( i=1; i<=$1; i++ ))
do
        echo "$i"
done

Disk-free script:

#!/bin/bash

df -h | awk '{print $1,$3}'

Free memory of system script:

#!/bin/bash

echo "free memory of the system is :" && free -h | xargs | awk '{print $10 "/" $8}'

Automation Project : Disk Status

#!/bin/bash 

echo "Today is:" && date;
echo "##################"

echo "The uptime of system is:" && uptime
echo " **********************"

echo "Currently connected users list is:"
who
echo "++++++++++++++++ "

echo "Last login:" && last -a| head -3
echo "============== "

echo "Disk & Memory Usage:"
df -h | xargs | awk '{print "Free/Total disk:" $11 "/" $9}'
free -m | xargs | awk '{print "Free/Total memory:" $10 "/" $8 }'
echo "--------------------------------------------------------------- "

echo "Utilization & Most expensive process is :"
top -b | head -3

Automation : Shell Script for temp file deletion:

#!/bin/bash

# Script name script.sh
# Script for removing all temporary files from temporary directory

TMP_DIR="/var/tmp"
# This is the tmp directory location.
echo "Removing all temporary files from $TMP_DIR"

# Counting the number of temporary files
files=`ls -l $TMP_DIR | wc -l` 

echo "There are total $files temporary files/directory in $TMP_DIR"

rm -rf $TMP_DIR/*

if [[ "$?" == "0" ]];then
    echo "All temporary files successfully deleted"
else
    echo "Failed to delete temporary files"
fi

# Counting the number of temporary files
files=`ls -l $TMP_DIR | wc -l` 

echo "There are total $files temporary files/directory in $TMP_DIR directory"

wc Command:

The wc command in Linux is a command line utility for printing newline, word and byte counts for files. It can return the number of lines in a file, the number of characters in a file and the number of words in a file.

Thank you for reading!Happy Learning!!

Santosh Chauhan

Did you find this article valuable?

Support Santosh Chauhan's blog by becoming a sponsor. Any amount is appreciated!