bash对话框从表单中读取变量

时间:2016-03-12 08:09:49

标签: bash dialog

假设我得到了如此定义的对话框:

dialog --backtitle "Dialog Form Example" --title "Dialog - Form" \
--form "\nDialog Sample Label and Values" 25 60 16 \
"Form Label 1:" 1 1 "Value 1" 1 25 25 30 \
"Form Label 2:" 2 1 "Value 2" 2 25 25 30 \
"Form Label 3:" 3 1 "Value 3" 3 25 25 30 \
"Form Label 4:" 4 1 "Value 4" 4 25 25 30

这将显示4个输入...但是如何在bash脚本中读取输出?

$?似乎输出0

2 个答案:

答案 0 :(得分:1)

如果要将不同的表单字段分配给不同的var,则需要解析输出。

ans=$(dialog --backtitle "Dialog Form Example" --title "Dialog - Form" \
--form "\nDialog Sample Label and Values" 25 60 16 \
"Form Label 1:" 1 1 "Value 1" 1 25 25 30 \
"Form Label 2:" 2 1 "Value 2" 2 25 25 30 \
"Form Label 3:" 3 1 "Value 3" 3 25 25 30 \
"Form Label 4:" 4 1 "Value 4" 4 25 25 30 2>&1 >/dev/tty)
echo -e "\n\n\n\nAnswer=[${ans}]"
i=0
while read -r line; do
   ((i++))
   declare var$i="${line}"
done <<< "${ans}"
echo "var2=${var2}"

答案 1 :(得分:0)

您需要将对话框的标准输出重定向到变量:

RESULTS=$(dialog --backtitle "Dialog Form Example" --title "Dialog - Form" \
--form "\nDialog Sample Label and Values" 25 60 16 \
"Form Label 1:" 1 1 "Value 1" 1 25 25 30 \
"Form Label 2:" 2 1 "Value 2" 2 25 25 30 \
"Form Label 3:" 3 1 "Value 3" 3 25 25 30 \
"Form Label 4:" 4 1 "Value 4" 4 25 25 30)

通过这种方式,所有的值都会存在。

https://bash.cyberciti.biz/guide/The_form_dialog_for_input