Bash 未绑定变量数组(脚本:s3-bash)
Bash unbound variable array (script: s3-bash)
我正在使用: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/$$..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"
}
更改了 temporaryfiles
的数组声明
declare -a temporaryFiles
至:
temporaryFiles=()
为什么这在 ubuntu 14.04.1 Linux 3.13.0-32-generic x86_64
和 OS X
中不同/不起作用我不确定?
这里似乎有一个 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
您可以在较新的 bash 版本上使用 declare -a tF=()
来解决这个问题。
$ declare -a tF=()
$ echo "${#tF[@]}"
0
Bash 可以使用破折号将空值替换为未设置的变量。
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[@]}
为什么for循环里面的${#arr[@]}结果是正确的,而外面的结果是0?
我正在使用: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/$$..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"
}
更改了 temporaryfiles
declare -a temporaryFiles
至:
temporaryFiles=()
为什么这在 ubuntu 14.04.1 Linux 3.13.0-32-generic x86_64
和 OS X
中不同/不起作用我不确定?
这里似乎有一个 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
您可以在较新的 bash 版本上使用 declare -a tF=()
来解决这个问题。
$ declare -a tF=()
$ echo "${#tF[@]}"
0
Bash 可以使用破折号将空值替换为未设置的变量。
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[@]}
为什么for循环里面的${#arr[@]}结果是正确的,而外面的结果是0?