我收到错误“array.sh:3:array.sh:语法错误:"(“意外") [英] I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
问题描述
我编写了以下代码:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
我收到错误消息:array.sh: 3: array.sh: 语法错误:("意外
And I am getting error: array.sh: 3: array.sh: Syntax error: "(" unexpected
据我从 Google 了解到的情况,这可能是因为 Ubuntu 现在默认情况下不采用#!/bin/bash"...但我再次添加了该行,但错误是还在.
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.
我也尝试过执行 bash array.sh
但没有成功!它打印空白.
Also I have tried by executing bash array.sh
but no luck! It prints blank.
我的 Ubuntu 版本是:Ubuntu 14.04
My Ubuntu version is: Ubuntu 14.04
推荐答案
鉴于该脚本:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
并假设:
- 它位于您当前目录中名为
array.sh
的文件中; - 你已经完成了
chmod +x array.sh
; - 您在
/bin/bash
中安装了足够新的 bash 版本(您报告说您有 4.3.8,这当然足够新);和 - 你正确地执行它
- 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
那么应该可以正常工作.
then that should work without any problem.
如果您通过键入执行脚本
If you execute the script by typing
./array.sh
系统会注意#!/bin/bash
这一行,使用/bin/bash
执行脚本.
the system will pay attention to the #!/bin/bash
line and execute the script using /bin/bash
.
如果您通过键入以下内容来执行它:
If you execute it by typing something like:
sh ./array.sh
然后它将使用 /bin/sh
执行它.在 Ubuntu 上,/bin/sh
通常是到 /bin/dash
的符号链接,这是一个类似 Bourne 的 shell,不支持数组.这将准确地为您提供您报告的错误消息.
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't support arrays. That will give you exactly the error message that you report.
用于执行脚本的 shell 不受您当前使用的 shell 或在 /etc/passwd
或等效项中配置为登录 shell 的 shell 的影响(除非您使用source
或 .
命令).
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).
在您自己的回答中,您说您通过使用 chsh
将默认登录 shell 更改为 /bin/bash
解决了问题.这本身不应该有任何影响.(无论如何,/bin/bash
是 Ubuntu 上的默认登录 shell;您之前是否将其更改为其他内容?)
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?)
一定发生的事情是您在没有意识到的情况下将您使用的命令从 sh ./array.sh
更改为 ./array.sh
.
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
并查看是否出现相同的错误.
Try running sh ./array.sh
and see if you get the same error.
这篇关于我收到错误“array.sh:3:array.sh:语法错误:"(“意外")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!