Introduction to Shell Scripting

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,
what is shell script: In the simplest terms, a shell script is a file containing a series of commands.The shell reads this file and carries out the commands as through they have been entered directly on the command line.
- Normally the shell script has the file extension.sh
- To create shell script you use a text editor.There are many text editor available for your linux system,both for the command line enviroment and the gui enviroment.Here is the list of common ones.
1) vi or vim is the command line interface text editor
2) gedit is the gui text editor
Following steps are required to write shell script
step 1) Use ant editor like vi or gedit to write shell script.
syntax: vi <script file name>
example: vi hello.sh
step2) After writing shell script set execute permission for your script as follows
syntax: chmod <permission> <script file name>
example: chmod <permission> <script file name>
chmod 777 <script file name>
- chmod 777
- 7 ------> myself(root)
- 7 ------> mygroup
- 7 ------> everyone
- let's look at how 7 is divided to set a file permisson:
- 4 ---->Read, 2----> Write, 1-----> Execute
- Shell Scripting:
#! /bin/bash
echo "Hello world"
echo "Our shell name is $BASH"
echo "our shell version $BASH_VERSION"
echo "our home directory is $HOME"
echo "our current working directory is $PWD"
# echo command
echo "Hello world"
# VARIABLE# uppercase by convention# Letters, Number, Underscore
NAME="Brad"
echo "My name is $NAME"
# user input
read -p "Enter your name: " NAME
echo "Hello $NAME, nice to meet you"
- If condition:
NAME="Brad"
if [ "$NAME" == "Brad" ]
then echo "Your name is Brad"
fi
Output: Your name is Brad
- If-else condition:
NAME="jack"
if [ "$NAME" == "Brad" ]
then
echo "Your name is Brad"
else
echo "Your name is not Brad"
fi
output: Your name is not Brad
else-if (elif):
NAME="jack"
if [ "$NAME" == "Brad" ]
then
echo "Your name is Brad"
elif [ "$NAME" == "jack" ]
then
echo "Your name is jack"
else
echo "Your name is not Brad or Jack"
fi
output:Your name is jack
#COMPARISON
# val1 -eq val2 Returns true if the value are equal
# val1 -ne val2 Returns true if the value are not equal
# val1 -gt val2 Returns true if the val1 is greater than val2
# val1 -ge val2 Returns true if the val1 is greater than or equal to val2
# val1 -lt val2 Returns true if val1 is less than val2
# val1 -le val2 Returns true if val1 is less than or equal to val2
NUM1=3
NUM2=5
if [ "$NUM1" -gt "$NUM2" ]
then
echo "$NUM1 is greater than $NUM2"
else
echo "$NUM1 is less than $NUM2"
fi
output:$NUM1 is less than $NUM2"
# file condition
# -d file True if the file is directory
# -e file True if the file exist
# -r file True if the file is readable
# -w file True if the file is writable
# -x file true if the file is executable
FILE="test.txt"
if [ -f "$FILE" ]
then
echo $FILE is a file
else
echo $FILE is not a file
fi
output:$FILE is a file
#CASE STATEMENT
read -p "Are you 21 or over? Y/N " ANSWEAR
case "$ANSWEAR" in
[yY] | [yY][eE][sS])
echo "you can have a beer :)"
;;
[nN] | [nN][oO])
echo "sorry no drinking"
;;
*)
echo "please enter y/yes or n/no" ;;
esac
# Simple for loop:
NAMES="Brad Kevin alice mark"
for NAME in $NAMES
do
echo Hello $NAME
done
- Renaming the file:
FILES=$(ls *.txt)
NEW="new"
for FILE in $FILES
do
echo "Remaining $FILE to new-$FILE"
mv $FILE to $NEW-$FILE
done
- While loop:
LINE=1
while read -r CURRENT_LINE
do
echo "$LINE: $CURRENT_LINE"
((LINE++))
done < "./new1.txt"
#function
function sayHello() {
echo "Hello world"
}
sayHello
output: Hello world
- function with parameter
function greet() {
echo "Hello i am $1 and i am $2"
}
greet "Brad" "36"
output: Hello i am Brad and i am 36




