我有一段 shell 腳本,它之前可以工作,但現在它給了一些不同的結果:
MSG=
while true
do
themenu
getchar =
case $answer in
1) export_Config_tables;;
2) export_config_tables_file;;
3) export_Accounts_tables;;
4) export_both_tables;;
5) load_config_tables;;
6) load_config_tables_file;;
x|X) break;;
q|Q) break;;
d|D) toggle_debug;;
*) badchoice;;
esac
done
clear
選單功能:
themenu () {
clear
echo `date `
echo
echo " summit Data Extract utility (SOPRA v1.1)"
echo
echo
echo "1. Extract summit configuration data only"
echo "2. Extract summit config data in flat files only"
echo "3. Extract summit account data only"
echo "4. Extract all data "
echo "5. Load summit configuration data from Dump"
echo "6. Load summit config data from flat files only"
echo
echo
echo "x. Exit"
echo
echo $MSG
echo
echo "Select option : ";
}
功能getchar
:
getchar (){
stty raw
answer=`dd bs=1 count=1 2> /dev/null `
stty -raw
}
錯誤的選擇函數:
badchoice () {
MSG="Invalid menu choice"
}
執行腳本後,顯示選單
<System date>
summit Data Extract utility (SOPRA v1.1)
1. Extract summit configuration data only
2. Extract summit config data in flat files only
3. Extract summit account data only
4. Extract all data
5. Load summit configuration data from Dump
6. Load summit config data from flat files only
x. Exit
Select option :
使用者輸入5,但螢幕上不顯示,按Enter2-3次後,顯示訊息:
Invalid menu choice.
我無法弄清楚它在哪裡引起問題。選單函數執行正常,但在進入getchar()
函數和case
語句時會出現問題。
答案1
正如所寫的那樣,對我不起作用,大概是因為
answer=`dd bs=1 count=1 2> /dev/null `
不執行命令並設定answer
為返回值,dd
而是將字串 ( dd bs=1 count=1
) 賦給變數answer
。
編輯以進行擴展
answer=$(dd bs=1 count=1 2> /dev/null)
工作得很好