0
我是linux和shell腳本的新手。我需要寫一個shell腳本,打印以下菜單:在打印特定菜單的shell腳本中需要幫助
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
該腳本應讀取用戶的選擇和使用合適的shell命令來執行所陳述的功能,提示輸入任何必要的參數的用戶。例如,如果用戶選擇'p',提示用戶輸入文件名,並打印出文件的內容。
到目前爲止,我已經做到了這一點,但我希望選項可以是字母而不是數字,如前所述。可能更好,更乾淨的腳本。
#!/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
非常感謝。請同時告訴我們[C]是否會區分大小寫或者輸入'C'或'c'? –
@AhmedDildar - 它的大小寫敏感,所以它的工作取決於你申報的信。 –