找不到 Bash 中的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array in Bash Not Found
提问by Waffle
I am trying to declare an array in bash, but when the code is run it says it cannot find the array. I have tried to write out the declaration of the array in several different ways, but it seems no matter how I try to declare it I cannot get it to work. I originally tried to declare it as such:
我试图在 bash 中声明一个数组,但是当代码运行时它说它找不到该数组。我试图以几种不同的方式写出数组的声明,但似乎无论我如何声明它,我都无法让它工作。我最初试图这样声明它:
candidate[1]= 0
candidate[2]= 0
candidate[3]= 0
The error messages that are returned are:
返回的错误消息是:
votecalculation.sh: 13: candidate[1]=: not found
votecalculation.sh: 14: candidate[2]=: not found
votecalculation.sh: 15: candidate[3]=: not found
After this I tried another solution I found online:
在此之后,我尝试了另一个我在网上找到的解决方案:
ARRAY=( 'can1' 'can2' 'can3' )
When that is used it returns this error:
使用它时,它会返回此错误:
votecalculation.sh: 12: Syntax error: "(" unexpected
I am new to Bash and am getting really confused about arrays. Is there some specific way I need to declare an array or am I just going about it completely wrong?
我是 Bash 的新手,对数组感到非常困惑。是否有某种特定的方式我需要声明一个数组,或者我只是完全错误?
采纳答案by Bryan
#!/bin/bash
myarray[0]=hello
myarray[1]=world
echo ${myarray[0]}
echo ${myarray[1]}
save that to helloworld.bash
将其保存到 helloworld.bash
execute using . ./helloword.bash
使用 . ./helloword.bash
回答by Chris AtLee
It probably doesn't like the space after the equals sign.
它可能不喜欢等号后面的空格。
Some other ideas:
其他一些想法:
Be sure that you're actually using bash to run your script, and not sh/dash.
You can explicitly declare a variable to be an array using
declare -a varname
确保您实际上是在使用 bash 来运行脚本,而不是 sh/dash。
您可以使用显式声明变量为数组
declare -a varname
回答by Emilie Picard-Cantin
I had the same problem. I tried to call my script with ./my_script.s, with . ./my_script.sh and I also tried with "bash my_script.sh" and "sh my_script.sh". I always got the same message : "my_array[1]: command not found".
我有同样的问题。我试图用 ./my_script.s 调用我的脚本,用 . ./my_script.sh,我也尝试过“bash my_script.sh”和“sh my_script.sh”。我总是收到相同的消息:“my_array[1]: command not found”。
Then I saw Chris AtLee's comment about bash not liking the space after the equal sign.
然后我看到 Chris AtLee 关于 bash 不喜欢等号后面的空格的评论。
The exact lines in my code before
我之前代码中的确切行
my_array[1] = 34
my_array[2] = 57
I removed the spaces before and after the equal signs.
我删除了等号前后的空格。
my_array[1]=34
my_array[2]=57
Then, I simply tried the following in the terminal and didn't have the error message anymore.
然后,我只是在终端中尝试了以下操作,并且不再有错误消息。
$ my_script.sh
NOTE: Bash does not like spaces in variable definition !
注意:Bash 不喜欢变量定义中的空格!
Hope this helps other beginners like me !
希望这可以帮助像我这样的其他初学者!
回答by David Z
Try removing the space:
尝试删除空格:
candidate[1]=0
candidate[2]=0
and so on. I'm not an expert in this area myself but I think bash needs to recognize the whole assignment expression as one word, so you can't have spaces in it.
等等。我本人不是这方面的专家,但我认为 bash 需要将整个赋值表达式识别为一个单词,因此其中不能有空格。
回答by John Kugelman
In the first one there should be no spaces after the equal signs.
在第一个中,等号后不应有空格。
candidate[1]=0
candidate[2]=0
candidate[3]=0
The second one looks correct. Are you sure your shell is bash? Try adding a proper hash-bang line to the top of your script if you don't already have it:
第二个看起来是正确的。你确定你的shell是bash吗?如果您还没有它,请尝试在脚本顶部添加适当的 hash-bang 行:
#!/bin/bash
ARRAY=( 'can1' 'can2' 'can3' )
回答by Paused until further notice.
If you have the correct shebang and you chmod +x scriptname, you don't need to start the script using bash scriptname- you can just use ./scriptnameor if the directory it's in is in your PATH, then you can start it using simply scriptname.
如果你有正确的家当,你chmod +x scriptname,你并不需要开始使用脚本bash scriptname-你可以只使用./scriptname或者如果它是在该目录是在你的PATH,那么你可以使用简单的启动它scriptname。
If you have #!/bin/bashas your shebang and run sh scriptnamethen the shebang is overridden by the choice of shell on the command line.
如果您有#!/bin/bash作为您的shebang 并运行,sh scriptname那么shebang 将被命令行上的shell 选择覆盖。
There's no special meaning to having .shor .bashat the end of the filename. It's just a matter of style or preference that some people like since it's intended to indicate the type of script (but only to the user - not to the system).
在文件名的末尾有.sh或没有特殊含义.bash。这只是一些人喜欢的风格或偏好问题,因为它旨在指示脚本的类型(但仅对用户 - 而不是系统)。
回答by user11234014
May be, bad declaration of shebang... Be sure, use #!/bin/bash as shebang
可能是,shebang 的错误声明...请确保使用 #!/bin/bash 作为 shebang

