Bash未绑定变量数组(脚本:s3bash)

Bash未绑定变量数组(脚本:s3bash),bash,Bash,我正在使用:,当我在本地环境(OSX10.10.1)中运行它时,我没有任何问题,当我尝试在ubuntu服务器14.04.1上运行它时,我得到以下错误: ./s3-common-functions: line 66: temporaryFiles: unbound variable ./s3-common-functions: line 85: temporaryFiles: unbound variable 我已经查看了s3通用函数脚本,变量似乎已正确初始化(作为数组): 但评论中有一条注释,

我正在使用:,当我在本地环境(
OSX10.10.1
)中运行它时,我没有任何问题,当我尝试在
ubuntu服务器14.04.1
上运行它时,我得到以下错误:

./s3-common-functions: line 66: temporaryFiles: unbound variable
./s3-common-functions: line 85: temporaryFiles: unbound variable
我已经查看了
s3通用函数
脚本,变量似乎已正确初始化(作为数组):

但评论中有一条注释,我确信它是否相关:

# Do not use this from directly. Due to a bug in bash, array assignments do not work when the function is used with command substitution
function createTemporaryFile
{
    local temporaryFile="$(mktemp "$temporaryDirectory/$$.$1.XXXXXXXX")" || printErrorHelpAndExit "Environment Error: Could not create a temporary file. Please check you /tmp folder permissions allow files and folders to be created and disc space." $invalidEnvironmentExitCode
    local length="${#temporaryFiles[@]}"
    temporaryFiles[$length]="$temporaryFile"
}

更改了
临时文件的数组声明

declare -a temporaryFiles
致:


为什么这在
ubuntu14.04.1linux 3.13.0-32-generic x86_64
osx
中不同/不起作用?我不确定?

这里似乎有一个bash行为变化

由kojiro发现:

啊。修复了导致“declare”和“test”查找 已指定属性,但未指定值。这些变量是 未设置

vs


Bash可以使用破折号将空值替换为未设置的变量

set -u
my_array=()
printf "${my_array[@]-}\n"
这个特定的示例将不打印任何内容,但也不会给出未绑定变量错误


为什么for循环内部的${arr[@]}产生正确的结果,而外部的结果为0?

未绑定变量
是使用
set-u
时得到的结果。您在运行脚本的任何环境中都设置了该设置吗?您运行的导致此错误的命令是什么?错误?注释文本中描述的不是bug,而是正常和预期的行为
foo=$(bar)
在子shell中运行
bar
,因此在该子shell中完成的任务当然不会传播到父shell。(我想知道bash试图支持的bash解释器s3有多古老;附加到数组的特定方法已经有一段时间没有必要了,新语法是
temporaryFiles+=)“$temporaryFile”)
…当然,这不会改变变量从子shell到父shell的传播)@EtanReisner查看上面几行的
set-u
以及
set-e
,我不是bash专家,所以我不确定效果如何,但我不必设置任何环境变量来运行脚本(我不相信)。读取数组长度
local length=“${ary临时文件[@])时,它似乎也会失败“
&
length=“${#temporaryFiles[@]}”
在bash-4.2-release和bash-4.3-alpha之间进行了更改:–(抱歉,txt不允许更精确的链接,但如果您愿意,可以搜索精确的文本。)@kojiro感谢您的发现。在尝试查找大小时,这将不起作用。例如${#my_array[@]}将生成一个错误的替换错误。这为我解决了这个问题,因为我的Centos 7.7主机运行的是Bash 4.2.46(2),否则,如果我尝试在空数组上迭代,它会出错。
temporaryFiles=()
$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
$ set -u
$ declare -a tF
$ echo "${#tF[@]}"
0
$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ set -u
$ declare -a tF
$ echo "${#tF[@]}"
0
$ bash --version
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ set -u
$ declare -a tF
$ echo "${#tF[@]}"
-bash: tF: unbound variable
$ declare -a tF=()
$ echo "${#tF[@]}"
0
set -u
my_array=()
printf "${my_array[@]-}\n"
find $fullfolder -type f |
while read fullfile
do
    filename=$(basename "$fullfile")
    ext=$([[ $filename = *.* ]] && printf %s ${filename##*.} || printf 'NONE')
    arr+=($ext)
    echo ${#arr[@]}
done
echo ${#arr[@]}