分享

关于shell语法

 Home of heart 2011-06-04

学了那么多语言,没见过这么垃圾的语法.
这就是linux下的脚本语言--bash.

一个脚本语言, 语法这么苛刻, 让人抓狂.
举例如下:
1:
定义变量时, =号的两边不可以留空格.
eg:
gender=femal------------right
gender =femal-----------wrong
gender= femal-----------wrong
2
条件测试语句 [ 符号的两边都要留空格.
eg:
if [ $gender = femal ]; then-------right.
     echo "you are femal";
fi

if[ $gender...-----------------------wrong
if [$gender...----------------------wrong.

3
条件测试的内容,如果是字符串比较的话, 比较符号两边要留空格!
eg:
if [ $gender = femal ]; then-------right.
if [ $gender= femal ]; then--------wrong.
if [ $gender=femal ]; then---------wrong.

4
如果if 和 then写在同一行, 那么,注意, then的前面要跟上 ; 号.
如果 then 换行写, 那么也没问题.
eg:
if [ $gender = femal ]; then-------right.
if [ $gender = femal ]
     then-------------------------------right.
if [ $gender = femal ] then-------wrong. then前面少了 ; 号.
提示出错信息:
syntax error near unexpected token then
同理,还有很多出错信息 比如
syntax error near unexpected token fi 等都是这样引起的.

5
if 后面一定要跟上 then. 同理
elif 后面一定要跟上 then.
不然提示出错信息:
syntax error near unexpected token else

 

明明从书上抄下来的程序,死活执行不过去, 气死人了. 更气人的时, 净没有一本书提示 [ 符号的两边要留空格. 呜呼哀哉.以上是我的总结,希望对bash入门的哥们有用.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多