bash 我收到错误“array.sh:3:array.sh:语法错误:”(“意外”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25222259/
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
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
提问by Mistu4u
I have written the following code:
我编写了以下代码:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
And I am getting error: array.sh: 3: array.sh: Syntax error: "(" unexpected
我收到错误:array.sh:3:array.sh:语法错误:“(”意外
From what I came to know from Google, that this might be due to the fact that Ubuntu is now not taking "#!/bin/bash" by default... but then again I added the line but the error is still coming.
根据我从 Google 了解到的情况,这可能是因为 Ubuntu 现在默认不采用“#!/bin/bash”...但我再次添加了该行,但错误仍然存在。
Also I have tried by executing bash array.sh
but no luck! It prints blank.
我也尝试过执行bash array.sh
但没有运气!它打印空白。
My Ubuntu version is: Ubuntu 14.04
我的 Ubuntu 版本是:Ubuntu 14.04
回答by Keith Thompson
Given that script:
鉴于该脚本:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
and assuming:
并假设:
- It's in a file in your current directory named
array.sh
; - You've done
chmod +x array.sh
; - You have a sufficiently new version of bash installed in
/bin/bash
(you report that you have 4.3.8, which is certainly new enough); and - You execute it correctly
- 它位于您当前目录中名为
array.sh
;的文件中。 - 你已经完成了
chmod +x array.sh
; - 您安装了足够新的 bash 版本
/bin/bash
(您报告说您有 4.3.8,这当然足够新);和 - 你正确地执行它
then that should work without any problem.
那么这应该没有任何问题。
If you execute the script by typing
如果您通过键入来执行脚本
./array.sh
the system will pay attention to the #!/bin/bash
line and execute the script using /bin/bash
.
系统会注意该#!/bin/bash
行并使用/bin/bash
.
If you execute it by typing something like:
如果您通过键入以下内容来执行它:
sh ./array.sh
then it will execute it using /bin/sh
. On Ubuntu, /bin/sh
is typically a symbolic link to /bin/dash
, a Bourne-like shell that doesn'tsupport arrays. That will give you exactly the error message that you report.
然后它将使用/bin/sh
. 在 Ubuntu 上,/bin/sh
通常是指向 的符号链接/bin/dash
,一个不支持数组的类似 Bourne 的 shell 。这将准确地为您提供您报告的错误消息。
The shell used to execute a script is not affected by which shell you're currently using or by which shell is configured as your login shell in /etc/passwd
or equivalent (unless you use the source
or .
command).
用于执行脚本的 shell 不受您当前使用的 shell 或配置为登录 shell/etc/passwd
或等效的shell (除非您使用source
or.
命令)的影响。
In your own answer, you say you fixed the problem by using chsh
to change your default login shell to /bin/bash
. That by itself should not have any effect. (And /bin/bash
is the default login shell on Ubuntu anyway; had you changed it to something else previously?)
在您自己的回答中,您说您通过使用chsh
将默认登录 shell 更改为/bin/bash
. 这本身不应该有任何影响。(/bin/bash
无论如何都是Ubuntu上的默认登录shell;您之前是否将其更改为其他内容?)
What must have happened is that you changed the command you use from sh ./array.sh
to ./array.sh
without realizing it.
什么是必须发生的是,你改变你使用命令sh ./array.sh
来./array.sh
而不自知。
Try running sh ./array.sh
and see if you get the same error.
尝试运行sh ./array.sh
,看看是否出现相同的错误。
回答by Fabien Thetis
Instead of using sh to run the script,
而不是使用 sh 来运行脚本,
try the following command:
尝试以下命令:
bash ./array.sh
回答by Mistu4u
I solved the problem miraculously. In order to solve the issue, I found a link where it was described to be gone by using the following code. After executing them, the issue got resolved.
我奇迹般地解决了这个问题。为了解决这个问题,我找到了一个链接,它被描述为使用以下代码消失了。执行它们后,问题得到解决。
chsh -s /bin/bash adhikarisubir
grep ^adhikarisubir /etc/passwd
FYI, "adhikarisubir" is my username.
仅供参考,“adhikarisubir”是我的用户名。
After executing these commands, bash array.sh
produced the desired result.
执行bash array.sh
完这些命令后,就产生了想要的结果。