简体   繁体   English

Bash Shell脚本-对话框形式变量

[英]Bash Shell Scripting - Dialog form variables

So, I just took up Shell Scripting and I'm developing an address book. 因此,我刚开始学习Shell Scripting,并且正在开发通讯录。

For the user to insert a contact I made this form: 为了让用户插入联系人,我填写了以下表格:

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)

And I want to insert those values through a MySQL query. 我想通过MySQL查询插入这些值。 I saw somewhere that I had to use, for instance: 我看到了必须使用的地方,例如:

form[$1]

In order to access the variable $nome. 为了访问变量$ nome。 However, it was a comment from 2008. What is the easiest way to access those variables? 但是,这是2008年的评论。访问这些变量的最简单方法是什么?

Thank you! 谢谢!

So, after a bit of tinkering I got what I was looking for. 因此,经过一番修补后,我得到了我想要的东西。 Here is the new form: 这是新表格:

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 ... )

Unlike dialog ... | { read; ... } dialog ... | { read; ... }不同dialog ... | { read; ... } dialog ... | { read; ... } dialog ... | { read; ... } (which scopes the variables which are read to a subshell), this approach puts dialog in the subshell, and your variables in the main shell -- much more convenient. dialog ... | { read; ... } (它将读取到子外壳程序的变量的范围限定在内),这种方法将对话框放在子外壳程序中,而您的变量放在主外壳程序中-更加方便。

The question regarding the easiest way to access the result depends partly on whether the items might contain blanks. 有关访问结果的最简单方法的问题部分取决于项目是否可能包含空格。 If the items can contain arbitrary data, then line-oriented output (the default) seems the only way to go. 如果项目可以包含任意数据,那么行输出(默认)似乎是唯一的方法。 If they are more constrained, eg, not containing some readily-used punctuation character which can be used as a delimiter, then that makes it simpler. 如果它们受到更严格的约束,例如不包含一些可用作定界符的易于使用的标点符号,那么这将使其更加简单。

The manual page mentions an option (and alias) which can be used to do this: 手册页提到了一个选项(和别名),可用于执行此操作:

--separator string --separator 符字符串

--output-separator string --output-separator 字符串

Specify a string that will separate the output on dialog's output from checklists, rather than a newline (for --separate-output ) or a space. 指定一个字符串,该字符串将对话框输出中的输出与清单分开,而不是换行符(用于--separate-output )或空格。 This applies to other widgets such as forms and editboxes which normally use a newline. 这适用于其他小部件,例如通常使用换行符的表单编辑框

For example, if the data does not include a : (colon), then you could use the option 例如,如果数据不包含:冒号),则可以使用该选项

--output-separator :

and get colon-separated values on a single line. 并在一行上获取冒号分隔的值。

If there are no commas or quotes in the string, you could conceivably use 如果字符串中没有逗号或引号,则可以想象使用

--output-separator \",\"

and embed the result directly in an SQL statement. 并将结果直接嵌入到SQL语句中。 However, commas occur more frequently than the other punctuation mentioned, so processing the form's output with sed is the most likely way one might proceed. 但是,逗号比提到的其他标点符号更频繁地出现,因此使用sed处理表单的输出是最有可能进行的方式。

After several days looking for a way get those variables, here what I used, with your form: 经过几天的寻找,找到了获取这些变量的方法,这是我在表格中使用的:

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

This way you can use those variables later on your script. 这样,您可以稍后在脚本中使用这些变量。 Hope it helps others. 希望它能帮助别人。

So, you can really just put the output into an array and deal with that. 因此,您实际上可以将输出放入数组中并进行处理。 Avoids all the subshell / subprocess garbage. 避免所有子shell /子进程垃圾。 (Just trust on the flippy redirect, yeah, it's ugly but you're basically just subbing out stdin and swapping it back.) Not sure why that's been so elusive after 5 years, but hey. (只是信任软盘重定向,是的,这很丑陋,但是您基本上只是消减了stdin并将其交换回去。)不确定为什么5年后它是如此难以捉摸,但是,嘿。 I guess it's cool to be obscure. 我想晦涩难懂。

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

...and bob's your uncle. 鲍勃是你的叔叔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM