更正 shell 脚本

更正 shell 脚本

我有一段 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)

工作得很好

相关内容