如何在Bash脚本中将多个数组作为参数传递?

11

我已经搜索过,但只看到有关在脚本中传递一个数组的答案。

我想将多个数组传递给一个Bash脚本,并将它们分配为以下单独的变量:

./myScript.sh ${array1[@]} ${array2[@]} ${array3[@]}

如下: var1=array1var2=array2,以及var3=array3

我尝试了多种选项,但使用variableName=("$@")将所有数组合并到每个变量中。我希望在我的 Bash 脚本中有一个变量代表每个数组。


数组中包含任意数据,还是有一个您知道不会出现在数据中的字符?例如,如果您的数组不包含逗号之类的内容,我可以在几分钟内提供解决方案。 - Jeffrey Cash
Bash变量非常简单,你不能像这样有嵌套结构。 - Barmar
@Barmar 你可以用一点小技巧来实现。 - Jeffrey Cash
1
命令行传递的参数是字符串。如果您想要强制实施结构,您需要自己序列化它。(例如,传入JSON) - William Pursell
顺便提一下--引号很重要。假设默认IFS值,array1=("hello world" "goodbye world")将通过"${array1[@]}"扩展为两个值,但通过未加引号的${array1[@]}将扩展为四个值。 - Charles Duffy
1
(另外说一句 - 我倾向于建议不要使用“.sh”扩展名。可执行脚本定义命令,而命令通常不具有扩展名:人们不运行“ls.elf”或“systriage.py”;如果将命令在不同语言中重新编写但未更新所有调用者,则扩展名可能会变得具有误导性;而且,一个暗示符合POSIX sh的扩展名可能会导致人们使用需要ksh或bash扩展名才能正确运行的脚本与不提供相同功能的解释器进行调用)。 - Charles Duffy
5个回答

14

Shell将一个单一参数向量(换句话说,一个简单的C字符串数组)传递给正在运行的程序。这是一个操作系统级别的限制:除了通过将结构编码为此C字符串数组成员的内容之外,(使用任何语言编写的)两个程序之间没有办法在参数列表中传递结构化数据。


方法:长度前缀

如果效率是目标(无论是解析易用性还是在命令行和环境存储的ARG_MAX限制下所使用的空间量),则考虑采用一种方法是在每个数组前加上描述其长度的参数。

但是,通过提供长度参数,您可以指示该参数列表的哪些部分应该是给定数组的一部分:

./myScript \
  "${#array1[@]}" "${array1[@]}" \
  "${#array2[@]}" "${array2[@]}" \
  "${#array3[@]}" "${array3[@]}"

然后,在脚本中,你可以使用长度参数将内容分割回数组:

#!/usr/bin/env bash

array1=( "${@:2:$1}" ); shift "$(( $1 + 1 ))"
array2=( "${@:2:$1}" ); shift "$(( $1 + 1 ))"
array3=( "${@:2:$1}" ); shift "$(( $1 + 1 ))"

declare -p array1 array2 array3

如果以./myScript 3 a b c 2 X Y 1 z的形式运行,将会得到以下输出:

declare -a array1='([0]="a" [1]="b" [2]="c")'
declare -a array2='([0]="X" [1]="Y")'
declare -a array3='([0]="z")'

方法:逐参数数组名称前缀

顺便提一下,在 Python 世界中(特别是使用 argparse 库的用户),一种常见做法是允许多次传递同名参数来修改给定的数组。在 shell 中,它看起来像这样:

./myScript \
  "${array1[@]/#/--array1=}" \
  "${array2[@]/#/--array2=}" \
  "${array3[@]/#/--array3=}"

然后解析它的代码可能如下:

#!/usr/bin/env bash
declare -a args array1 array2 array3
while (( $# )); do
  case $1 in
    --array1=*) array1+=( "${1#*=}" );;
    --array2=*) array2+=( "${1#*=}" );;
    --array3=*) array3+=( "${1#*=}" );;
    *)          args+=( "$1" );;
  esac
  shift
done

因此,如果您的原始值为array1=( one two three ) array2=( aye bee ) array3=( "hello world" ),则调用约定如下:

