oarr =($ output)在shell中做什么?

时间:2018-03-19 19:22:27

标签: bash shell

https://www.virtuability.com/public/wp/?p=12上我看到了这个符号:

oarr=($output)

export AWS_ACCESS_KEY_ID="${oarr[1]}"
export AWS_SECRET_ACCESS_KEY="${oarr[3]}"
export AWS_SESSION_TOKEN="${oarr[4]}"

哪个shell允许oarr=($output)

输出将是这样的:

CREDENTIALS EUROIWPPEACEGPNASIA 2017-03-19T23:09:47Z z66Pku1EFb6dCP1+02RWzRhaYEGPpLy6xcjZz3rr FqODYXdzEMT//////////wEaDNYYo0b6nFVNB2mLsCKvAW2+69FQoDlxLFeBYfznVdS67QPGfFiRvMDd4f5VxkHosv2oFtXAHu8IedzzXT/Ex2P2Gce6Y2b8yBwzylaZAAu53SW9pesjunVprkzNVA3IznRj4hlTTgx8DTos4n+qDEfElv5lEvYKaNg2ER7/BtXTdzAwTNu1QHiMvNVySHnvZHgW5G5oHBEnYgsyR1guxyP/8hiRyR3nuUE0BMIl5+LVBaYaP637HlAXHQ+83KUo+5Ya1QU=

/bin/sh/bin/bash我只会空${oarr[1]} ${oarr[3]} ${oarr[4]}

1 个答案:

答案 0 :(得分:4)

这是Bash中的数组赋值。扩展$output的单词变为数组元素,从索引0开始。

例如:

string="one two three"
arr=($string)   # Bash does word splitting (see doc links below)
                # and globbing (wildcards '*', '?' and '[]' will be expanded to matching filenames)

declare -p arr  # gives declare -a arr='([0]="one" [1]="two" [2]="three")'
arr=("$string") # Word splitting is suppressed because of the quotes - but this won't be useful because the entire string ends up as the first element of the array
declare -p arr  # gives declare -a arr='([0]="one two three")'

为防止单词拆分和通配,将空格分隔的字符串转换为数组的正确方法是:

read -r -a oarr <<< "$output"

运行declare -p oarr以验证阵列的内容。这将告诉您当前代码中${oarr[1]} ${oarr[3]} ${oarr[4]}为空的原因。

如果您有不同的分隔符,请说:,然后:

IFS=: read -r -a arr <<< "$string"

请参阅: