shell脚本数组报错Syntax error: "(" unexpected
2015-03-03 15:11
375 查看
纠结了半天,原来如此,有错还是早点在网上找找有效率。
以下为转载部分:
——————————————————————————————————————————————————————————————————————————————
按照正常的shell数组定义,例如example.sh
#!/bin/sh
a=( 1 2 3)
for number in ${a[@]}
do
echo $number
done
执行该脚本,在有的机器上会报错Syntax error: "(" unexpected
这与你实际使用的shell版本有关。你可以用 ls -l /bin/*sh打印出来,例如:
-rwxr-xr-x 1 root root 959120 Mar 29 2013/bin/bash
lrwxrwxrwx 1 root root 21 Nov 22 2013 /bin/csh ->/etc/alternatives/csh
-rwxr-xr-x 1 root root 109768 Mar 30 2012/bin/dash
lrwxrwxrwx 1 root root 4 Mar 29 2013 /bin/rbash ->bash
lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh ->dash
lrwxrwxrwx 1 root root 7 Nov 17 2012 /bin/static-sh-> busybox
lrwxrwxrwx 1 root root 13 Oct 15 2011 /bin/tcsh ->/usr/bin/tcsh
在这里,sh被重定向到dash,因此,如果执行./example.sh,则使用的是dash
避免报错可有多种方法,例如执行 bash example.sh,或者,将脚本第一行改为
#!/bin/bash,执行./example.sh也可以。
有关该问题,还可以参考Ubuntu论坛: http://ubuntuforums.org/archive/index.php/t-444304.html
以下为转载部分:
——————————————————————————————————————————————————————————————————————————————
按照正常的shell数组定义,例如example.sh
#!/bin/sh
a=( 1 2 3)
for number in ${a[@]}
do
echo $number
done
执行该脚本,在有的机器上会报错Syntax error: "(" unexpected
这与你实际使用的shell版本有关。你可以用 ls -l /bin/*sh打印出来,例如:
-rwxr-xr-x 1 root root 959120 Mar 29 2013/bin/bash
lrwxrwxrwx 1 root root 21 Nov 22 2013 /bin/csh ->/etc/alternatives/csh
-rwxr-xr-x 1 root root 109768 Mar 30 2012/bin/dash
lrwxrwxrwx 1 root root 4 Mar 29 2013 /bin/rbash ->bash
lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh ->dash
lrwxrwxrwx 1 root root 7 Nov 17 2012 /bin/static-sh-> busybox
lrwxrwxrwx 1 root root 13 Oct 15 2011 /bin/tcsh ->/usr/bin/tcsh
在这里,sh被重定向到dash,因此,如果执行./example.sh,则使用的是dash
避免报错可有多种方法,例如执行 bash example.sh,或者,将脚本第一行改为
#!/bin/bash,执行./example.sh也可以。
有关该问题,还可以参考Ubuntu论坛: http://ubuntuforums.org/archive/index.php/t-444304.html
相关文章推荐
- shell脚本 数组操作报Syntax error: "(" unexpected
- shell脚本数组报错Syntax error: "(" unexpected
- shell脚本数组报错Syntax error: "(" unexpected-centos易出现问
- shell脚本调试报错:"syntax error: unexpected end of file"
- shell脚本----调试脚本时候出现Syntax error: "(" unexpected错误解决
- ubuntu下执行shell脚本报错 Syntax error: "(" unexpected
- Ubuntu 16.04下Shell脚本中使用数组提示:Syntax error: "(" unexpected
- 执行脚本是报错./cross.install: 25: Syntax error: "do" unexpected (expecting "fi")
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- 在执行shell脚本时,出现“syntax error : unexpected end of file"错误的解决方法
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- 执行脚本是报错./cross.install: 25: Syntax error: "do" unexpected (expecting "fi")
- shell脚本报错:"[: =: unary operator expected"
- netca 配置 local naming method报错-TNS-04605: Invalid syntax error: Unexpected char or LITERAL "(" befor
- shell 脚本 正则表达式通配符之 "[]" [大三TJB_708]
- 编译android源码报错:build/envsetup.sh: 1: Syntax error: "(" unexpected
- 编译android源码报错:build/envsetup.sh: 1: Syntax error: "(" unexpected