Shell Scripting in Linux
Steps to start your scrtipt:-
Step1: Check your current working directory by executing pwd
Step2: Create a directory named Scripts(any directory you can
make)
Step 3: Check your terminal by running the echo $0 command
Output should be -bash.
Step 4: Now start writing your first script using vi editor or touch.
File extension – “.sh” ( preferred – vi filename.sh )
Type “vi filename.sh”
Please make sure that vi editor should be in insert mode if not
press i
#!/bin/bash – to be included at the top of every script you will
make.
Type echo Hello, Welcome to Shell Scripting!
Press Esc key and then type :wq to save and exit
Checking the output:
Condition:
First check the contents using ls -lt , Initially you only have the
read and write permission…..
You need to add permission to user to execute also….
Do this by executing the following command:-
chmod u+x filename.sh
For checking the granted permission again hit
ls -lt or ls -ltr
The first column should be similar to this –
“-rwxr--r-- 1 <username> <username> 30 Feb 25 22:49
helloWorld.sh “
For loop program-
#!/bin/bash
count=1
num=5
while [ $count -le $num ]
do
echo Numbers are $count
let count++
done
Output-
Number is 1
Number is 2
Number is 3
My task is Coding
My task is Browsing
My task is Calling
My task is Sleeping
While loop program-
#!/bin/bash
count=1
num=5
while [ $count -le $num ]
do
echo Numbers are $count
let count++
done
Output-
Numbers are 1
Numbers are 2
Numbers are 3
Numbers are 4
Numbers are 5
For running the script, type
./filename.sh
If-else Program-
#!/bin/bash
echo Hi User!
echo What is your name?
read name
echo Hey $name, Welcome to Online Portal for
Elections Status...
echo Please enter your age:
read age
if [ $age -ge 18 ]
then
echo You can vote!
else
echo Sorry $name, You cannot vote!
Fi
Output-
Hi User!
What is your name?
Garvit
Hey Garvit, Welcome to Online Portal for Elections
Status...
Please enter your age:
19
You can vote!
Variables Program-
#!/bin/bash
name=Garvit
age=19
echo My name is $name and my age is $age
Output-
My name is Garvit and my age is 19
Switch-case Program-
#!/bin/bash
echo Hi, Welcome to E-Learning Platform for Linux
echo Enter your name:
read name
echo Welcome $name, Please choose any option from
below:
echo
echo 1 = To print the current date
echo 2 = To list all the files in current directory
echo 3 = Show current path
echo
read choice
case $choice in
1)date;;
2)ls -ltr;;
3)pwd;;
*)echo Invalid choice detected....Exitting the
program..Bye!!
esac
Output-
If we enter the choice – 1
Hi, Welcome to E-Learning Platform for Linux
Enter your name:
Garvit
Welcome Garvit, Please choose any option from
below:
1 = To print the current date
2 = To list all the files in current directory
3 = Show current path
1
Mon Feb 27 10:08:24 IST 2023
If we enter the choice – 2
Hi, Welcome to E-Learning Platform for Linux
Enter your name:
Garvit
Welcome Garvit, Please choose any option from below:
1 = To print the current date
2 = To list all the files in current directory
3 = Show current path
2
total 0
-rwxr--r-- 1 chugh09 chugh09 30 Feb 25 22:49 helloWorld.sh
-rwxr--r-- 1 chugh09 chugh09 73 Feb 25 22:54 variables.sh
-rwxr--r-- 1 chugh09 chugh09 94 Feb 25 22:57 linux_commands_printing.sh
-rwxr--r-- 1 chugh09 chugh09 169 Feb 25 23:01 user_input.sh
-rwxr--r-- 1 chugh09 chugh09 253 Feb 25 23:45 if_else.sh
-rwxr--r-- 1 chugh09 chugh09 407 Feb 25 23:58 cases_script.sh
-rwxr--r-- 1 chugh09 chugh09 138 Feb 26 00:38 for_loop.sh
-rwxr--r-- 1 chugh09 chugh09 100 Feb 26 00:48 while_loop.sh
-rw-r--r-- 1 chugh09 chugh09 46 Feb 26 01:02 Friends
-rwxr--r-- 1 chugh09 chugh09 125 Feb 26 01:04 file_iteration.sh
If we enter the choice – 3
Hi, Welcome to E-Learning Platform for Linux
Enter your name:
chugh
Welcome chugh, Please choose any option from
below:
1 = To print the current date
2 = To list all the files in current directory
3 = Show current path
3
/home/chugh09/Scripts`
If we enter the choice – 4(Other choice)
Hi, Welcome to E-Learning Platform for Linux
Enter your name:
Garvit
Welcome Garvit, Please choose any option from
below:
1 = To print the current date
2 = To list all the files in current directory
3 = Show current path
4
Invalid choice detected....Exitting the
program..Bye!!