捕获嵌入在bash函数中的对话框调用的结果 [英] Capturing results of dialog call embedded in a bash function

查看:39
本文介绍了捕获嵌入在bash函数中的对话框调用的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从bash中的泛型函数中调用Unix dialog 编辑框.我感兴趣的两个结果是:

I want to call the unix dialog editbox from within a generic function in bash. The two outcomes I am interested in are:

  1. 如果用户点击确定,我想捕获用户在编辑框中输入的内容
  2. 检测用户是否单击取消

这里有一些代码,但是我不确定这是否是最好的方法:

Here is some code I have but I'm not sure if this is the best way:

function ib_generic()
{
    tmp_file="/tmp/file.tmp"
    if [ -f $tmp_file ]
    then
    rm -f $tmp_file
    fi
    
    mkfifo $tmp_file

    # push the user input to $tmp_file
    dialog --stdout \
    --title "$1" \
    --backtitle  "My Backtitle" \
    --inputbox "$2" 20 40 2> $tmp_file &

    # detect 'cancel' or 'escape':
    if [[ $? -eq 0 || $? -eq 255 ]] 
    then
        rm -f $tmp_file
        echo 1
    else # 'ok' was pressed so proceed:
        result="$( cat /tmp/file.tmp )"
        rm -f $tmp_file
        echo $result
    fi
}

如果单击确定",取消结果的最佳方法是什么?如何检测取消"或转义"?

What's the best way to cancel the result if OK is hit and if not, how to detect Cancel or Escape?

推荐答案

对话框在每个按钮上返回不同的退出代码,这是我的操作方式:

Dialog returns different exit codes on each button, here's how i'd do it:

input=$(
    dialog --stdout \
           --title "test" \
           --backtitle  "My Backtitle" \
           --inputbox "$2" 20 40 
)

case $? in
     0) echo "ok $input";;
     1) echo 'cancel'   ;;
     2) echo 'help'     ;;
     3) echo 'extra'    ;;
   255) echo 'esc'      ;;
esac

更多信息是此处

这篇关于捕获嵌入在bash函数中的对话框调用的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