2015-01-20 43 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

相關問題