Write a shell script (in Linux) that displays the following menu items, prompts
ID: 3903604 • Letter: W
Question
Write a shell script (in Linux) that displays the following menu items, prompts the user to enter a selection, and then executes the command associated
with that menu item. If required, prompt the user for additional commands should the menu item require it.
The menu items:
(a) Display a list of files and directories in the current directory
(b) Display a long list of the contents of the current directory
(c) Display a list of the last n messages in /var/log/messages, prompting for the value of n
(d) Display the contents of this script
(e) Display the contents of this script in reverse
(f) Exit the script
Don’t forget to have the script keep running until the user selects the exit option.
Explanation / Answer
Instructions:
1) Put this below code in a file named "s.sh"
2) open terminal and go to directory in which the file is present
3) run "sh s.sh"
Code:
#!/bin/bash
while :
do
echo "(a) Display a list of files and directories in the current directory"
echo "(b) Display a long list of the contents of the current directory"
echo "(c) Display a list of the last n messages in /var/log/messages, prompting for the value of n"
echo "(d) Display the contents of this script"
echo "(e) Display the contents of this script in reverse"
echo "(f) Exit the script"
read -p "Enter an option: " var
echo "###################################"
case "$var" in
"a") ls
;;
"b") ls -l
;;
"c") read -p "Enter n: " n
tail -$n /var/log/messages
;;
"d") cat s.sh
;;
"e") tail -r s.sh
;;
"f") break
;;
esac
echo "################################### "
done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.