UNIX/putty!! Use the korn shell for this project. Using the select command from
ID: 3747248 • Letter: U
Question
UNIX/putty!!
Use the korn shell for this project. Using the select command from the korn shell, write a menu driven script that calls your trash script from Project 4 in the following manner:
- Menu item 1 calls trash –l
- Menu item 2 calls trash–p
- Menu item 3 asks the user for a file or a list of files, and calls trash sending those files.
- Menu item 4 should exit the script.
- Do not have a menu item for the empty string.
Be careful of your trash scripts location and permissions of the trash folder and the trash script. I must be able to access and run the trash script and create the trash folder. For the menu script to access your trash script, you will need to include the path to your trash script in each call in the menu script. You will also need to have your trash script in an accessible directory.
PROJECT4 CODE IF NEEDED:
#!/bin/sh
print_usage() #This is used to print the usage
{
echo "Usage: trash -l | -p | { filename }*"
}
if [ $# -eq 0 ]; then #If no arguments are provided to the script
print_usage
exit 0
fi
if [ "$1" = "-l" ]; then #If the first argument is a -l
if [ -d "$HOME/.trash" ]; then #Check for home directory
for filename in "$HOME/.trash"/*
do
echo "$(basename $filename)" #List the files
done
else
echo "There are no files in trash"
fi
exit 0
fi
if [ "$1" = "-p" ]; then #if the first arument is a -p
if [ -d "$HOME/.trash" ]; then
`rm -r $HOME/.trash` #Remove the trash directory
fi
exit 0
fi
CWD=`pwd`
for argument in "$@" #Remaining arguments are treated as filenames in the current working directory
do
if [ -f "$CWD/$argument" ]; then
if ! [ -d "$HOME/.trash" ]; then
`mkdir "$HOME/.trash"`
fi
`mv "$CWD/$argument" "$HOME/.trash/"`
fi
done
Explanation / Answer
#!/bin/bash
# This script assumes that the trash script is in the same folder of the menu script.
echo "Trash Menu" # This echo command is to display the Title Trash Menu
# The select command allows to generate simple menus.
# All the menu items are provided as a list
select menuitem in "list" "purge" "trash files" "exit"
do
case "$menuitem" in
"list") ./trash -l;; # the list command will be displayed as 1) list. Select 1 to use this option
"purge") ./trash -p;; # the purge command will be displayed as 2) purge. Select 2 to use this option
"trash files") echo "List the files to trash with a space between them:"
read filenames # read the filenames typed as input and store the value in the variable filenames;
./trash $filenames;;
"exit") break;;
*) echo "Invalid option";;
esac
done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.