我正在使用:s3-bash,当我在本地环境中运行它时OS X 10.10.1
)当我尝试在ubuntu server 14.04.1
上运行它时,我没有任何问题我收到以下错误:
./s3-common-functions: line 66: temporaryFiles: unbound variable
./s3-common-functions: line 85: temporaryFiles: unbound variable
我查看了s3-common-functions
脚本,变量看起来正确初始化(作为数组):
# Globals
declare -a temporaryFiles
但是评论中有一个注释,我确定它是否相关:
# 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"
}
答案 0 :(得分:9)
这里似乎有一个bash行为改变。
由kojiro发现:CHANGES
HHHH。修复了导致`declare'和`test'查找变量的错误 已被赋予属性但未赋值。这些变量是 没有设定。
$ 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
VS
$ 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
VS
$ 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
您可以在较新的bash版本上使用declare -a tF=()
来解决此问题。
$ declare -a tF=()
$ echo "${#tF[@]}"
0
答案 1 :(得分:3)
Bash可以使用破折号将空值替换为未设置的变量。
set -u
my_array=()
printf "${my_array[@]-}\n"
此特定示例不会打印任何内容,但它也不会为您提供未绑定的变量错误。
答案 2 :(得分:0)
更改了temporaryfiles
declare -a temporaryFiles
为:
temporaryFiles=()
为什么ubuntu 14.04.1 Linux 3.13.0-32-generic x86_64
与OS X
中的这个不同/不起作用我不确定?
答案 3 :(得分:0)
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[@]}
为什么for循环中的$ {#arr [@]}产生正确的结果,而外面的一个给出0?