0% found this document useful (0 votes)
119 views113 pages

Linux and Shell Programming Lab-1

The document demonstrates creating a college management system directory structure and files using Linux file and directory commands. Key elements created include subdirectories for staff, students, courses, labs, and library under the parent college directory. Files are created to manage employee names, student details, course durations, lab durations, and books in each department. The commands used include mkdir, cd, and vi to navigate directories and create/view files.

Uploaded by

ABHIJITH DAS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
119 views113 pages

Linux and Shell Programming Lab-1

The document demonstrates creating a college management system directory structure and files using Linux file and directory commands. Key elements created include subdirectories for staff, students, courses, labs, and library under the parent college directory. Files are created to manage employee names, student details, course durations, lab durations, and books in each department. The commands used include mkdir, cd, and vi to navigate directories and create/view files.

Uploaded by

ABHIJITH DAS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 113

INDEX

Sl no Date Experiment Name Page no


SET 1 COMMANDS
1 22/6/2020 Installation of Linux Operating system 1
2 22/6/2020 Demonstrate a College Management system 3

3 25/6/2020 Execute the commands of vi Editor. 6


4 25/6/2020 Practice the Filter commands in Linux 8

5 29/6/2020 How to execute Redirection and pipes in Linux. 14


6 2/7/2020 Implement File Administration commands 16

7 2/7/2020 Execute the Disk related commands 19

8 6/7/2020 Illustrate the communication commands 24


SET 2 SHELL SCRIPT
A script to read any 2 floating values and find the sum,
9 6/7/2020 difference, quotient, and remainder. 26
Calculate the area and perimeter of rectangle and area and
10 9/7/2020 circumference of circle. 29
Shell program to find Sum of digits, Reverse of the number
11 13/7/2020 and check given number is palindrome or not 32
Shell script to display the digits which are in odd position
12 13/7/2020 in a given integer. 40
Script to read the basic salary of n employees and calculate
13 13/7/2020 the gross salary. 42
Script to read the cost and selling price of an item and to
14 16/7//2020 decide how much loss or profit has incurred by the seller. 44

15 16/7/2020 Write a script to read 5 marks of n students. 46


Script to read a character and to display if it is lowercase,
16 16/7//2020 uppercase, digit or special character or not a character. 49
Script to prepare multiplication table of a given number to
17 23/7 /2020 any order. 51
18 23 /2020 Script to find the value of one number rose to the power. 53

19 23 /2020 Script to print all prime numbers from 1 to n. 55

20 23 /2020 Script to generate all combinations of a, b and c. 57

21 27/7/2020 Shell script to sum up the following series 1/1! + 2/2! + 3/3! 59

22 27/7/2020 Leap year or not 62

23 27/7/2020 Operations in a file 64

24 30/7/2020 Menu driven 68

25 30/7/2020 How many terminals this user logged in 71

26 30/7/2020 Append the text to the given filename 73

27 3/8/2020 File Permission 75

28 3/8/2020 File’s contents are same or not 77

29 3/8/2020 File already exist or not 80

30 3/8/2020 Student data 82

31 3/08/2020 Bubble sort 86

32 06/08/2020 Factorial using function 90

33 06/08/2020 String palindrome or not 92

34 10/08/2020 Rename files 95

35 10/08/2020 File Searching 97

36 10/08/2020 Receive file name as arguments 100

37 13/08/2020 Command line argument 103

38 13/08/2020 Number of file names 106

39 13/08/2020 Wish the user 109


Program no: 1
Date: 22/6/2020

Installation of Linux Operating system

AIM: Installation of Linux Operating system

1. COMPUTER ASSEMBLING
 Plug or connect all hardware devices in Mother board (mother PCB (Printed Circuit
Board)) properly.
Processor, Hard drive, Floppy Drive, CD Drive, RAM, Display card, Sound card,
Network card etc….
 Trouble shoots the system for its proper working.

2. STARTUP DISK OR BOOT DISK


Creating a Boot Disk
 To install from a CD-ROM, You would require a boot disk. To create a boot disk under
MS DOS, you can use the rewrite utility ,which included in the Linux CD under dosutils
directory. To create boot disk, we will need a blank floppy.

 Steps for creating boot disk


 E:\>cd \dosutils
 E:\dosutils>rawrite
 Enter disk image source file name:\image\boot.img
 Enter target diskette drive: a:
 Please insert a formatted diskette into drive A and Press <Enter>

3. Hard Disk Installation


Booting
Booting is the process which loads the operating system into RAM.

Partition naming schemes


For partitions

/dev/xxyN
 /dev/ - name of the directory in which all device files are stored
 xx - type of device on which partition is created. hd( for IDE) or sd (for SCSI disk)

1
 y - The device on which the partition is present.
 /dev/hda indicates first hard disk. So it is first ide. If it is b, c means second and
third respectively.
 N denotes partition number. Here 1-4 for primary and logical drives starts from 5.
 Dev/hda5-first logical drive of first ide hard disk
 Dev/sdb1-first primary partition of second scsi hard disk
 Dev/hdc2-second primary partition of third id

4. Linux installation

Number of Partitions needed for installation


While installing Linux, you might have to create one or more partitions on the hard disk.
 Swap Partition- For virtual memory. This partition is utilized when the physical memory
is not sufficient.

 /boot partition. It contains operating system kernel. It is always be the double the size of
RAM.
 Root partition: Partition which root directory (/) is located. All the files of the system
except boot files are stored.

Installation methods
 Local CD-ROM
 NFS Image
 FTP and HTTP
 Hard Drive

Installation Classes
Workstation
Server
Laptop
Custom

RESULT
Linux installed successfully.

2
Program no: 2
Date: 22/6/2020

College Management system

AIM: Demonstrate a College Management system in Linux using File and Directory commands.

# Make college as a parent directory using mkdir


$mkdir -p /tmp/college
# To Set read/write/execution Permissions
$mkdir –m777 college
# Make the first subdirectory inside college
$mkdir /tmp/college/staff
# Inside the staff make the subdirectory teaching
$mkdir /tmp/college/staff/teaching
# Inside teaching directory make fulltime subdirectory
$mkdir /tmp/college/staff/teaching/fulltime
# Inside the fulltime directory there is a file for managing the staff/employees
$ vi employeenames.sh
# To display the content of the file using cat command
$ cat employeenames.sh
# Back to the teaching directory.
$ cd..
# Inside teaching directory make parttime subdirectory
$mkdir /tmp/college/staff/teaching/parttime
# Inside the parttime directory there is a file for managing the staff/employees.
$ vi employeenames.sh
# Back to the staff directory
$ cd..
$ cd..
# Inside the staff directory make the subdirectory nonteaching
$mkdir /tmp/college/staff/nonteaching
# Inside nonteaching directory make fulltime subdirectory
$mkdir /tmp/college/staff/nonteaching/fulltime
#Inside the fulltime directory there is a file for managing the nonteaching staff/employees
$ vi employeenames.sh
# Back to the nonteaching directory.
$ cd..
# Inside teaching directory make parttime subdirectory
$mkdir /tmp/college/staff/nonteaching/parttime
# Inside the parttime directory there is a file for managing the nonteaching staff/employees.
$ vi employeenames.sh
#Back to the parent directory
$ cd..
$ cd..

3
$ cd..
# Make the second subdirectory inside college
$mkdir /tmp/college/students
# Inside the student directory make the subdirectory departments
$mkdir /tmp/college/students/departments
# Inside the department directory there is a file for managing the students details.
$ vi studentnames.sh
# Back to the parent directory
$ cd..
$ cd..
# Make the third subdirectory inside college
$mkdir /tmp/college/course
# Inside the course directory make the subdirectory coursetype
$mkdir /tmp/college/course/coursetype
# Inside the coursetype directory there is a file for managing the duration of courses.
$ vi courseduration.sh
# Back to the parent directory
$ cd..
$ cd..
# Make the fourth subdirectory inside college
$mkdir /tmp/college/lab
# Inside the lab directory make the subdirectory labtype
$mkdir /tmp/college/lab/labtype
# Inside the labtype directory there is a file for managing the duration of labs.
$ vi labduration.sh
# Back to the parent directory college
$ cd..
$ cd..
# Make the fifth subdirectory inside college
$mkdir /tmp/college/library
# Inside the lab directory make the subdirectory departments
$mkdir /tmp/college/library/departments
# Inside the departments directory there is a file for managing the books in each department.
$ vi books.sh
# Back to the parent directory
$ cd..
$ cd..
# Make the sixth subdirectory inside college
$mkdir /tmp/college/clubs
# Inside the clubs directory make the subdirectory artsclub
$mkdir /tmp/college/clubs/artsclub
# Inside the artsclub directory there is a file for managing the functions and activities in the arts
club.
$ vi functions.sh
# Back to the clubs directory
$ cd..

4
# Inside the clubs directory make the subdirectory sportsclub
$mkdir /tmp/college/clubs/sportsclub
# Inside the sportsclub directory there is a file for managing the functions and activities in the
sports club.
$ vi functions.sh
# Back to the club directory
$ cd..
# Inside the clubs directory make the subdirectory placementcell
$mkdir /tmp/college/clubs/placementcell
# Inside the placementcell directory there is a file for managing the functions and activities in the
placement cell.
$ vi functions.sh
# Back to the club directory
$ cd..
# Inside the clubs directory make the subdirectory iedc
$mkdir /tmp/college/clubs/iedc
# Inside the iedc directory there is a file for managing the functions and activities in the iedc.
$ vi functions.sh
# Back to the parent directory college
$ cd..

RESULT

College management system executed successfully.

5
Program no: 3
Date: 25/6/2020

Visual Editor
AIM: Familiarize and execute the commands of vi Editor.

Creating a File with Vi


vi filename
Example: vi textfile.txt
To create a file in a specific location

vi /path/to/filename
Opening an already existing file
vi /path/to/filename

The Vi Modes

Moving cursor across a file:


1. Move left: h

2. Move right: l

3. Move up: k

4. Move down: j

Common vi Commands
Editing a File
1. i : to insert text before the current cursor location
2. I: to insert text at the beginning of the line
3. a: to insert text after the current cursor location
4. o: to create a new line for text below the current cursor location

Deleting characters
1. x : delete the character under the current location
2. X : delete the character before the current location
3. dw : delete from the current location to the next word
4. D : delete from current location till end of the line
5. dd : delete the entire line

6
Copying and pasting

1. yy : to copy the current line


2. p : to paste the copied text after the cursor
3. P : to paste the yanked(cut) text before the cursor

Changing text

1. cc : to remove contents of the line


2. s : to replace the character with the character you write
3. r : to replace the character under the cursor and return to command mode

RESULT
vi editor commands executed successfully.

7
Program no: 4
Date: 25/6/2020

Filter commands
Aim: Practice the Filter commands in Linux with suitable examples.

1. cat command (concatenate)


1) To view a single file
$ cat filename

2) To view multiple files

$ cat file1 file2

3) Create a file
$ cat>newfile
4) Copy the contents of one file to another file.

$ cat[filename-whose-contents-is-to-be-copied]>[destination-filename]

5. Display content in reverse order using tac command.

$ tac filename

2. The head command


Print the first N number of data of the given input.
head [OPTION]... [FILE]...

$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir

8
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland

Example:
$ head state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir

3. tail command
Print the last N number of data of the given input.
tail [OPTION]... [FILE]...

$ cat state.txt

Kerala
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
$ tail state.txt

9
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal

4. The wc command

wc stands for word count used for counting purpose.


wc [OPTION]... [FILE]...
eg.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

$ wc state.txt
5 7 63 state.txt

5. The cut command


It can be used to cut parts of a line by byte position, character and field.

cut OPTION... [FILE]...


$cat state.txt

Andhra Pradesh

Arunachal Pradesh

Assam

Bihar

Chhattisgarh

$ cut state.txt

10
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.
6. The paste command
It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from
each file specified, separated by tab as delimiter, to the standard output.
paste [OPTION]... [FILES]...
$ cat state
Arunachal Pradesh
Assam
Andhra Pradesh
Bihar
Chhattisgrah

$ cat capital
Itanagar
Dispur
Hyderabad
Patna
Raipur
$ paste number state capital
1 Arunachal Pradesh Itanagar
2 Assam Dispur
3 Andhra Pradesh Hyderabad
4 Bihar Patna
5 Chhattisgrah Raipur

7. The pr command
The pr command formats a file to make it look better when printed.

pr [OPTION]...[FILE]...

8. The grep Command


The grep filter searches a file for a particular pattern of characters, and displays all lines
that contain that pattern.
grep [options] pattern [files]

$cat > geekfile.txt


unix is great os. unix is opensource. unix is free os.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
$grep -i "UNix" geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.

11
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
9. The tr command
The tr command in LINUX is a command line utility for translating or deleting characters
$ tr [OPTION] SET1 [SET2]
$ cat greekfile
Output:
WELCOME TO
GeeksforGeeks
$ cat greekfile | tr “[a-z]” “[A-Z]”
Output:
WELCOME TO
GEEKSFORGEEKS
10. The Sort command
SORT command is used to sort a file.
Examples
$ cat > file.txt
abhishek
chitransh
satish
rajan
naveen
divyam
harsh
$ sort filename.txt
Command:
$ sort file.txt
abhishek
chitransh
divyam
harsh
naveen

12
rajan
satish

11. The uniq Command

uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate
lines.
$uniq [OPTION] [INPUT[OUTPUT]]
$cat kt.txt
I love music.
I love music.
I love music.
I love music of Kartik.
I love music of Kartik.
$uniq kt.txt
I love music.
I love music of Kartik.

RESULT
Filter commands executed successfully

13
Program NO: 5
Date: 29/6/2020

Redirection and pipes in Linux


AIM: How to execute Redirection and pipes in Linux.

Redirection

The process of changing the assignment for the standard input (keyboard), the standard output
(monitor) and the standard error file (monitor) is known as redirection.

Input redirection

Example:

$ cat < test1 <Enter>

Output Redirection
$ ls -l > listings

Example 1:

Test1 contain the string “Very good morning.”

Test2 contain the string “Welcome you all”

$ cat test1 > test 2

Output is “Very good morning”. Here the content of test1 will be overwritten to test2.

Error Redirection
Syntax:

Command 2 >error file

Example
Cat Datafile 2> temp

Piping
It is used in Linux operating systems to send the output of one command/program/process to
another command/program/process for further processing.

command_1 | command_2 | command_3 | .... | command_N

14
Example:
1. Listing all files and directories and give it as input to more command.

$ ls -l | more
2. Use sort and uniq command to sort a file and print unique values.
$ sort record.txt | uniq
This will sort the given file and print the unique values only.

RESULT
Redirection and pipe commands executed successfully.

15
Program No: 6

Date: 2/7/2020

File Administration commands in Linux

AIM: Implement File Administration commands in Linux.

Listing Files

$ls

bin hosts lib res.03


ch07 hw1 pub test_results
ch07.bak hw2 res.01 users
docs hw3 res.02 work

Creating Files

$ vi filename
$ vi filename
 h key to move to the left side.
 k key to move upside in the file.
 j key to move downside in the file.

Display Content of a File

$ cat filename

This is Linux file....I created it for the first time.....


I'm going to save this content in this file.
$

Counting Words in a File

$ wc filename
2 19 103 filename
$

Copying Files

$ cp source_file destination_file
$ cp filename copyfile

16
$

Renaming Files

$ mv old_file new_file

$ mv filename newfile
$

Deleting Files

$ rm filename
Directory management
$cd ~
$
$cd ~username
$
To go in your last directory, you can use the following command −
$cd -
$
Following are some examples of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
command pwd to print the current working directory −
$pwd
/user0/home/amrood
$

Listing Directories
$ls dirname
$ls /usr/local
X11 bin gimp jikes sbin
ace doc include lib share
atalk etc info man ami

Creating Directories
$mkdir dirname
Eg. $mkdir mydir
$
$mkdir /tmp/test-dir
$

17
Removing Directories
$rmdir dirname
$
$rmdir dirname1 dirname2 dirname3
$

Changing Directories
$cd dirname
$
$cd /usr/local/bin
$

Renaming Directories
$mv olddir newdir
$

RESULT

File and directory commands executed successfully.

18
Program no: 7

Date: 2/7/2020

Disk related commands in Linux


AIM: Execute the Disk related commands in Linux

1. fdisk

Fdisk is the most commonly used command to check the partitions on a disk

2. sfdisk

It can display the size of each partition in MB.

19
3. cfdisk

It can be used to list out the existing partitions as well as create or modify them.

4. Parted

Parted is yet another command line utility to list out partitions and modify them if
needed.

20
5. df

Df is not a partitioning utility, but prints out details about only mounted file systems.

6. pydf

Improved version of df, written in python. Prints out all the hard disk partitions in a easy to read
manner.

7. lsblk

List all the storage blocks, which include disk partitions and optical drives.

21
8. blkid
Prints the block device (partitions and storage media) attributes like uuid and file system type..

9. hwinfo

The hwinfo is a general purpose hardware information tool and can be used to print out the disk
and partition list.

22
10. Inxi
Inxi is a very useful command line program that can display information

RESULT

Disk related commands executed successfully.

23
Program no: 8

Date: 6/7/2020

Communication commands in Linux

AIM: Illustrate the communication commands in Linux.

SSH

SSH which stands for Secure Shell.

SSH username@ip-address or hostname

Eg. ls

Ping

This utility is commonly used to check whether your connection to the server is healthy or not.

ping hostname="" or=""

Example: ping 172.16.170.1

FTP

FTP is file transfer protocol. It's the most preferred protocol for data transfer amongst

ftp hostname="" or=""

Telnet

 connect to a remote Linux computer


 run programs remotely and conduct administration
24
 This utility is similar to the Remote Desktop feature found in Windows Machine.

Syntax

telnet hostname="" or=""

Example:
telnet localhost

RESULT

Communication commands executed successfully.

25
Program no: 9
Date: 6/7/2020

Read 2 floating values


AIM: Write a script to read any 2 floating values and find the sum, difference, quotient, and
remainder.

Algorithm
Step1: Start
Step2: Input two numbers (a, b)

Step3: Sum= (a + b)
Step4: Difference= (a - b)
Step5: Quotient = (a * b)
Step6: Remainder= (a % b)
Step7: Display Sum, Difference, Quotient, Remainder
Step8: Stop

Shell Script
#!/bin/bash
echo "Enter two numbers"
read a

read b
sum=$(echo "$a + $b" | bc -l)
difference=$(echo "$a - $b" | bc -l)
quotient=$(echo "$a * $b" | bc -l)
reminder=$(echo "$a / $b" | bc)
echo "The sum of $a and $b is : $sum"

echo "The difference of $a and $b is : $difference"


echo "The quotient of $a and $b is : $quotient"
echo "The reminder of $a and $b is : $reminder"

26
RESULT
Script executed successfully.

27
OUTPUT

28
Program no: 10
Date: 9/7/2020

Area and Perimeter


AIM: Write a script to calculate the area and perimeter of rectangle and area and circumference
of circle.

Algorithm
Step 1: start
Step 2: Input length and breadth (a, b)
Step 3: Area= a + b;
Step 4: Perimeter= 2*(a + b)

Step 5: Input radius(r)


Step 6: circumference= 2*3.14*r
Step 7: Display Area, perimeter and circumference
Step 8: Stop

Shell Script
#!/bin/bash
#For Rectangle
echo -n "Enter the Length of a Rectangle:"
read l
echo -n "Enter the Breadth of a Rectangle:"
read b
# For Circle
echo -n "Enter the Radius of a Circle:"
read r
echo -------------------------------------------------
echo "Area of Rectangle : $(($l*$b))"
echo "Perimeter of Rectangle : $((2*$(($l+$b))))"
pi=3.14
area=$(echo "$pi*$r*$r" | bc -l)
circumference=$(echo "$pi*2*$r" | bc -l)
echo "area of the circle is : $area"
echo "circumference of the circle is: $circumference"
echo ------------------------

29
RESULT

Shell script executed successfully.

30
OUTPUT

31
Program no: 11

Date: 13/7/2020

SHELL PROGRAMS
AIM: - Write a shell program to find
a) Sum of digits of a number
b) Reverse of the number

c) Determine whether the given number is a palindrome or not.

Algorithm
Step 1: start
Step 2: Input a number (num)
Step 3: Assign num to variable g
Step 4: Assign s=0

Step 5: Repeat step 6 until $Num >= 0


Step 6: k=$(( $Num % 10 )) , Num=$(( $Num / 10 )), s=$(( $s + $k ))
Step 7: Print sum of digits [sum]
Step 8: Stop
Shell Script Sum of digits

#!/bin/bash
echo "Enter a number "
read Num

g=$Num
s=0
while [ $Num -gt 0 ]
do
k=$(( $Num % 10 ))
Num=$(( $Num / 10 ))

32
s=$(( $s + $k ))
done
echo "sum of digits of $g is : $s"

RESULT
Shell script executed successfully.

33
OUTPUT

34
Algorithm
Step 1: start

Step 2: Input a number (num)


Step 3: Assign rev=0
Step 4: Assign num to variable temp
Step 5: Repeat step 6 until $num > 0
Step 6: s=$(( $num % 10 )), rev=$(( $rev * 10 + $s )), num=$(( $num / 10 ))
Step 7: Print Reverse of number [rev]

Step 8: Stop

Shell Script Reverse of a number


#!/bin/bash
echo "Enter a number: "
read num
rev=0
temp=$num
while [ $num -gt 0 ]
do
s=$(( $num % 10 ))
rev=$(( $rev * 10 + $s ))
num=$(( $num / 10 ))
done
echo "The reverse of $temp is $rev"

RESULT
Shell script executed successfully.

35
OUTPUT

36
Algorithm
Step 1: start

Step 2: Input a number (num)


Step 3: Assign rev=0
Step 4:
Step 5:
Step 6:
Step 7:

Step 8: Stop

Shell Script Palindrome or not


#!/bin/bash
echo "Enter a number: "
read num
rev=0
temp=$num
while [ $num -gt 0 ]
do
s=$(( $num % 10 ))
rev=$(( $rev * 10 + $s ))
num=$(( $num / 10 ))
done
if [ $temp -eq $rev ]
then

echo " Number is palindrome "


else
echo " The number is not a palindrome "

37
fi

RESULT
Shell script executed successfully.

38
OUTPUT

39
Program no: 12
Date: 13/7/2020

ODD POSITION
AIM: - Write a shell script to display the digits which are in odd position in a given integer.

Algorithm
Step 1: Start
Step 2: Input a number(num)
Step 3: Assign n=1
Step 4: Repeat step 5 until $n<10
Step 5: a=`echo $num | cut -c $n`, echo $a(prints the value of a), n=`expr $n + 2`
Step 6: Stop
Shell Script
s#!/bin/bash

echo Enter a digit number


read num
n=1
while [ $n -le 10 ]
do
a=`echo $num | cut -c $n`

echo $a
n=`expr $n + 2`
done

RESULT
Shell script executed successfully.

40
OUTPUT

41
Program no: 13
Date: 13/7/2020

SALARY OF EMPLOYEE
AIM :- Write a script to read the basic salary of n employees and calculate the gross salary
If BP&lt; 15000, DA=30% of BP, HRA =Rs 500. TA = 10% of BP.
If BP &gt;=5000, DA=50% of BP, HRA=15%, TA=1000.

Algorithm
Step 1: Start
Step 2: Read the number employees(n)
Step 3: repeat for( i = 0; i < $n; i++ )
Step 4: Read the basic salary(bsal)
Step 5: check if $bsal<15000, then calculate gross salary and print the value
Step 6: otherwise,check if $basl>5000, then calculate gross salary and print the value
Step 7:Stop
Shell script

#!/bin/bash
echo "enter the number of employees"
read n
for (( i = 0; i < $n; i++ )); do
echo "enter the basic salary:"
read bsal
if [ $bsal -lt 15000 ]
then
gsal=$((($bsal+500)+($bsal/100*30)))
echo "The gross salary : $gsal"
elif [ $bsal -ge 5000 ]
then
gsal=$((($bsal+$bsal/100*15)+($bsal/100*50)))
echo "the gross salary : $gsal"
fi
done

RESULT
Shell script executed successfully.

42
OUTPUT

43
Program no: 14
Date: 16/7//2020

COST OF SELLING PRICE


AIM: - Write a script to read the cost and selling price of an item and to decide how much loss
or profit has incurred by the seller.

Algorithm
Step 1: Start
Step 2: Read the Cost price (cp)
Step 3: Read the Selling price (sp)
Step 4: check if $sp>$cp, then calculate profit and print the profit value.
Step 5: Otherwise, calculate the loss and print the loss value.
Step 6: Stop

Shell script
#!/bin/bash

echo "Enter the cost price"


read cp
echo "Enter the selling price"
read sp
if [ $sp -gt $cp ]
then

profit=`expr $sp - $cp`


echo "Profit made is $profit"
else
loss=`expr $cp - $sp`
echo "Loss is $loss"
fi

RESULT
Shell script executed successfully.

44
OUTPUT

45
Program no: 15
Date: 16/7/2020

STUDENT REPORT
AIM: - Write a script to read 5 marks of n students .Find the total and return distinction if the
total percentage &gt;= 80. [Distinction] if total % is &gt;=60 and &lt;80 [first class].if total % is
&gt;=50 and &lt;60 [second class] else print failed [&lt;50].

Algorithm
Step 1: Start
Step 2: Read the number of students (n)
Step 3: Repeat for(i=0;i<$n;i++)
Step 4: Read the name of the student (name)
Step 5: Read the marks of 5 subjects (m1,m2,m3,m4,m5)
Step 6: Calculate the sum of marks
Step 7: Print the sum of 5 subjects
Step 8: Calculate percentage
Step 9: Print percentage
Step 10: Check if $per>80, print you get distinction
Step 11: Otherwise, check if $per>60 & $per< 80, then print you get first class
Step 12: Otherwise, check if $per>50 & $per<60, then print you get second class
Step 13: Else print you get fail
Step 14: Stop
Shell script

#!/bin/bash
echo "Enter the number of students:"
read n
for (( i = 0; i < $n; i++ )); do
echo "Enter the name of the student:"
read name
echo "Enter the mark1:"
read m1
echo "Enter the mark2:"
read m2
echo "Enter the mark3:"
read m3
echo "Enter the mark4:"

