今天在Ubuntu下调试代码,明明是正确的,却仍然报错,查了错误信息才知道:Ubuntu中默认不是bash,而是为了加快开机速度,使用了dash。

dash中需要严格的语法,而且与bash语法不同。例如,bash中定义函数是使用function关键字的(function foo() {}),但在dash中没有该关键字,直接使用foo(){}定义。我的错误就是这样因为有了function而没有通过。

注意:在sh文件首行定义的“#!/bin/bash”,指的是在(文件具有x权限的前提下),使用“./filename.sh”方法进行执行的时候使用的bash。不过,我在把这行删掉之后,使用"./filename.sh"方法还是可以执行不出错的。只有在使用“sh filename.sh”下才会出错。

网上资料如下:

------:http://blog.sina.com.cn/s/blog_49c2012f0100z3bb.html

今天跟着练习一个shell scripts,内容如下:

=======================================
#!/bin/bash
# Using for and loop
# allen 2010/04/13
declare -i s # <==变量宣告
for (( i=1; i<=100; i=i+1 ))
do
        s=s+i
done
echo "The count is =l
=======================================

但是运行时总是报下面这个错,如下:

test11-loop.sh: 5: Syntax error: Bad for loop variable

几经查找语法,没有问题,后来在网上找到问题原因:

代码对于标准bash而言没有错,因为Ubuntu为了加快开机速度,用dash代替了传统的bash,是dash在捣鬼。
解决方法是 取消dash
sudo dpkg-reconfigure dash
在选择项中选No,即可。

-----:http://www.2cto.com/os/201305/210033.html

