4

I want to call dialog à la:

dialog --menu Choose: 0 40 10 A '' B '' C ''

except A, B and C are the result of a dynamic query, for the sake of this question the latter being { echo A; echo B; echo C; }.

I can get the desired command line seemingly by:

{ echo A; echo B; echo C; } | sed -e "s/\$/ ''/;"

but:

echo $({ echo A; echo B; echo C; } | sed -e "s/\$/ ''/;")

and its output:

A '' B '' C ''

show that the result of the command substitution is only word-split, but '' isn't interpreted as an empty argument, but passed verbosely to echo (and thus, dialog wouldn't display no descriptions for the menu items, but literally ''s).

I can work around this in bash using arrays, but is there a simpler solution I'm missing?

Given

$ e() { printf "tag: [$1] item: [$2]"; }
$ e $(echo "A ''")
$ tag: [A] item: ['']

How can I change the $(...) part, such that the item is [] instead of [''].

4

3 回答 3

2

好问题。

我不知道这是否符合“更简单”的条件,但它有一定的优雅:

with-tags() {
  local line
  if read -r line; then with-tags "$@" "$line" ""; else "$@"; fi
}

{ echo A; echo B; echo C; } |
with-tags dialog --menu Choose: 0 40 10

处理以下形式的输入行很容易扩展TAG <optional description>

with-tag-lines() {
  local tag desc
  if read -r tag desc; then with-tag-lines "$@" "$tag" "$desc"; else "$@"; fi
}

{ echo A with optional description; echo B; echo C; } |
with-tag-lines dialog --menu Choose: 0 40 10
于 2013-08-04T21:44:45.450 回答
1

您可以更改IFS(内部字段分隔符)

$ IFS=, e $(echo "a,,")
tag: [a] item: []

似乎工作。是不是不错?我不知道,但会尝试一些数组魔法。顺便说一句,您通常可以使用${parameter/pattern/string}for 替换而不是调用sed. 不幸的是,它只对变量进行操作,这使得它不太可用。

于 2013-08-04T19:25:59.000 回答
0

你可以试试这个:

cmd="dialog --menu Choose: 0 40 10 "
arg="`echo $({ echo A; echo B; echo C; } | sed -e "s/\$/ ''/;")`"
eval "${cmd}${arg}"

但我认为sed这里不需要。你可以保持简单:

cmd="dialog --menu Choose: 0 40 10 "
arg="`echo A \'\' B \'\' C \'\'`"
eval "${cmd}${arg}"
于 2013-08-04T19:10:44.927 回答