SHELL SCRIPTING EXAMPLE SET 1

Shell Script

Shell Script

In this “Shell Script Example Set 1″, You can learn different programming commands in LINUX Shell. If you are a beginner, then go to “Shell Script Example Set 2” directly. This article covers File related scripts, date-related scripts, calculator related scripts, directory related scripts.

The Linux command line is a text interface to your computer. Also known as shell, terminal, console, command prompts and many others, is a computer program intended to interpret commands.

SHELL SCRIPTING EXAMPLE SET 1

In this example set, You can familiar with vi Editor Commands. A table containing commands, descriptions and examples are given below.

vi Editor Commands

COMMANDSDESCRIPTIONSEXAMPLE
viTo create a shell script through vi editorvi first.sh
shTo execute the shell scriptsh first.sh
bashTo execute the shell scriptbash first.sh
./To execute the shell script./ first.sh
iTo insert text into vi editorEsc i
wq or xTo save & exit from editor to terminalEsc :wq or 😡
qTo quit editing mode with no changesEsc :q
!qTo quit without savingEsc: !q
wTo save & remain in editing modeEsc :w
ddTo delete entire linedd
OPERATORTYPESWITCH
!Logical NOT!
Less thanRelational-lt
Less than equal toRelational-le
Greater thanRelational-gt
Greater than equal toRelational-ge
Equal toRelational-eq
Not Equal to Relational-ne
ANDLogical and (True if all conditions are true)-a
ORLogical or (False if all conditions are false)-o
Null Stringthat has zero length-z
FILE OPERATIONS
True if file exists and has size > zero-s filename
True if file exists and is not a directory-f filename
True if a directory exists-d filename
True if file exists and has read permission-r filename
True if file exists and has write permission-w filename
True if file exists and has execute permission-x filename
Vi editor Commands

When Run commands within echo then use Grave Accent sign placed above the tab button.

1> Script to welcome the user who logged into the system.

clear
echo “Hello”
echo “whoami | cut –d “ “ –f1
echo “Welcome to Ubuntu Linux”

Output:

Hello
saif
Welcome to Ubuntu Linux

2> Script to welcome the user and display login date and time.

clear
echo “Hello”
echo “whoami | cut –d “ “ –f1
echo “Welcome to Ubuntu Linux”
echo “The current date and time: “
date

Output:

Hello
saif
Welcome to Ubuntu Linux
The current date and time:
Friday 25 December 2020 11:31:37 AM IST

3> Shell script to show use of variables.

clear
name=”Saif”
subject=”Computer science”
echo “I am $name”
echo “I like to study $subject.”

Output:

I am Saif
I like to study Computer science.

SHELL SCRIPT
SHELL SCRIPT

4> Shell script to accept value of variables form user.

clear
echo -n “Enter your name: “
read name
echo -n “Enter your favorite subject: “
read subject
echo “I am $name and I like to study $subject”

Output:
Enter your name: Saif
Enter your favorite subject: DBMS
I am Saif and I like to study DBMS

5> Shell script to display total number lines in a file.

clear
echo “Enter file name: “
read fname
echo “There are cat $fname | wc -l lines in $fname file.”

Output:
Enter file name:
datep2.sh
There are 6 lines in datep2.sh file.

6> Shell script to find the size of a file.

clear
echo “Enter file name: “
read fname
echo “There are cat $fname | wc -c Bytes.”

Output:
Enter file name:
datep2.sh
There are 106 Bytes.

7> Script to addition of two numbers.

clear
echo “Enter first number: “
read num1
echo “Enter second number: “
read num2
sum=expr $num1 + $num2
echo “The addition of $num1 and $num2 is $sum.”

Output:
Enter first number:
4
Enter second number:
5
The addition of 4 and 5 is 9.

Use other mathematical operators like Subtraction ( – ) , division ( / ), multiplication ( * ), and for remainder ( % ).

8> Script to calculate age of user in years.

clear
echo “Enter year of your birth: “
read byear
cyear=date +"%Y"
age=expr $cyear - $byear
echo “You are $age years old as of today.”

Output:
Enter year of your birth:
1998
You are 22 years old as of today.

9> To display the date and time in the given format:-
“Today is December 15, 2019 and current time is 12:10:23”.

clear
echo “Today is date +"%B%d,%Y" and current time is date+"%T".”

Output:
Today is December25,2020 and current time is 12:04:03.

10> To display the login details of current user in the following format.
Name of the user :
Login date:
Login time :

clear
echo “Name of the user : whoami
echo “Login date : date +"%D"
echo “Login time : date +"%T"

Output:
Name of the user : saif
Login date : 12/25/20
Login time : 12:12:54

11> To display the date, time, user name and current directory.

clear
echo “Current date is date +"%d"
echo “Current time : date +"%T"
echo “User name is whoami
echo “Current directory is pwd

Output:
Current date is 25
Current time : 12:21:05
User name is saif
Current directory is /home/saif

12> To accept two file names from the user and creates new file containing the contents of both the files provided as input.

clear
echo “Enter two file names “
read a b
echo “Enter new file name “
read c
cat $a $b > $c
echo “$c file created successfully”
echo “to display the contents of $c file is “
cat $c

Output:
Enter two file names
addp7.sh datep2.sh
Enter new file name
file12.sh
file12.sh file created successfully
to display the contents of file12.sh file is
clear
echo “Enter first number: “
read num1
echo “Enter second number: “
read num2
sum=expr $num1 + $num2
echo “The addition of $num1 and $num2 is $sum.”
clear
echo “Hello”
echo “whoami
echo “Welcome to Ubuntu Linux”
echo “The current date and time: “
date

13> To accept two file names from the user and compare them. (without if condition)

clear
echo “Enter two file names “
read a b
echo cmp $a $b

Output:
Enter two file names
datep2.sh newfilep12.sh
datep2.sh newfilep12.sh differ: byte 13, line 2

14> Script to find out how many processes user is running.

clear
echo -n “Enter user name “
read username
cnt=ps -ef
echo “User $username is running $cnt processes”

Output:
Enter user name saif
User saif is running
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 13:11 ? 00:00:02 /sbin/init splash noautomount
root 2 0 0 13:11 ? 00:00:00 [kthreadd]

15> Script to find out how may terminals a user has opened.

clear
cnt=who
echo “User $user has opened $cnt terminals”

Output:
User saif has opened saif tty7 2020-12-25 13:25 (:0) terminals
user=”saif”

16> Script to display welcome message to the user.

clear
hour=date +"%H"
username=whoami
if [ $hour -ge 0 -a $hour -lt 12 ]
then
echo “Good Morning $Username, Welcome to Ubuntu Linux Session”
elif [ $hour -ge 12 -a $hour -lt 18 ]
then
echo “Good Afternoon $Username, Welcome to Ubuntu Linux Session”
else
echo “Good Evening $Username, Welcome to Ubuntu Linux Session”
fi

Output:
Good Afternoon , Welcome to Ubuntu Linux Session

17> Script to create a directory with appropriate message.

clear
echo -n “Enter directory name: “
read dir
if [ -d $dir -o -f $dir ]
then
echo “A file or directory with the name $dir already exists.”
else
mkdir $dir
echo “Directory with name $dir created successfully.”
fi

Output:
Enter directory name: Ultra
Directory with name Ultra created successfully.

18> Script to create a new file.

clear
echo “Enter a file name: “
read fn
if [ -r $fn ]
then
echo “$fn file already exists”
else
cat >$fn
echo “$fn file created successfully”
fi

Output:
Enter a file name:
datep2.sh
datep2.sh file already exists

If the file does not exists than type some contents and press ctrl + d. After this file created
successfully message to be displayed on screen.

19> Script to copy file into another file.

clear
echo “Enter your source file name: “
read sf
echo “Enter destination file name: “
read df
if [ -r $sf ]
cp $sf $df
then
echo “$sf file copied successfully”
else
echo “$sf file does not exists”
fi

Output:
Enter your source file name:
datep2.sh
Enter destination file name:
datep2.sh
cp: ‘datep2.sh’ and ‘datep2.sh’ are the same file
datep2.sh file does not exists

Is Linux a Command Line or GUI ?

An operating system like UNIX has CLI While an operating system like Linux and windows have both CLI and GUI.

How do I type vi?

  1. To enter vi, type: vi filename <Return>
  2. To enter insert mode, type: i.
  3. Type in the text: This is easy.
  4. To leave insert mode and return to command mode, press: <Esc>
  5. In command mode, save changes and exit vi by typing: :wq <Return> You are back at the Unix prompt.

How do I run Script?

To run a Shell Script, We can use “sh filename.sh” or “bash filename.sh”. Given in the table above.

Here discussed File related scripts, date-related scripts, calculator related scripts, directory related scripts. You might like, https://shoutcoders.com/sql-preparation-note-set-1

More Shell Scripts related articles are published. You might like, https://www.shoutcoders.com/shell-scripting-example-set-2/

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami