我收到错误" array.sh:3:array.sh:语法错误:"("意外" [英] I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"

查看:225
本文介绍了我收到错误" array.sh:3:array.sh:语法错误:"("意外"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的code:

I have written the following code:

#!/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

从我来自谷歌就知道了,这可能是由于这样的事实,Ubuntu是现在不采取#!/斌/ 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.

此外,我已经通过执行尝试,但没有运气!它打印空白。

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 ;

  • 您已经安装在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

系统会注意#!/斌/庆典行,并使用 /斌/庆典

the system will pay attention to the #!/bin/bash line and execute the script using /bin/bash.

如果你输入类似执行:

sh ./array.sh

然后它会使用 / bin / sh的执行。在Ubuntu上, / bin / sh的通常是一个符号链接 /斌/破折号,一个类似Bourne外壳,的的支持数组。这会给你正是您报告的错误消息。

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文件或当量(除非您使用命令)。

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更改为 /斌/庆典。这本身不应有任何影响。 (和 /斌/庆典是在Ubuntu默认的登录shell呢;你有它改成别​​的previously)

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