【问题标题】:Process Substitution For Each Array Entry, Without Eval处理每个数组条目的替换,没有 Eval
【发布时间】:2018-12-05 00:33:09
【问题描述】:

我有一个任意字符串数组,例如a=(1st "2nd string" $'3rd\nstring\n' ...)
我想将这些字符串传递给将其参数解释为文件的命令,例如paste

对于固定数量的变量,我们可以使用进程替换

paste <(printf %s "$var1") <(printf %s "$var2") <(printf %s "$var3")

但这只有在事先知道变量数量的情况下才有效。
对于数组a,我们可以写一些相当安全的东西,比如

eval paste $(printf '<(printf %%s %q) ' "${a[@]}")

出于兴趣:有没有一种方法可以在不使用eval 的情况下处理替换a 的每个条目?请记住,a 的条目可以包含任何字符(\0 除外,因为bash 不支持它)。

【问题讨论】:

  • printf %s "$var1""$var1" 几乎是等价的;这些只是更复杂命令的占位符吗?
  • @chepner 也许我错了,但对我来说printf 似乎是必要的。这个问题中唯一的占位符是pastea 中的值。如果不是&lt;(printf %s "$var1"),您将如何模拟与$var1 具有相同内容的文件?
  • 哦,对了,您希望$var1 作为(伪)匿名文件的内容

标签: arrays bash process-substitution


【解决方案1】:

这是一个示例,说明如何使用递归来一次设置一个参数列表。该技术有时很有用。

使用进程替换将文本转换为管道可能不是当前问题的最佳解决方案,但它确实具有重用现有工具的优点。

我试图使代码合理通用,但可能需要进行更多调整。

nameref 需要 Bash 4.3(尽管如果您尚未达到该版本,您可以使用固定的数组名称来执行此操作)。 Namerefs 需要谨慎,因为它们不卫生;可以按名称捕获局部变量。因此使用以下划线开头的变量名。

# A wrapper which sets up for the recursive call
from_array() {
  local -n _array=$1
  local -a _cmd=("${@:2}")
  local -i _count=${#_array[@]}
  from_array_helper
}

# A recursive function to create the process substitutions.
# Each invocation adds one process substitution to the argument
# list, working from the end.
from_array_helper() {
  if (($_count)); then
    ((--_count))
    from_array_helper <(printf %s "${_array[_count]}") "$@"
  else
    "${_cmd[@]}" "$@"
  fi
}

例子

$ a=($'first\nsecond\n' $'x\ny\n' $'27\n35\n')
$ from_array a paste -d :
first:x:27
second:y:35

【讨论】:

  • 好主意,谢谢!我使用您的方法创建了一个version,而不会发生名称冲突。但是,这两个版本都有点长。我保留这个问题是为了鼓励人们找到更好的解决方案。
【解决方案2】:

这个解决方案的灵感来自rici's answer。 它解决了 namerefs 可能导致的名称冲突,但需要用户指定一个不会出现在要执行的命令中的分隔符。不过,分隔符可以毫无问题地出现在数组中。

# Search a string in an array
# and print the 0-based index of the first identical element.
# Usage: indexOf STRING "${ARRAY[@]}"
# Exits with status 1 if the array does not contain such an element.
indexOf() {
    search="$1"
    i=0
    while shift; do
        [[ "$1" = "$search" ]] && echo "$i" && return
        ((++i))
    done
    return 1
}

# Execute a command and replace its last arguments by anonymous files.
# Usage: emulateFiles DELIMITER COMMAND [OPTION]... DELIMITER [ARGUMENT]...
# DELIMITER must differ from COMMAND and its OPTIONS.
# Arguments after the 2nd occurrence of DELIMITER are replaced by anonymous files.
emulateFiles() {
    delim="$1"
    shift
    i="$(indexOf "$delim" "$@")" || return 2
    cmd=("${@:1:i}")
    strings=("${@:i+2}")
    if [[ "${#strings[@]}" = 0 ]]; then
        "${cmd[@]}"
    else
        emulateFiles "$delim" "${cmd[@]}" <(printf %s "${strings[0]}") \
                     "$delim" "${strings[@]:1}"
    fi
}

使用示例

a=($'a b\n c ' $'x\ny\nz\n' : '*')
$ emulateFiles : paste : "${a[@]}"
a b x   :   *
 c  y       
    z       
$ emulateFiles : paste -d: : "${a[@]}"   # works because -d: != :
a b:x:::*
 c :y::
:z::
$ emulateFiles delim paste -d : delim "${a[@]}"
a b:x:::*
 c :y::
:z::

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多