Write a small shell script which does the following using shell functions: 1. Go
ID: 3702029 • Letter: W
Question
Write a small shell script which does the following using shell functions:
1. Goes into a loop and prints out a menu with three options
1. Query the user for a month and year, then show the month using the cal command
2. Show who is logged on, then let the user send a message to another logged on user
3. Queries for a filename, then prints out the file type (file command), the owner, and the
file permissions
2. Reads the users selection and performs the selected function
3. Allows the user to exit the program with a return code of 0
Explanation / Answer
Answer:-
In this shell script we use the following commands
cal - Displays a calendar
who - show who is logged on
read - Read input from user
file - Print file type
stat - Display file status
write - Send message to another user
code to run the program:-
#!/bin/sh
#infinite loop
while true
do
#display menu choice
echo "1. Display calander"
echo "2. Send Message"
echo "3. File Details"
echo "0. Exit"
read -p "Enter your choice: " choice
case $choice in
1)
#display monthly calander for given year and month
read -p "Enter month: " month
read -p "Enter Year: " year
cal -m $month $year
;;
2)
#sending message to selected user
echo "List of user's currently login"
echo "------------------------------"
who -u
read -p "Enter user name to send message: " user
read -p "Enter message: " msg
echo $msg | write $user
;;
3)
#display file owner and it's permission using stat command
read -p "Enter File Name(Full Path): " fname
type=$(file -b $fname)
echo "File type: " $type
owner=$(stat -c '%U' $fname)
echo "File Owner: " $owner
permission=$(stat -c '%A' $fname)
echo "File Permission: " $permission
;;
0)
exit 0
;;
esac
done
executed output:-
1. Display calander
2. Send Message
3. File Details
0. Exit
Enter your choice: 1
Enter month: 1
Enter Year: 2016
January 2016
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
1. Display calander
2. Send Message
3. File Details
0. Exit
Enter your choice: 3
Enter File Name(Full Path): smallshell.sh
File type: POSIX shell script, ASCII text executable
File Owner: bharath
File Permission: -rwxrwxrwx
1. Display calander
2. Send Message
3. File Details
0. Exit
Enter your choice: 2
List of user's currently login
------------------------------------
balu tty2 2018-03-29 22:35 00:21 738 (:0)
Enter user name to send message: balu
Enter message: Welcome to here
1. Display calander
2. Send Message
3. File Details
0. Exit
Enter your choice: 0
//----------//--------happy coding--------//----------//
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.