Dash与Bash的语法区别
2013-05-11 14:08:06     我来说两句    来源:www.igigo.net  
收藏    我要投稿
Dash与Bash的语法区别
如今Debian和Ubuntu中,/bin/sh默认已经指向dash,这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,它速度更快,但功能相比bash要少很多,语法严格遵守POSIX标准,下面简要列举下从bash迁移到dash一般需要注意的问题
1.定义函数
bash: function在bash中为关键字
igi@gentoo ~ $ foo(){ echo $0;}
igi@gentoo ~ $ foo
/bin/bash
igi@gentoo ~ $ function foo2(){ echo $0;}
igi@gentoo ~ $ foo2
/bin/bash
dash: dash中没有function这个关键字
$ foo(){ echo $0;}
$ foo
dash
$ function foo2(){ echo $0;}
dash: Syntax error: "(" unexpected
2.select var in list; do command; done
bash:支持
igi@gentoo ~ $ select input in A B
> do
>   case $input in
>     A)
>        echo 'Input:A'
>        break
>        ;;
>     B)
>        echo 'Input:B'
>        break
>        ;;
>   esac
> done
1) A
2) B
#? 1
Input:A
igi@gentoo ~ $ echo $0
/bin/bash
dash:不支持, 替代方法:采用while+read+case来实现
menu(){ echo -n "1)A;\n2)B\n>";}
menu
while read input
do
case $input in
1)
echo 'A'
break
;;
2)
echo 'B'
break
;;
*)
menu
continue
;;
esac
done
3. echo {0..10}
bash:支持{n..m}展开
igi@gentoo ~ $ echo $0
/bin/bash
igi@gentoo ~ $ echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
dash:不支持,替代方法, 采用seq外部命令
$ echo $0
dash
$ echo {0..10}
{0..10}
$ echo `seq 0 10`
0 1 2 3 4 5 6 7 8 9 10
4. here string
bash:支持here string
igi@gentoo ~ $ cat <<<"string"
string
igi@gentoo ~ $ echo $0
/bin/bash
dash:不支持, 替代方法:可采用here documents
$ echo $0
dash
$ cat <<<"string"
dash: Syntax error: redirection unexpected
$ cat <<EOF
> string
> EOF
string
5. >&word重定向标准输出和标准错误
bash: 当word为非数字时,>&word变成重定向标准错误和标准输出到文件word, 常见用法>&/dev/null
igi@gentoo ~/test $ ls
a
igi@gentoo ~/test $ ls a b
ls: cannot access b: No such file or directory
a
igi@gentoo ~/test $ ls a b >&/dev/null
igi@gentoo ~/test $ ls a b >/dev/null 2>&1
igi@gentoo ~/test $ echo $0
/bin/bash
dash: >&word, word不支持非数字, 替代方法: >word 2>&1; 常见用法 >/dev/null 2>&1
$ echo $0
dash
$ ls a
a
$ ls a b
ls: cannot access b: No such file or directory
a
$ ls a b >&/dev/null
dash: Syntax error: Bad fd number
$ ls a b >/dev/null 2>&1
$
6. 数组
bash: 支持数组, bash4支持关联数组
igi@gentoo ~/test $ echo $0
/bin/bash
igi@gentoo ~/test $ array=( a b c )
igi@gentoo ~/test $ echo ${array[2]}
c
dash: 不支持数组,替代方法, 采用变量名+序号来实现类似的效果
$ for i in a b c
> do
> id=$((${id:=-1}+1))
> eval array_$id=$i
> done
$ echo ${array_1}
b
$ echo $0
dash
很蛋疼的方法,非不得以不建议这么用
7. 子字符串扩展
bash: 支持${parameter:offset:length},${parameter:offset}
igi@gentoo ~/test $ string='hello'
igi@gentoo ~/test $ echo ${string:1:3}
ell
igi@gentoo ~/test $ echo ${string:1}
ello
igi@gentoo ~/test $ echo $0
/bin/bash
dash: 不支持, 替代方法:采用expr或cut外部命令代替
$ string='hello'
$ expr substr "$string" 2 3
ell
$ echo "$string" | cut -c2-4
ell
$ expr substr "$string" 2 "${#string}"
ello
$ echo "$string" | cut -c2-
ello
$ echo $0
dash
$
8. 大小写转换
bash: 支持${parameter^pattern},${parameter^^pattern},${parameter,pattern},${parameter,,pattern}
igi@gentoo ~/test $ string="abcABC"
igi@gentoo ~/test $ echo ${string^^}
ABCABC
igi@gentoo ~/test $ echo ${string,,}
abcabc
igi@gentoo ~/test $ echo ${string^^b}
aBcABC
igi@gentoo ~/test $ echo $0
/bin/bash
dash: 不支持,替代方法:采用tr/sed/awk等外部命令转换
$ string='abcABC'
$ echo "$string" | tr '[a-z]' '[A-Z]'
ABCABC
$ echo "$string" | tr '[A-Z]' '[a-z]'
abcabc
$ echo "$string" | sed 's/b/\U&/g'
aBcABC
$
9. 进程替换<(command), >(command)
bash: 支持进程替换
igi@gentoo ~ $ diff <(seq 3) <(seq 4)
3a4
> 4
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持, 替代方法, 通过临时文件中转
$ diff <(seq 3) <(seq 4)
dash: Syntax error: "(" unexpected
$ seq 3 >tmp1
$ seq 4 >tmp2
$ diff tmp1 tmp2
3a4
> 4
$ echo $0
dash
$
10. [ string1 = string2 ] 和 [ string1 == string2 ]
bash: 支持两者
igi@gentoo ~ $ [ 'a' = 'a' ] && echo 'equal'
equal
igi@gentoo ~ $ [ 'a' == 'a' ] && echo 'equal'
equal
igi@gentoo ~ $ echo $0
/bin/bash
dash: 只支持=
$ [ 'a' = 'a' ] && echo 'equal'
equal
$ [ 'a' == 'a' ] && echo 'equal'
[: 2: a: unexpected operator
$ echo $0
dash
$
11. [[ 加强版test
bash: 支持[[ ]], 可实现正则匹配等强大功能
igi@gentoo ~ $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'
equal
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持[[ ]], 替代方法,采用外部命令
$ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'
dash: [[: not found
$ echo 'xyz123' | grep -q 'xyz[0-9]\+' && echo 'equal'
equal
$ echo $0
dash
$
12. for (( expr1 ; expr2 ; expr3 )) ; do list ; done
bash: 支持C语言格式的for循环
igi@gentoo ~ $ for((i=0;i<=3;i++));do echo "$i";done
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持该格式的for, 替代方法,用while+$((expression))实现
$ i=0
$ while [ "$i" -le 3 ]
> do
> echo "$i"
> i=$((i+1))
> done
0
1
2
3
$ echo $0
dash
$
13. let命令和((expression))
bash: 有内置命令let, 也支持((expression))方式
igi@gentoo ~ $ i=0
igi@gentoo ~ $ let i++
igi@gentoo ~ $ echo $i
1
igi@gentoo ~ $ ((i++))
igi@gentoo ~ $ echo $i
2
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持,替代方法,采用$((expression))或者外部命令做计算
$ i=0
$ i=$((i+1))
$ echo $i
1
$ echo $0
dash
$
14. $((expression))
bash: 支持id++,id--,++id,--id这样到表达式
igi@gentoo ~ $ i=0
igi@gentoo ~ $ echo $((i++))
0
igi@gentoo ~ $ echo $i
1
igi@gentoo ~ $ echo $((++i))
2
igi@gentoo ~ $ echo $i
2
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持++,--, 替代方法:id+=1,id-=1, id=id+1,id=id-1
$ i=0
$ echo $((i++))
dash: arithmetic expression: expecting primary: "i++"
$ echo $i;i=$((i+1))
0
$ echo $i
1
$ echo $((i+=1))
2
$ echo $i
2
$ echo $0
dash
$

Ubuntu下默认使用dash而非bash相关推荐

  1. wireshark使用教程 linux,Linux入门教程:ubuntu下安装wireshark(以及配置非root),这个强大的工具可以捕...

    Linux入门教程:ubuntu下安装wireshark(以及配置非root),这个强大的工具可以捕 Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据,并为用户提供关 ...

  2. Ubuntu下Linux系统部署fisco时bash操作报错权限不足(permission denied) failed to run command ‘........‘

    Ubuntu下Linux系统部署fisco时bash操作报错权限不足(permission denied) failed to run command '-' 例如这里就指的是fisco-bcos这个 ...

  3. Ubuntu下使用IntelliJ IDEA的正确姿势

    Preface 原文:http://yangbingdong.com/2017/note-of-learning-idea-under-ubuntu/ 公司里的大牛们用的IDE基本都是IDEA~~近墨 ...

  4. 搭建Ubuntu下c/c++编译环境【转】

    1.       安装Ubuntu. 2.       安装gcc 方法一: sudo apt-get  install  build-essential 安装完了可以执行 gcc--version的 ...

  5. 配置linux终端主题需要密码,Mac/Ubuntu下终端色彩主题设置

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 审美是主观的, 但是总有一些东西是大家普遍觉得更"美"的. 我自己由于工作性质和个人爱好两方面的原因 ...

  6. Ubuntu 下安装zsh和oh-my-zsh

    注意:安装前先备份/etc/passwd 一开始装oh-my-zsh我是拒绝的,因为这东西安装容易,卸载难,真的很难. Mac安装参考:http://www.cnblogs.com/EasonJim/ ...

  7. ubuntu下文件名乱码的解决办法

    ubuntu下文件名乱码的解决办法 (2012-11-30 11:38:49) 转载▼ 标签: 终端 ubuntu it 分类:系统问题 ubuntu下文件名乱码的解决办法       最近一直在用u ...

  8. ubuntu下配置jdk(离线压缩包方式)

    2019独角兽企业重金招聘Python工程师标准>>> 1,下载jdk压缩包 2.解压后得到名字为jdk1.7.0_21的文件夹,将其复制到 /usr/lib/jvm下(需要新建jv ...

  9. ubuntu php 错误,Ubuntu下如何开启PHP错误提示教程

    ubuntu下默认是没有php语法错误提示的,如果要开启,需要修改几个地方: 1. 打开php.ini文件. 这个文件在: /etc/php5/apache2 目录下,需要修改这个文件的权限才能写入. ...

最新文章

  1. form表单提交,Servlet接收并读取Excel文件
  2. 第一个OC类、解析第一个OC程序
  3. js uri解码_js进行URL编码(escape,encodeURI,encodeURIComponent)
  4. 单页面 Web 应用(Single Page Application,SPA)的工作原理介绍
  5. HTML与CSS基础之常用选择器(一)
  6. 是否可以改变 宏的值_给女人的建议:当父母不同意你的男朋友,可以尝试六个方法...
  7. 【AI面试题】为什么必须在神经网络中引入非线性
  8. 杭电2066一个人的旅行(spfa)
  9. ExcelToDataTable
  10. 在linux下挂载ios镜像文件,linux下挂载iso镜像文件
  11. 不同数据库之间的独立性
  12. VC++6.0下编译xvidcore1.1.0
  13. linux 使用 雅黑字体,linux 使用微软雅黑字体
  14. 计算机上求平均分的公式,求平均值的公式
  15. 面试经常考的五个Sql查询
  16. 《如何阅读一本书》笔记
  17. [PR] 关于Adobe Premiere Pro CS6 中文乱码的问题(怎么选中文字体?)
  18. 他向导师下跪,仍被强制退学!5年博士白读,双方各执一词,同门师兄也有回应……...
  19. JAVA制作网页的软件有哪些,html5开发工具(开发html5网页的软件有哪些)
  20. [渝粤教育] 兰州文理学院 信息技术基础 参考 资料

热门文章

  1. java 进程100_原创:如何排查java进程cpu100%的问题
  2. C++中的Lambda表达式详解
  3. idea 升级到2020后 无法启动_i.MXRT软复位后无法从32MB Flash启动?
  4. 一个页面区分管理者和普通用户如何设计_电商系统:优惠券原型设计说明(二)...
  5. java 智能家居管理系统_智能家居系统手机客户端应用源码
  6. php mac 常用代码,在Mac上使用PHP exec shell(代码签名)重新分配IPA
  7. python监听udp端口_python检测远程udp端口是否打开
  8. 请求时的编码问题 Use body.encode(‘utf-8‘) if you want to send it encoded in UTF-8
  9. html连接服务器文件夹,云服务器html链接到文件夹
  10. 文本编辑器中实现自动换行的功能