0

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

1 Answer 1

2

Sample script below, make the change accordingly,

Sample Script

#!/bin/bash

while true; do
echo -e "\nPlease enter a Letter : \nP - Print Date \nE - Exit"
read value
case $value in
    [Pp]* ) echo `date`;;
    [Ee]* ) exit;;
    * ) echo "\nPlease P or E";;
esac
done

Sample Output:

[root@localhost ~]# sh my.sh

Please enter a Letter :
P - Print Date
E - Exit
p
Tue Apr 18 06:29:15 PDT 2017

Please enter a Letter :
P - Print Date
E - Exit
E

In your case the script will be like,

#!/bin/bash
# Bash Menu Script Example

while true; do
echo -e "\nPlease enter your choice: "
echo -e "\n(C)Change into a directory\n(L)ist the files in the current directory \n(M)Move a file \n(K)Copy a file \n(P)Print the contents of a file \n(Q)Quit\n"
read opt
case $opt in
[Cc]* )
echo "you chose choice 1"
echo -n "Enter a directory to change into"
read answer
cd $answer
pwd
;;
[Ll]* )
echo "you chose choice 2"
echo -n "Listing the files in the current directory"
ls -ltr ./
;;
[Mm]* )
echo "you chose choice 3"
echo -n "Enter a file name to move"
read answer
mv $answer /tmp
;;
[Kk]* )
echo "you chose choice 3"
echo -n "Enter a file to copy"
read answer
cp $answer /tmp
;;
[Pp]* )
echo "you chose choice 3"
echo -n "Print to contents of a file"
read answer
cat $answer
;;
[Qq]* )
break
;;
*) echo invalid option;;
esac
done

Note: [Cc]* ) - Wich means any Name start with C/c as input it will accept, if you need only one letter as input remove the *(asterisk) on each case, say [C] )

Hope this might help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot. Please also tell if [C] will work as case sensitive or for both inputs of 'C' or 'c' ?
@AhmedDildar - Its Case Sensitive, so it will work depends upon you declare the letter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.