46
read m4
echo "Enter the mark5:"
read m5
sum1=$(expr $m1 + $m2 + $m3 + $m4 + $m5)
echo "Sum of 5 subjects are: " $sum1
per=$(expr $sum1 / 5 )
echo " Percentage: " $per
if [ $per -ge 80 ]
then
echo "You get Distinction"
elif [ $per -ge 60 -a $per -lt 80 ]
then
echo "You get First class"
elif [ $per -ge 50 -a $per -lt 60 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi
done

RESULT
Shell script executed successfully.

47
OUTPUT

48
Program no: 16
Date: 16/7//2020

READ A CHARACTER
AIM: - Write a script to read a character and to display if it is lowercase, uppercase, digit or
special character or not a character.

Algorithm
Step 1: Start
Step 2: Read a character (c)
Step 3: Check if $c is in the range [a-z], then print lowercase.
Step 4: Otherwise, check $c in range [A-Z], then print uppercase.
Step 5: Otherwise, check $c in range [0-9], then print digit.
Step 6: Otherwise, check $c is a special character, then print special character.
Step 7: Otherwise, print not a character.
Step 6:Stop
Shell script

#!/bin/bash
echo "Type a character"
read c
if [[ "$c" =~ [a-z] ]]; then
echo "lowercase"
elif [[ "$c" =~ [A-Z] ]]; then
echo "uppercase"
elif [[ "$c" =~ [0-9] ]]; then
echo "Digit"
elif [[ "$c" =~ ['!'@#\$%^\&*()_+] ]]; then
echo "Special character"
else
echo "Not a character"
fi

RESULT
Shell script executed successfully.

49
OUTPUT

50
Program no: 17
Date:23/7 /2020

MULTIPLICATION TABLE
AIM: - Write a script to prepare multiplication table of a given number to any order.

Algorithm
Step 1: Start
Step 2: Read the number (n)
Step 3: Assign i=1
Step 4: Repeat step 5 until $i<10
Step 5: Print, $n X $i = `expr $n \* $i and increment i by 1
Step 6:Stop

Shell script

#!/bin/bash
echo "Enter a Number"
read n
i=1
while [ $i -le 10 ]
do
echo " $n X $i = `expr $n \* $i`"
i=$(expr $i + 1)
done

RESULT
Shell script executed successfully.

51
OUTPUT

52
Program no: 18

Date:23 /2020

POWER OF A NUMBER
AIM: - Write a script to find the value of one number raised to the power.

Algorithm
Step 1: Start
Step 2: Read a number (n)
Step 3: Read the power (power)
Step 4: Assign counter=0 and ans=1.
Step 5: Repeat step 6 until $power!=$counter.
Step 6: ans=$(expr $ans \* $n), counter=$(expr $counter + 1)
Step 8: Prints the power of the number ($ans)
Step 7:Stop
Shell script

#!/bin/bash
echo "Enter a number"
read n
echo "Enter the power"
read power
counter=0
ans=1
while [ $power -ne $counter ]
do
ans=$(expr $ans \* $n)
counter=$(expr $counter + 1)
done
echo "$n power of $power is $ans"

RESULT
Shell script executed successfully.

53
OUTPUT

54
Program no: 19
Date: 23/7/2020

PRIME NUMBER
AIM: - Write a script to print all prime numbers from 1 to n.

Algorithm
Step 1: Start
Step 2: Read the limit (num)
Step 3: Repeat step 4 & 6, for(i=2;i<=num;i++)
Set f=0
Step 4: Repeat 6 for(j=2;j<i;j++)
Step 5: check if [‘expr $i % $j` -eq 0], then set f=1
Step 6: check if $f=0, then print $i.
Step 7 : Stop
Shell script

#!/bin/bash
echo "Enter the limit "
read num
echo "Primes of 1 to $num are :"
for((i=2;i<=$num;i++))
do
f=0
for((j=2;j<i;j++))
do
if [ `expr $i % $j` -eq 0 ]
then
f=1
fi
done
if [ $f -eq 0 ]
then
echo " "$i
fi
done

RESULT
Shell script executed successfully.

55
OUTPUT

56
Program no: 20
Date: 23/7/2020

COMBINATION OF a , b and c
AIM: - Write a script to generate all combinations of a, b and c.

Algorithm
Step 1: Start
Step 2: Assign p=a, q=b, s=c
Step 3: Repeat step 4 & 7 , for((i=0;i<3;i++))
Step 4: Repeat step 5 & 6 , for((j=1;j<=2;j++))
Step 5: Print $p,$q,$s
Step 6: Swap the values: t=$q, q=$s, s=$t
Step 7: Swap the values: t=$p, p=$q, q=$s, s=$t
Step 6:Stop
Shell script

#!/bin/bash
p=a
q=b
s=c
for((i=0;i<3;i++))
do
for((j=1;j<=2;j++))
do
echo "$p$q$s"
t=$q
q=$s
s=$t
done
t=$p
p=$q
q=$s
s=$t
done

RESULT
Shell script executed successfully.

57
OUTPUT

58
Program No: 21
Date: 27/7/2020

Sum of series
Aim: Write a shell script to sum up the following series 1/1! + 2/2! + 3/3! + ….
Algorithm
Step 1: Start

Step 2: Enter and read the limit

Step 3: Assign fact=1 and n-0


Step 4: Execute i and j loop
Step 5: n=`echo "scale=4;$n + $i / $fact"|bc

Step 6: Display the sum or else print It is non-negative number

Shell Script
#!/bin/bash

echo "Enter the limit :"


read l
fact=1
n=0
if [ $l -gt 0 ]
then

for((i=1;i<=$l;i++))
do
for((j=$i;j<=$i;j++))
do
fact=$(($fact * $j))
done

n=`echo "scale=4;$n + $i / $fact"|bc`

59
done
echo $n
else
echo "It is a non-negative number"

fi

RESULT

Shell script executed successfully.

60
OUTPUT

61
Program No: 22

Date: 27/7/2020

Leap year or not

AIM: Write a script to read a year and to decide whether it is a leap year or not. If no year is
supplied then the current year is assumed.

Algorithm

Step 1: Start

Step 2: Enter and read year

Step 3: a=$((year%4)), b=$((year%100)), c=$((year%400))


Step 4: if [ $a == 0 -a $b != 0 -o $c == 0 ]
Step 5: Display The year is leap year or no

Step 6: Stop
Shell Script

#!/bin/bash
echo -n "Enter year (YYYY): "
read y
a=$((y%4))
b=$((y%100))
c=$((y%400))

if [ $a == 0 -a $b != 0 -o $c == 0 ]
then
echo "$y is leap year"
else
echo "$y is not a leap year"
Fi
RESULT

Shell script executed successfully.

62
OUTPUT

63
Program No: 23

Date: 27/7/2020

Operations in a file
AIM: Shell script to perform operations like display, list, and make directory and copy, rename,
delete, edit file.

Algorithm
Step 1: Start

Step 2: Enter each operation as choice and read them

Step 3: check each operation

Step 4: Display the operation by selecting the choice

Step 5: Stop

Shell Script
#!/bin/bash
i="y"

while [ $i = "y" ]
do
echo "1. Display the directory"
echo "2. Listing the directory"
echo "3. Make a directory"
echo "4. Copy a file"

echo "5. Rename file"


echo "6. Delete file"
echo "7. Edit file"
echo "8. Exit"
echo "Enter your choice"
read c
case $c in

64
1)echo "Current directory is: "
pwd;;
2)echo "Directories are "
ls;;

3)echo "Enter directory name to create: "


read d
mkdir $d
echo $d " directory is created.";;
4)echo "Enter filename from copy: "
read f1

echo "Enter filename to be copied: "


read f2
# cat $f1 > $f2
cp $f2 $f1
echo $f2 "is copied from " $f1;;
5)echo "Enter file name to rename: "

read f1
echo "Enter new name of the file: "
read f2
mv $f1 $f2
echo $f1 " is renamed as " $f2;;
6)echo "Enter the file name to delete: "

read f
rm $f
echo $f " is deleted successfully";;
7)echo "Enter the filename you wanted to edit: "
read f

65
vi $f;;
8)echo "Thank you...Now you are exiting from the program"
exit;;
*)echo "Invalid input"

esac
echo "Do you want to continue(y/n): "
read i
if [ $i != "y" ]
then
exit

fi
done
RESULT

Shell script executed successfully.

66
OUTPUT

67
Program no: 24
Date: 30/7/2020

Menu driven program


AIM: Write a menu driven program to display the Contents of /etc/passwd, List of output of
‘who’, Present working directory, Exit.

Algorithm
Step 1: Start

Step 2: Enter each operation as choice and read them

Step 3: check each operation

Step 4: Display the operation by selecting the choice

Step 5: Stop

Shell Script
#!/bin/bash
choice=y
while [ $choice = "y" ]
do
echo "1. Contents of etc/passwd"

echo "2. List of the users who have logged in"


echo "3. Present working directory"
echo "4. Exit"
echo "Enter your choice "
read choice
case $choice in

1)echo "Conents of /etc/passwd : "


cat /etc/passwd | more
read a
;;

68
2)echo "List of users currently logged:"
who | more
read a
;;

3)pwd
read a
echo "You are in $a directory";;
4)echo "Thank you...Now you are exiting from the program"
exit;;
*)echo "Invalid input"

esac
echo "Do you want to continue(y/n): "
read choice
if [ $choice != "y" ]
then
exit

fi
done

RESULT

Shell script executed successfully.

69
OUTPUT

70
Program no: 25
Date: 30/7/2020

Terminals
AIM: Write a shell script to find how many terminals this user logged in.

Algorithm
Step 1: Start

Step 2: get the information about currently logged in user by using who command
cur_log=`who | wc -l`

Step 3: Display who is logged.


Step 4: Stop
Shell Script

#!/bin/bash
cur_log=`who | wc -l`

echo "Currently logged : $cur_log"


echo `who`

RESULT

Shell script executed successfully.

71
OUTPUT

72
Program no: 26
Date: 30/7/2020

Append the text to the given filename


AIM: Write a script to accept a filename while running the script and check it is got the write
permission, if yes prompt the user to enter a text and append the text to the given filename.

Algorithm
Step 1: Start

Step 2: enter the filename

Step 3: if [[ -r $f && -w $f ]]
Step 4: Display the file has read write permission
Step 5: enter the content you wanted to add
Step 6: Stop

Shell Script
#!/bin/bash
echo "enter the filenname: "
read f
if [[ -r $f && -w $f ]];
then
echo "the file has read write permission"
echo "enter the content you wanted to add: "
cat >>$f
fi
cat $f
RESULT

Shell script executed successfully.

73
OUTPUT

74
Program no: 27
Date: 3/8/2020

File permissions
AIM: Write a shell script which displays a list of all files in the current directory to which you
have read, write & execute permissions.

Algorithm
Step 1: Start

Step 2: list of File Names in the current directory

Step 3: if [ -f $file]then if [ -r $file -a -w $file -a -x $file ]

Step 4: Display Which have Read, Write and Execute permissions


Step 5: Stop

Shell Script
#!/bin/bash
echo "The list of File Names in the curent directory."
echo "Which have Read,Write and Execute permisions. "

for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then

ls -l $file
fi
fi
done

RESULT
Shell script executed successfully.

75
OUTPUT

76
Program no: 28
Date: 3/8/2020

File’s contents are same or not


AIM: Write a shell script which receives two file names as arguments. It should check whether
the two file’s contents are same or not .If they are same, delete the second file.

Algorithm
Step 1: Start

Step 2: Enter two filenames and read them

Step 3: compare two files cmp $f1 $f2 > error, total=`wc -c error | tr -s " " | cut -d ":" -f4`

Step 4: echo $total


Step 5: if [ $total -le 0 ]
Step 6: Display both file's contents are same or else Both file's contents are not same.
Step 7: Stop

Shell Script
#!/bin/bash

echo "Enter first file name "


read f1
echo "Enter second file name "
read f2
cmp $f1 $f2 > error
total=`wc -c error | tr -s " " | cut -d ":" -f4`

echo $total
if [ $total -le 0 ]
then
echo "Both file's contents are same"
else
echo "Both file's contents are not same"

77
fi

RESULT
Shell script executed successfully.

78
OUTPUT

79
Program no: 29
Date: 3/8/2020

Files already exist or not


AIM: Write a shell script, which will receive any number of filenames as arguments .The shell
script should check whether such files already exist.

Algorithm
Step 1: Start
Step 2: enter the file
Step 3: read f1
Step 4: for i in $f1

Step 5: if [ -f $i ]
Step 6: Display File is already exist or File is not exist
Step 6: Stop

Shell Script
#!/bin/bash
echo "ente a file"

read f1
for i in $f1
do
if [ -f $i ]
then
echo "File $i is already exist."
else

echo "file is not exist."


fi
done

RESULT
Shell script executed successfully.

80
OUTPUT

81
Program no: 30
Date: 03/08/2020

Student data
AIM: Write a shell script to perform operations for student data like view, add and delete
records.

Algorithm
Step 1: Start
Step 2: Enter database name

Step 3: while [$i=’Y], Enter each operation as choice and read them
Step 4: check each operation
Step 5: Display the operation by selecting the choice
Step 6: Stop

Shell script
#!/bin/bash

clear
i="y"
echo "Enter name of database "
read db
while [ $i = "y" ]
do

clear
echo "1.View the Data Base "
echo "2.View Specific Records "
echo "3.Add Records "
echo "4.Delete Records "
echo "5.Exit "

echo "Enter your choice "


read ch

82
case $ch in
1)cat $db;;
2)echo "Enter id "
read id

grep -i "$id" $db;;


3)echo "Enter new std id "
iread tid
echo "Enter new name:"
read tnm
echo "Enter designation "

read des
echo "Enter college name"
read college
echo "$tid $tnm $des $college">>$db;;
4)echo "Enter Id"
read id

# set -a
# sed '/$id/d' $db>dbs1
grep -v "$id" $db >dbs1
echo "Record is deleted"
cat dbs1;;
5)exit;;

*)echo "Invalid choice ";;


esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]

83
then
exit
fi
done

RESULT
Shell script executed successfully.

84
OUTPUT

85
Program no: 31
Date: 03/08/2020

BUBBLE SORT
AIM: Write a shell script to sort the given numbers in descending order using Bubble sort.

Algorithm
Step 1: Start

Step 2: Enter the elements


Step 3: Compare the current number with the next number.
Step 4: Is the next number larger than the current number? If so, swap the two numbers
around. If not, do not swap

Step 5: Move to the next number along in the list and make this the current number
Step 6: Repeat from step 2 until the last number in the list has been reached
Step 7: If any numbers were swapped, repeat again from step 1
Step 8: If the end of the list is reached without any swaps being made, then the list is ordered
Step 9: Stop

Shell script
#!/bin/bash
echo
i=1
k=1
echo "Enter number of integers to be sorted"
read n
echo "Enter the numbers"

while [ $i -le $n ]
do
read num
x[$k]=$num

86
i=$((i + 1))
k=$((k + 1))
done
x[$k]=0

k=1
echo "The number you have entered are "
while [ ${x[$k]} -ne 0 ]
do
echo "${x[$k]}"
k=$((k + 1))

done
k=1
while [ $k -le $n ]
do
j=1
while [ $j -lt $n ]

do
y=$((j + 1))
if [ ${x[$j]} -lt ${x[$y]} ]
then
temp=${x[$j]}
x[$j]=${x[$y]}

x[$y]=$temp
fi
j=$((j + 1))
done
k=$((k + 1))

87
done
k=1
echo "Number in sorted order..."
while [ ${x[$k]} -ne 0 ]

do
echo "${x[$k]}"
k=$((k + 1))
done

RESULT
Shell script executed successfully.

88
OUTPUT

89
Program no: 32
Date: 06/08/2020

Factorial of a number using function


AIM: Write a shell program to find factorial of a number using function

Algorithm

Step 1: Start

Step 2: Enter and read a number

Step 3: Use while loop to compute the factorial inside the function fact() by using the
below formula
Step 4: fact=$((fact * num
num=$((num - 1))

Step 5: Stop

Shell Script
echo "Enter a number"
read num
function fact()
{
fact=1
while [ $num -gt 1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done
echo $fact
}
fact $num

RESULT
Shell script executed successfully.

90
OUTPUT

91
Program no: 33
Date: 06/08/2020

String is palindrome or not


AIM: Write a shell program to determine whether the given string is palindrome or not using
function.

Algorithm
Step 1: Start

Step 2: Enter and read a string

Step 3: for i in $(seq 0 ${#str}) ; do

revstr=${str:$i:1}$revstr
Step 4: if [ "$str" = "$revstr" ]
Step 5: Display “It is a palindrome” or else” It is not palindrome”
Step 6: Stop

Shell Script
echo "Input the string without space"
read str
function pali()
{

for i in $(seq 0 ${#str}) ; do


revstr=${str:$i:1}$revstr
done
echo "The given string is " $str
echo "Its reverse is " $revstr
if [ "$str" = "$revstr" ]; then

echo "It is a palindrome."


else

92
echo "It is not a palindrome."
fi
}pali $str

RESULT
Shell script executed successfully.

93
OUTPUT

94
Program no: 34
Date: 10/08/2020

Rename files
AIM: Write a script to rename all c files to cpp files.

Algorithm
Step 1: Start

Step 2: Read Line

Step 3: move exiting line to new

Step 4: Display” Renamed all c files to cpp files"


Step 5: Stop

Shell Script
#!/bin/ksh
ls *.c >amit.temp
while read line

do
echo $line
new=${line}pp
echo $new
mv $line $new > /dev/null
echo "Renamed all c files to cpp files"
done < amit.temp

RESULT
Shell script executed successfully.

95
OUTPUT

96
Program no: 35
Date: 10/08/2020

File Searching
AIM: The word “mca” is present in some of the files supplied as arguments. Write a script to
search each of these files, and to stop at the first file containing the word “mca” and report it.

Algorithm
Step 1: Start

Step 2: Search the file using grep command

Step 3: grep -w "^mca" $x > output


Step 4: Display the file that found.
Step 5: Stop

Shell Script
if [ $# -lt 1 ]

then
echo " Invlaid Arguments....... "
exit
fi

for x in $*
do
grep -w "^mca" $x > output
if [ $? -eq 0 ]
then
cat output

exit
fi
done

97
RESULT
Shell script executed successfully.

98
OUTPUT

99
Program no: 36
Date: 10/08/2020

Receive filenames as arguments


AIM: Write a script to receive any number of filenames as arguments and to check whether the
arguments supplied is a file or directory. If it is a directory, it should be appropriately reported. if
it is a filename then name of the file as well as the number of lines present in it should be
reported.

Algorithm
Step 1: Start
Step 2: for x in $* do if [ -f $x ]
Step 3: Display File name
Step 4: Display number of lines

Step 5: elif [ -d $x ]
Step 6: Display directory name
Step 7: Stop

Shell Script
for x in $* # for receiving any no of arguments
do

if [ -f $x ]
then
echo "File name is : $x"
echo "no of lines are"
wc -l $x
elif [ -d $x ]

then
echo "Give name is directory"
fi

100
done

RESULT
Shell script executed successfully.

101
OUTPUT

102
Program no: 37
Date: 13/08/2020

Command line argument


AIM: Write a script to read from a file which is supplied as command line argument and count
the number of lines and words. If there is no filename supplied, script should accept text from
keyboard.

Algorithm
Step 1: Start

Step 2: c=$( wc -c < ${file})


Step 3: Display Number of characters
Step 4: w=$( wc -w < ${file} )
Step 5: Display Number of words

Step 6: l=$( wc -l < ${file})

Step 7: Display Number of line


Step 8: Stop

Shell Script
for file in ${*}
do
echo #printing blank line

c=$( wc -c < ${file})


echo "Number of characters in ${file} is $c"
echo
w=$( wc -w < ${file} )
echo "Number of words in ${file} is $w"
echo

l=$( wc -l < ${file})


echo "Number of lines in ${file} is $l"
done

103
RESULT
Shell script executed successfully.

104
OUTPUT

105
Program no: 38
Date: 13/08/2020

Number of file names


AIM: Write a shell script which receives even number of file names. Suppose four file names are
supplied then the first file should get copied into second file, the third file should get copied into
fourth file, and so on. If odd numbers of file names are supplied then no copying should take
place and an error message should be displayed.

Algorithm
Step 1: Start

Step 2 if [ $((r%2)) -ne 0 ]


Step 3: Display Odd number of files
Step 4: else swap the files
Step 5: Display Copied successfully

Step 6: Stop

Shell Script
#!/bin/bash

r=$#

if [ $((r%2)) -ne 0 ]

then

echo "Odd number of files!!!"

else

for((i=0;i<$r;i=i+2))

do

f1=$1

f2=$2

cp $f1 $f2

shift 2

done

echo "Copied successfully..."

106
fi

RESULT
Shell script executed successfully.

107
OUTPUT

108
Program no: 39
Date: 13/08/2020

Wish the user


AIM: Write a script to wish the user “Good Morning, Good Afternoon and Good Evening” when
he logs in to the system based on the time.

Algorithm

Step 1: Start

Step 2: if [ $hour -ge 0 -a $hour -lt 12 ] then greet="Good Morning


Step 3: elif [ $hour -ge 12 -a $hour -lt 18 then greet="Good Afternoon”
Else “Good evening”
Step 4: Stop

Shell Script

# get current hour (24 clock format i.e. 0-23)

hour=$(date +"%H")
# if it is midnight to midafternoon will say G'morning
if [ $hour -ge 0 -a $hour -lt 12 ]
then
greet="Good Morning, $USER"
# if it is midafternoon to evening ( before 6 pm) will say G'noon

elif [ $hour -ge 12 -a $hour -lt 18 ]


then
greet="Good Afternoon, $USER"
else # it is good evening till midnight
greet="Good evening, $USER"
fi
# display greet

echo $greet

109
RESULT
Shell script executed successfully.

110
OUTPUT

111

You might also like