Whiptail 将名称中的连字符检测为命令

Whiptail 将名称中的连字符检测为命令

我正在尝试使用制作一个检查表whiptail,但当我输入其中一个选项时,它会将其检测为命令,即使它在引号中。此检查表的要点是 bash 一个文件,并在 bash 期间将变量添加到 shell 命令的末尾,即

bash something.sh -cached_var_here

以下是代码和错误:

BENCH=$(whiptail --title "Choose Benchmark Options" --checklist "Choose:" 20 
30 15 \
 "-info" "System Information" off \
 "-io" "System I/O Test" off \
 "-cdn" "CDN Test Download (200MB)" off \
 "-northamerica" "North-American Server Test Download (800MB)" off \
 "-europe" "European Server Test Download (900MB)" off \
 "-asia" "Asian Server Test Download (400MB)" off \
 "-b" "System Information, CDN Download Test & I/O Test" off \
 "-speed" "Network Speedtest Using speedtest-cli" off \
 "-share" "Share Your Results With Others!" off \
 "-help" "Help Menu. Do NOT Execute This With Other Options!" off \
 "-about" "Show About-Info. Do NOT Execute This With Other Options!" off \
3>&1 1>&2 2>&3)

echo "You chose the following options: $BENCH"
echo

echo "Do you want to run CUSTOM benchmark and information? (y/n)? "
old_stty_cfg=$(stty -g)
stty raw -echo
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )
stty $old_stty_cfg
if echo "$answer" | grep -iq "^y" ;then
  echo Yes;

  curl -LsO raw.githubusercontent.com/sayem314/serverreviewbenchmark/master/bench.sh; chmod +x bench.sh
  echo
  ./bench.sh $BENCH

我收到此错误:

You chose the following options: -info: unknown option

答案1

让我们咨询一下man whiptail

whiptail将以破折号“ -”开头的参数解释为参数。为了避免这种情况,并以破折号开头一些文本(例如,菜单框项),whiptail请遵守 getopt 接受特殊参数“ --”的惯例,这意味着所有以破折号开头的后续参数都应按原样处理,而不是解析为选项。

因此,下面的方法应该会非常有效:

whiptail --title "Choose Benchmark Options" --checklist -- Choose: 20 30 15 -info "System Information" off

相关内容