Bash Shell 脚本 - 对话框表单变量

Bash Shell Scripting - Dialog form variables

所以,我刚刚开始 Shell 编写脚本,并且正在开发地址簿。

为了让用户插入联系人,我做了这个表格:

form=$(dialog                                      \
    --title "INSERIR"                              \
    --form  ""                                     \
    0 0 0                                          \
    "Nome:"      1 1    "$nome"     1 10 20 0      \
    "Morada:"    2 1    "$morada"   2 10 20 0      \
    "Telefone:"  3 1    "$telefone"     3 10 20 0  \
    "E-Mail:"    4 1    "$mail"     4 10 20 0      \  
2>&1 1>&3)

我想通过 MySQL 查询插入这些值。我看到我必须使用的地方,例如:

form[]

为了访问变量$nome。但是,这是 2008 年的评论。 访问这些变量的最简单方法是什么?

谢谢!

有关访问结果的最简单方法的问题部分取决于项目是否可能包含空白。如果项目可以包含任意数据,那么面向行的输出(默认)似乎是唯一的出路。如果它们更受限制,例如,不包含一些可以用作分隔符的现成标点符号,那么这会使它更简单。

manual page 提到了一个可用于执行此操作的选项(和别名):

--separator string

--output-separator string

Specify a string that will separate the output on dialog's output from checklists, rather than a newline (for --separate-output) or a space. This applies to other widgets such as forms and editboxes which normally use a newline.

例如,如果数据不包含 :(冒号),则您可以使用选项

--output-separator :

并在一行中获取以冒号分隔的值。

如果字符串中没有逗号或引号,您可以想象使用

--output-separator \",\"

并将结果直接嵌入到 SQL 语句中。但是,逗号比提到的其他标点出现得更频繁,因此使用 sed 处理表单输出是最可能的处理方式。

所以,经过一番修改后,我得到了我要找的东西。 这是新表格:

exec 3>&1

dialog                                             \
--separate-widget $'\n'                            \
--title "INSERIR"                                  \
--form ""                                          \
0 0 0                                              \
"Nome:"     1 1 "$nome"     1 10 30 0              \
"Morada:"       2 1     "$morada"       2 10 30 0  \
"Telefone:"     3 1     "$telefone" 3 10 30 0      \
"E-Mail:"       4 1     "$mail"         4 10 30 0  \
2>&1 1>&3 | {
    read -r nome
    read -r morada
    read -r telefone
    read -r mail

    #The rest of the script goes here
}

exec 3>&-
IFS=$'\n' read -r -d '' nome morada telefone mail < <( dialog ... )

dialog ... | { read; ... }(将读取到子 shell 的变量的范围限定)不同,此方法将对话框放在子 shell 中,而您的变量放在主 shell -- 方便多了。

经过几天寻找获取这些变量的方法,这里是我使用的,以及您的表单:

nome=""
morada=""
telefone=""
mail=""

user_record=$(\
dialog                                             \
--separate-widget $'\n'                            \
--title "INSERIR"                                  \
--form ""                                          \
0 0 0                                              \
"Nome:"     1 1 "$nome"     1 10 30 0              \
"Morada:"       2 1     "$morada"       2 10 30 0  \
"Telefone:"     3 1     "$telefone" 3 10 30 0      \
"E-Mail:"       4 1     "$mail"         4 10 30 0  \
  3>&1 1>&2 2>&3 3>&- \
)
nome=$(echo "$user_record" | sed -n 1p)
morada=$(echo "$user_record" | sed -n 2p)
telefone=$(echo "$user_record" | sed -n 3p)
mail=$(echo "$user_record" | sed -n 4p)

echo $nome
echo $morada
echo $telefone
echo $mail

这样您以后就可以在脚本中使用这些变量。 希望对其他人有帮助。

所以,您真的可以将输出放入一个数组中并进行处理。避免所有子外壳/子进程垃圾。 (只要相信 flippy 重定向,是的,它很难看,但你基本上只是替换掉标准输入并将其换回。)不知道为什么 5 年后它变得如此难以捉摸,但是嘿。我想默默无闻很酷。

response=$(dialog                                  \
    --title "INSERIR"                              \
    --form  ""                                     \
    0 0 0                                          \
    "Nome:"      1 1    "$nome"     1 10 20 0      \
    "Morada:"    2 1    "$morada"   2 10 20 0      \
    "Telefone:"  3 1    "$telefone"     3 10 20 0  \
    "E-Mail:"    4 1    "$mail"     4 10 20 0      \  
    3>&1 1>&2 2>&3 3>&-)

#convert the space separated string to an array.. the madness!!
responsearray=($response)

echo ${responsearray[0]} #nome
echo $(responsearray[1]} #morada
echo ${responsearray[2]} #telefone
echo ${responsearray[3]} #mail

...鲍勃是你的叔叔。