bash 对话框显示带有文件输入的菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10744935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:22:42  来源:igfitidea点击:

dialog show a menu with input from a file

bashdialog

提问by Ivan

In my script i created a file that i want to use as an input source for dialog program, in order to populate the menu dialog.

在我的脚本中,我创建了一个文件,我想将其用作对话框程序的输入源,以填充菜单对话框。

In the file every line contain a number and a menu title.

在文件中,每一行都包含一个数字和一个菜单标题。

INPUT=""
rm result
while read LINE
do
        case $LINE in
            '/ >'*|---*|'/ > '*)
                  continue;;
            esac
#       echo "1 $LINE "
                echo -n " 1  \"$LINE\"" >>result
done < tmpfile
#printf "%s " $INPUT
DIALOG  --menu "Latest news " 20 50 30 `cat result`

DIALOG is a function, and this is the source:

DIALOG 是一个函数,这是来源:

DIALOG () {
        dialog --backtitle "$TITLE" "$@"
        return $?
}

Every menu item for dialog need an identifie (in my example is the number) and a label (between double quotes ", if it is more than one word).

对话框的每个菜单项都需要一个标识符(在我的例子中是数字)和一个标签(在双引号“之间,如果它是多个单词)。

The problem is that even if the file content has the correct format:

问题是即使文件内容的格式正确:

1 "item number 1" 2 "item number 2" ...

The dialog command doesn't recognize the \" as a label delimiter, but it consider it as a part of the string, resulting in a messed up menu, where i have a menu voice for every word.

对话框命令不会将 \" 识别为标签分隔符,但它会将其视为字符串的一部分,从而导致菜单混乱,其中每个单词都有一个菜单语音。

but if i try to copy and pastethe same file content directly into the shell command (not using a script), the menu is showed correctly.

但是,如果我尝试将相同的文件内容直接复制并粘贴到 shell 命令中(不使用脚本),则菜单会正确显示。

Any idea?

任何的想法?

回答by choroba

You can use an array to keep the results, then use proper quoting to send it to dialog:

您可以使用数组来保留结果,然后使用正确的引用将其发送到dialog

ar=()
while read n s ; do
    ar+=($n "$s")
done < result
dialog  --menu "Latest news " 20 50 30 "${ar[@]}"

You probably do not have to create a temp file at all, you can populate the array right ahead.

您可能根本不需要创建临时文件,您可以直接填充数组。

回答by stuchlyd

The problem are the spaces. Set IFS=$'\n' and keep the values in the result file on separate lines as suggested above.

问题是空格。设置 IFS=$'\n' 并按照上面的建议将结果文件中的值保留在单独的行中。