无法正确退出 bash 脚本

无法正确退出 bash 脚本

我在 bash 上制作了一个脚本。

#!/bin/bash

zen(){
mark=$(zenity --scale \
    --text 'FREQUENCY' \
    --value=$la \
    --min-value=0\
    --max-value=5000 \
    --step=1)
}
la=500

echo "Script for shim. Regulary frequency"
zen
while [ true ] 
do

case $? in

    0) echo $mark
       la=$mark
       #zenity --notification --window-icon="info" --text="Thank you!" --timeout=1
       zen
    ;;
    1) 
       # exit 1
       # sl -e || break
       # break
       # return 1       
    ;;
esac 
done
echo "thanks for using!"

它工作正常,不包括退出点。 # 位于我尝试过的选项之前,并且每个选项都不允许正确退出此脚本,而不是“感谢使用!”或者我在终端什么也没有得到:

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

^XThis option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

This option is not available. Please see --help for all possible usages.

.........................................

当我尝试退出脚本时,看起来 zenity 有问题。我查了一下这个错误,唯一合理的想法是升级 zenity,我已经这样做了,但它没有给我带来任何新的东西......

那么我该如何解决它并正确地破坏这个脚本..?

我的操作系统是 Ubuntu Server 16.04

编辑

通过我的脚本,我想实现从zenity到用户单击“取消”的那一刻的重复问题

答案1

$?是最后运行的命令的退出状态。在您的情况下,这就是命令[(您用它来测试true字符串是否非空作为while循环的条件)。

您几乎永远不需要$?显式使用。做就是了

la=500
while
  mark=$(zenity --scale \
      --text 'FREQUENCY' \
      --value="$la" \
      --min-value=0 \
      --max-value=5000 \
      --step=1)
do
  echo "$mark"
  la=$mark
done

或者简单地:

mark=500
while
  mark=$(zenity --scale \
      --text 'FREQUENCY' \
      --value="$mark" \
      --min-value=0 \
      --max-value=5000 \
      --step=1)
do
  echo "$mark"
done

答案2

您的调用中的反斜杠之前缺少空格zenity,可能会导致错误:

zen(){
mark=$(zenity --scale \
    --text FREQUENCY \
    --value=$la \
    --min-value=0 \
    --max-value=5000 \
    --step=1)
}

la=500

echo "Script for shim. Regulary frequency"
zen
zen_ec=$?
while true
do

    case $zen_ec in

        0) echo $mark
           la=$mark
           #zenity --notification --window-icon="info" --text="Thank you!" --timeout=1
           zen
        ;;

[...]

相关内容