I am a newbie to linux and shell scripting. I need to write a shell script that prints the following menu:
C)hange into a directory
L)ist the files in current directory
M)ove a file
K)opy a file
P)rint the contents of a file
The script should read the user's choice and use appropriate shell commands to execute the stated function, prompting the user for any necessary arguments. For example, if the user chose 'p', prompt the user for a file name, and print out the contents of the file.
So far I have done this, but I want the options to be letters instead of numbers, like described earlier. Possible a better, cleaner script.
#!/bin/bash
# Bash Menu Script Example
PS3='Please enter your choice: '
options=("C)hange into a directory" "L)ist the files in the current
directory" "M)ove a file" "K)opy a file" "P)rint the contents of a file" "Quit")
select opt in "${options[@]}"
do
case $opt in
"C)hange into a directory")
echo "you chose choice 1"
echo -n "Enter a directory to change into"
read answer
cd $answer
pwd
;;
"L)ist the files in the current directory")
echo "you chose choice 2"
echo -n "Listing the files in the current directory"
ls -ltr ./
;;
"M)ove a file")
echo "you chose choice 3"
echo -n "Enter a file name to move"
read answer
mv $answer /tmp
;;
"K)opy a file")
echo "you chose choice 3"
echo -n "Enter a file to copy"
read answer
cp $answer /tmp
;;
"P)rint the contents of a file")
echo "you chose choice 3"
echo -n "Print to contents of a file"
read answer
cat $answer
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done