2015-01-20 53 views
4

我正在使用:s3-bash,当我在本地环境中运行它时(OS X 10.10.1)我没有任何问题,当我尝试在ubuntu server 14.04.1上运行它时出现以下错误:Bash unbound variable array(script:s3-bash)

./s3-common-functions: line 66: temporaryFiles: unbound variable 
./s3-common-functions: line 85: temporaryFiles: unbound variable 

我已经看过了s3-common-functions脚本和变量看起来是正确初始化(作为数组):

# Globals 
declare -a temporaryFiles 

但有记下评论,我相信它是否相关:

阵列为 temporaryfiles

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" 
} 
+1

'unbound variable'是你在使用'set -u'的时候得到的。您是否在任何环境下设置了该脚本? – 2015-01-20 21:23:28

+1

导致此错误的命令是什么? – 2015-01-20 21:28:56

+2

错误?评论文字中描述的内容不是一个错误,而是正常和预期的行为。 'foo = $(bar)'在子shell中运行'bar',所以当然在那个子shell中完成的**赋值不会传播到父shell。 – 2015-01-20 22:19:34

回答

9

这里玩起来似乎有一个bash行为改变。

由小次郎发现:CHANGES

hhhh. Fixed a bug that caused `declare' and `test' to find variables that had been given attributes but not assigned values. Such variables are not set.

$ 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=()在新的bash的版本来解决THI秒。

$ declare -a tF=() 
$ echo "${#tF[@]}" 
0 
+3

在bash-4.2-release和bash-4.3-alpha之间进行了修改:[_hhhh。修正了导致\'声明'和\'测试'以查找已赋予属性但未赋值的变量的错误。这些变量没有设置。(https://tiswww.case.edu/php/chet/bash/CHANGES) - (对不起,txt不允许更精确的链接,但是如果你喜欢,你可以搜索确切的文字) – kojiro 2015-01-21 03:11:26

+1

@kojiro感谢您的发现。 – 2015-01-21 03:16:59

0

更改声明:

temporaryFiles=() 

为什么这是ubuntu 14.04.1 Linux 3.13.0-32-generic x86_64OS X不同/非功能我不知道?

2

Bash可以使用短划线将空值替换为未设置的变量。

set -u 
my_array=() 
printf "${my_array[@]-}\n" 

这个特定的例子不会打印任何东西,但它也不会给你一个未绑定的变量错误。

Stolen from here

+0

尝试查找大小时不起作用。即$ {#my_array [@] - }会产生一个错误的替换错误。 – kenj 2017-04-09 17:26:47