./myScript --array1=one --array1=two --array1=three \
           --array2=aye --array2=bee \
           --array3="hello world"

方法:NUL-Delimited Streams

另一种方法是传递每个数组的文件名,其中可以从该文件中读取其内容的NUL分隔列表。这种方法的主要优点是,数组内容的大小不会计入ARG_MAX,即操作系统强制的命令行长度限制。此外,在具有此类功能的操作系统中,下面的代码不会创建真正的硬盘文件,而是创建/dev/fd样式的链接到由子shell写入的FIFO(每个数组的内容)。

./myScript \
  <( (( ${#array1[@]} )) && printf '%s\0' "${array1[@]}") \
  <( (( ${#array2[@]} )) && printf '%s\0' "${array2[@]}") \
  <( (( ${#array3[@]} )) && printf '%s\0' "${array3[@]}")

…并且,要读取(使用bash 4.4或更高版本,并提供mapfile -d):

#!/usr/bin/env bash
mapfile -d '' array1 <"$1"
mapfile -d '' array2 <"$2"
mapfile -d '' array3 <"$3"

...或者为了支持旧版的bash:

#!/usr/bin/env bash
declare -a array1 array2 array3
while IFS= read -r -d '' entry; do array1+=( "$entry" ); done <"$1"
while IFS= read -r -d '' entry; do array2+=( "$entry" ); done <"$2"
while IFS= read -r -d '' entry; do array3+=( "$entry" ); done <"$3"

在第二种情况下,我认为您会想要 "${1#*=}" 或类似的东西。 - ephemient
1
这个是一个值得保留的东西,可以放在旧的bash工具箱中,在将数组作为参数传递时进行合并之后。做得很好。 - David C. Rankin

0

我更喜欢使用base64对数组进行编码和解码,例如:

encode_array(){
    local array=($@)
    echo -n "${array[@]}" | base64
} 
decode_array(){
    echo -n "$@" | base64 -d
}
some_func(){
    local arr1=($(decode_array $1))
    local arr2=($(decode_array $2))
    local arr3=($(decode_array $3))
    echo arr1 has ${#arr1[@]} items, the second item is ${arr1[2]} 
    echo arr2 has ${#arr2[@]} items, the third item is ${arr2[3]} 
    echo arr3 has ${#arr3[@]} items, the here the contents ${arr3[@]} 
}

a1=(ab cd ef)
a2=(gh ij kl nm)
a3=(op ql)
some_func "$(encode_array "${a1[@]}")" "$(encode_array "${a2[@]}")" "$(encode_array "${a3[@]}")"

输出结果为

arr1 has 3 items, the second item is cd
arr2 has 4 items, the third item is kl
arr3 has 2 items, the here the contents op ql

无论如何,这对于具有制表符或空格的值是行不通的。如果需要,我们需要更详细的解决方案。例如:

encode_array()
{
        for item in "$@";
        do
                echo -n "$item" | base64
        done | paste -s -d , -
}

decode_array()
{
        local IFS=$'\2'

        local -a arr=($(echo "$1" | tr , "\n" |
                while read encoded_array_item;
                do 
                        echo  "$encoded_array_item" | base64 -d;
                        echo "$IFS"
                done))
        echo "${arr[*]}";
}

test_arrays_step1()
{
        local IFS=$'\2'
        local -a arr1=($(decode_array $1))
        local -a arr2=($(decode_array $2))
        local -a arr3=($(decode_array $3))
        unset IFS
        echo arr1 has ${#arr1[@]} items, the second item is ${arr1[1]} 
        echo arr2 has ${#arr2[@]} items, the third item is ${arr2[2]} 
        echo arr3 has ${#arr3[@]} items, the here the contents ${arr3[@]} 
}

test_arrays()
{
        local a1_2="$(echo -en "c\td")";
        local a1=("a b" "$a1_2" "e f");
        local a2=(gh ij kl nm);
        local a3=(op ql );
        a1_size=${#a1[@])};
        resp=$(test_arrays_step1 "$(encode_array "${a1[@]}")" "$(encode_array "${a2[@]}")" "$(encode_array "${a3[@]}")");
        echo -e "$resp" | grep arr1 | grep "arr1 has $a1_size, the second item is $a1_2" || echo but it should have only $a1_size items, with the second item as $a1_2
        echo "$resp"
}


0

这里有一个代码示例,展示了如何将2个数组传递给函数。除了提供完整的代码示例外,与之前的答案没有什么不同。

这是用bash 4.4.12编写的,即在bash 4.3之后,需要采用不同的编码方法。一个数组包含要着色的文本,另一个数组包含要用于每个文本元素的颜色:

function cecho_multitext () {
  # usage : cecho_multitext message_array color_array
  # what it does : Multiple Colored-echo.

    local -n array_msgs=$1
    local -n array_colors=$2
    # printf '1: %q\n' "${array_msgs[@]}"
    # printf '2: %q\n' "${array_colors[@]}"


    local i=0
    local coloredstring=""
    local normalcoloredstring=""

    # check array counts
    # echo "msg size : "${#array_msgs[@]}
    # echo "col size : "${#array_colors[@]}

    [[ "${#array_msgs[@]}" -ne "${#array_colors[@]}" ]] && exit 2

    # build the colored string
    for msg in "${array_msgs[@]}"
    do
      color=${array_colors[$i]}
      coloredstring="$coloredstring $color $msg "
      normalcoloredstring="$normalcoloredstring $msg"
      # echo -e "coloredstring ($i): $coloredstring"
      i=$((i+1))
    done

    # DEBUG
    # echo -e "colored string : $coloredstring"
    # echo -e "normal color string : $normal $normalcoloredstring"

    # use either echo or printf as follows :
    # echo -e "$coloredstring"
    printf '%b\n' "${coloredstring}"

    return
}

调用函数:

#!/bin/bash
green='\E[32m'
cyan='\E[36m'
white='\E[37m'
normal=$(tput sgr0)
declare -a text=("one" "two" "three" )
declare -a color=("$white" "$green" "$cyan")
cecho_multitext text color

任务完成 :-)


0

Charles Duffy的回答完全有效,但我会用不同的方式来使初始化var1var2var3在您的脚本中更简单:

./myScript.sh "${#array1[@]} ${#array2[@]} ${#array3[@]}" \
 "${array1[@]}" "${array2[@]}" "${array3[@]}"

然后在myScript.sh

#!/bin/bash
declare -ai lens=($1);
declare -a var1=("${@:2:lens[0]}") var2=("${@:2+lens[0]:lens[1]}") var3=("${@:2+lens[0]+lens[1]:lens[2]}");

编辑:由于Charles简化了他的解决方案,它可能比我的更好、更清晰。


-1
根据对这个问题的回答,你可以尝试以下方法。
在shell中将数组定义为变量:
array1=(1 2 3)
array2=(3 4 5)
array3=(6 7 8)

有一个像这样的脚本:

arg1=("${!1}")
arg2=("${!2}")
arg3=("${!3}")


echo "arg1 array=${arg1[@]}"
echo "arg1 #elem=${#arg1[@]}"

echo "arg2 array=${arg2[@]}"
echo "arg2 #elem=${#arg2[@]}"

echo "arg3 array=${arg3[@]}"
echo "arg3 #elem=${#arg3[@]}"

并像这样调用它:

. ./test.sh "array1[@]" "array2[@]" "array3[@]"

请注意,脚本需要被引用(. 或 source)以便在当前的 shell 环境中执行而不是在子 shell 中执行。

1
嗯?基本的shell变量可以被导出到环境中,因此对子进程可用,但是数组不行。这个应该怎么做呢? - Charles Duffy
这种方法适用于函数,但我相信@CharlesDuffy是正确的。 - Jeffrey Cash
Charles,你是正确的。然而,正如anubhava在链接问题的答案中所解释的那样,诀窍在于使用“.”或“source”命令来调用脚本,以便在当前shell环境中执行脚本,而不是在子shell中执行。 - saxitoxin
在需要被调用才能工作的东西中加入shebang会有点误导。 - Charles Duffy
@CharlesDuffy 是的,你又一次正确了。我将编辑答案以提高清晰度并将其删除。 - saxitoxin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接