zoukankan      html  css  js  c++  java
  • bash 脚本中分号的作用

    在Linux bash shell中,语句中的分号一般用作代码块标识

    1、单行语句一般要用到分号来区分代码块。比如:

    weblogic@pmtest:/$if [ "$PS1" ]; then echo test is ok; fi
    test is ok

    该脚本或命令行中,须要两个分号才为正确的语句,第一个分号是then 前的分号,用于标识条件块结束。第二个分号在fi前,用于标识then块结束,假设缺少这两个分号,则程序运行错误。

    这里有趣的是echo后的字符串不须要使用引號也能正确地被识别。


    注意:语句结尾不要分号。


    2、该代码若写作多行。用换行符来区分代码块,则无需用到分号,比如:

    weblogic@pmtest:/$if [ "PS1" ]
    > then echo "test is ok"
    > fi
    test is ok

    从这个样例可看出 if推断语句分if块,then块,fi结束标识。

    当然还有可能有elesif块,比如:

    weblogic@pmtest:/$if [ "$PS1" ]
    > then echo test is ok
    > elif [ "$PS2" ]
    > then echo here
    > fi
    test is ok


    注意:这里要使用elif。而不使用esle if,若使用else if,则为不完整语句,如以下的样例不能被正确运行:

    weblogic@pmtest:/$if [ "$PS1" ]
    > then echo test is ok
    > else if [ "$PS2" ]
    > then echo here
    > else
    > echo ""
    > fi
    >

    敲回车后。shell程序觉得句子没有完毕。继续等待输入。




  • 相关阅读:
    在source insight中阅读Python代码
    修改linux命令行提示符
    gcc链接程序时出现undefined reference to""错误
    [转]init.d解析
    [转]Linux中find常见用法示例
    Linux的网卡由eth0变成了eth1,如何修复?
    测试使用Windows Live Writer
    黑盒测试
    白盒测试技术(二)
    白盒测试技术(一)
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5415615.html
Copyright © 2011-2022 走看看