在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]}"
哪个外壳允许
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]}
最佳答案
这是Bash中的数组赋值。从索引0开始,$output
扩展后的单词变成数组元素。
例如:
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"
见:
GNU documentation: Word splitting
Greg's wiki: Word splitting
Reading a delimited string into an array in Bash
关于bash - oarr =($ output)在shell中做什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49370809/