【问题标题】:Whiptail: How can I handle an error when a gauge is running?Whiptail:仪表运行时如何处理错误?
【发布时间】:2021-10-26 17:41:06
【问题描述】:

这是一个 for 循环,它运行一个常规的 Whiptail 仪表。在这个循环中,一个命令可能会引发错误,我想显示一个消息框--msgbox 来显示错误。之后,我可以让脚本继续,就像错误从未发生过一样。

这里以一个过于简化的代码为例。这是它的行为和代码:

  • for 列表正在对包含 3 个数字和 1 个字母的数组进行迭代。
  • 对于前 2 个数字,执行使用它们的命令并且量表增加 25%。
  • 对于字母,命令将失败,我想显示一个--msgbox 错误等待我按回车
  • 对于最后一个数字,与其他 2 个数字的行为相同。
#!/bin/bash

array=("1" "2" "A" "3")

i=0
for element in "${array[@]}"; do
        command $element #This command will fail with any non int value.
        echo "XXX"
        echo $(expr 100 / 4 \* $i)
        echo ${array[$i]}
        echo "XXX"
        i=$((i+1))
        sleep 1
done | whiptail --title "Gauge" --gauge "Wait" 10 80 0

我已经尝试过一些类似command $element || whiptail --title "Exception" --msgbox "Error!" 10 80 的方法。但是,由于消息框的whiptail 在仪表的循环中,所以输出被破坏了。

问题可能出在我的设计上?

感谢您的帮助:)

【问题讨论】:

    标签: bash whiptail


    【解决方案1】:

    即使已经有一段时间了,您也可以输出到不同的输出描述符。就我而言,我输出到一个单独的日志文件,如下所示:

    #!/bin/bash
    
    array=("1" "2" "A" "3")
    
    i=0
    for element in "${array[@]}"; do
            command $element #This command will fail with any non int value.
            echo "XXX" >> logs.log
            echo $(expr 100 / 4 \* $i)
            echo ${array[$i]} >> logs.log
            echo "XXX" >> logs.log
            i=$((i+1))
            sleep 1
    done | whiptail --title "Gauge" --gauge "Wait" 10 80 0
    
    

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 2015-06-05
      • 1970-01-01
      • 2015-12-06
      • 2016-03-31
      • 2019-06-23
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多