您的位置:首页 > 运维架构 > Linux

shell脚本数组报错Syntax error: "(" unexpected-centos易出现问

2018-01-03 22:40 871 查看
按照正常的shell数组定义,例如3.sh

cat 3.sh
#!/bin/sh
a=( 1 2 3)
for number in ${a[@]}
do
echo $number
done

执行命令
sh 3.sh
3.sh: 2: 3.sh: Syntax error: "(" unexpected
执行该脚本,在有的机器上会报错Syntax error: "(" unexpected

bash 3.sh
1
2
3
与你实际使用的shell版本有关。你可以用 ls -l /bin/*sh 打印出来,例如:
ls -l /bin/*sh
-rwxr-xr-x 1 root root 1021112 Oct  7  2014 /bin/bash
-rwxr-xr-x 1 root root  121272 Feb 19  2014 /bin/dash
lrwxrwxrwx 1 root root       4 Dec  4 01:40 /bin/rbash -> bash
lrwxrwxrwx 1 root root       4 Dec  4 01:40 /bin/sh -> dash
lrwxrwxrwx 1 root root       7 Nov 14  2013 /bin/static-sh -> busybox

在这里,sh被重定向到dash,因此,如果执行./3.sh,则使用的是dash
避免报错可有多种方法,例如执行 bash example.sh,或者,将脚本第一行改为
#!/bin/bash,执行./3.sh也可以。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sh: Syntax error: