捕获嵌入在bash函数中的对话框调用的结果
我想从bash中的泛型函数中调用unix捕获嵌入在bash函数中的对话框调用的结果,bash,dialog,Bash,Dialog,我想从bash中的泛型函数中调用unix对话框editbox。我感兴趣的两个结果是: 如果用户点击OK,我想捕获用户在编辑框中输入的内容 检测用户是否点击“取消” 以下是我的一些代码,但我不确定这是否是最好的方法: function ib_generic() { tmp_file="/tmp/file.tmp" if [ -f $tmp_file ] then rm -f $tmp_file fi mkfifo $tm
对话框
editbox。我感兴趣的两个结果是:
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
}
如果点击OK,取消结果的最佳方法是什么?如果没有,如何检测取消或转义?不要在后台运行
对话框,因为它不会等待对话框完成,也不会设置$?
您的退出状态也不正确0
表示按下了OK
,1
表示取消
,255
表示退出
dialog --stdout \
--title "$1" \
--backtitle "My Backtitle" \
--inputbox "$2" 20 40 2> "$tmp_file"
exit=$?
if [ $exit -eq 0 ] # OK was pressed
then
echo "$result"
elif [ $exit -eq 1 ] || [ $exit -eq 255 ] # Cancel or Escape was pressed
then
echo 1
else
echo 2
fi
有一个更完整的示例对话框在每个按钮上返回不同的退出代码,下面是我的操作方法:
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
更多信息是不要在后台运行
对话框
。脚本不会等待退出。只需使用result=$(dialog…
@Barmar我已经更新了问题。我想找到最好的方法来检测是否点击OK(确定)或是否点击Cancel(取消)的输出。变量赋值将根据它们退出对话框的方式设置$?