• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Welcome to ErranLi's Blog!

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(12)

            搜索

            •  

            積分與排名

            • 積分 - 175914
            • 排名 - 151

            最新評(píng)論

            閱讀排行榜

            bashdash/bin/bash/bin/sh) 

              原文:http://www.cnblogs.com/dkblog/archive/2011/04/02/2003822.html

            Linux中的shell有多種類型,其中最常用的幾種是Bourne   shellsh)、C   shellcsh)和Korn   shellksh)。三種shell各有優(yōu)缺點(diǎn)。

            Bourne   shellUNIX最初使用的shell,并且在每種UNIX上都可以使用。Bourne   shellshell編程方面相當(dāng)優(yōu)秀,但在處理與用戶的交互方面做得不如其他幾種shell

            Linux操作系統(tǒng)缺省的shellBourne   Again   shell,它是Bourne   shell的擴(kuò)展,簡(jiǎn)稱Bash,與Bourne   shell完全向后兼容,并且在Bourne   shell的基礎(chǔ)上增加、增強(qiáng)了很多特性。Bash放在/bin/bash中,它有許多特色,可以提供如命令補(bǔ)全、命令編輯和命令歷史表等功能,它還包含了很多C   shellKorn   shell中的優(yōu)點(diǎn),有靈活和強(qiáng)大的編程接口,同時(shí)又有很友好的用戶界面。

            GNU/Linux 操作系統(tǒng)中的 /bin/sh 是 bashBourne-Again Shell)的符號(hào)鏈接,

                但鑒于 bash 過(guò)于復(fù)雜,有人把 ash 從 NetBSD 移植到 Linux 并更名為 dashDebian Almquist Shell),并建議將 /bin/sh 指向它,以獲得更快的腳本執(zhí)行速度。Ubuntu 號(hào)稱自從他們?cè)?nbsp;6.10 版里這樣做了以后,系統(tǒng)啟動(dòng)速度有了明顯的提升。Debian 計(jì)劃在下一個(gè)發(fā)行版(代號(hào) lenny)中也將 dash 作為默認(rèn)的 /bin/sh。


            /bin/sh與/bin/bash的細(xì)微區(qū)別

            原文:不詳

            在shell腳本的開(kāi)頭往往有一句話來(lái)定義使用哪種sh解釋器來(lái)解釋腳本。
            目前研發(fā)送測(cè)的shell腳本中主要有以下兩種方式:
            (1) #!/bin/sh
            (2) #!/bin/bash
            在這里求教同??蜅5母魑淮髠b們一個(gè)問(wèn)題:
            以上兩種方式有什么區(qū)別?對(duì)于腳本的實(shí)際運(yùn)行會(huì)產(chǎn)生什么不同的影響嗎?

            腳本test.sh內(nèi)容:
            #!/bin/sh
            source pcy.sh #pcy.sh并不存在
            echo hello
            執(zhí)行./test.sh,屏幕輸出為:
            ./test.sh: line 2: pcy.sh: No such file or directory
            由此可見(jiàn),在#!/bin/sh的情況下,source不成功,不會(huì)運(yùn)行source后面的代碼。
            修改test.sh腳本的第一行,變?yōu)?!/bin/bash,再次執(zhí)行./test.sh,屏幕輸出為:
            ./test.sh: line 2: pcy.sh: No such file or directory
            hello
            由此可見(jiàn),在#!/bin/bash的情況下,雖然source不成功,但是還是運(yùn)行了source后面的echo語(yǔ)句。
            但是緊接著我又試著運(yùn)行了一下sh ./test.sh,這次屏幕輸出為:
            ./test.sh: line 2: pcy.sh: No such file or directory
            表示雖然腳本中指定了#!/bin/bash,但是如果使用sh 方式運(yùn)行,如果source不成功,也不會(huì)運(yùn)行source后面的代碼。

            為什么會(huì)有這樣的區(qū)別呢?

            junru同學(xué)作了解釋

            1. sh一般設(shè)成bash的軟鏈
            [work@zjm-testing-app46 cy]$ ll /bin/sh
            lrwxrwxrwx 1 root root 4 Nov 13 2006 /bin/sh -> bash
            2. 在一般的linux系統(tǒng)當(dāng)中(如redhat),使用sh調(diào)用執(zhí)行腳本相當(dāng)于打開(kāi)了bash的POSIX標(biāo)準(zhǔn)模式
            3. 也就是說(shuō) /bin/sh 相當(dāng)于 /bin/bash --posix

            所以,sh跟bash的區(qū)別,實(shí)際上就是bash有沒(méi)有開(kāi)啟posix模式的區(qū)別

            so,可以預(yù)想的是,如果第一行寫成 #!/bin/bash --posix,那么腳本執(zhí)行效果跟#!/bin/sh是一樣的(遵循posix的特定規(guī)范,有可能就包括這樣的規(guī)范:“當(dāng)某行代碼出錯(cuò)時(shí),不繼續(xù)往下解釋”)


            例如:
            [root@localhost yuhj]# head -n1 x.sh
            #!/bin/sh
            [root@localhost yuhj]# ./x.sh

            ./x.sh: line 8: syntax error near unexpected token `<'
            ./x.sh: line 8: ` while read line; do { echo $line;((Lines++)); } ; done < <(route -n)'
            [root@localhost yuhj]#



            [root@localhost yuhj]# head -n1 x.sh
            #!/bin/bash
            [root@localhost yuhj]#./x.sh

            Kernel IP routing table
            Destination Gateway Genmask Flags Metric Ref Use Iface
            192.168.202.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
            0.0.0.0 192.168.202.2 0.0.0.0 UG 0 0 0 eth0
            Number of lines read = 4
            [root@localhost yuhj]#


            [root@localhost yuhj]# head -n1 x.sh
            #!/bin/bash --posix
            [root@localhost yuhj]#
            [root@localhost yuhj]# ./x.sh

            ./x.sh: line 8: syntax error near unexpected token `<'
            ./x.sh: line 8: ` while read line; do { echo $line;((Lines++)); } ; done < <(route -n)'



            [root@localhost yuhj]# whereis sh bash
            sh: /bin/sh /usr/share/man/man1/sh.1.gz /usr/share/man/man1p/sh.1p.gz
            bash: /bin/bash /usr/share/man/man1/bash.1.gz

            [root@localhost yuhj]# ll /bin/sh /bin/bash
            -rwxr-xr-x 1 root root 735004 May 25 2008 /bin/bash
            lrwxrwxrwx 1 root root 4 Jan 29 00:39 /bin/sh -> bash
            [root@localhost yuhj]#



             
            posted on 2012-05-24 16:45 erran 閱讀(3700) 評(píng)論(0)  編輯 收藏 引用
            久久中文字幕一区二区| 色悠久久久久久久综合网| 久久婷婷五月综合色奶水99啪| 久久久噜噜噜久久| 91麻精品国产91久久久久 | 久久青青草原精品国产不卡| 久久久久久精品成人免费图片| 久久99精品国产99久久| 热久久这里只有精品| 亚州日韩精品专区久久久| 欧美国产精品久久高清| 久久人人妻人人爽人人爽| 国产精品免费久久久久久久久| 久久久久久国产精品免费免费| 亚洲欧美成人久久综合中文网| 精品一二三区久久aaa片| 国产三级久久久精品麻豆三级| 国产2021久久精品| 久久精品国产亚洲AV忘忧草18| 欧美精品一本久久男人的天堂| 亚洲欧美一级久久精品| 久久精品国产亚洲精品2020| 麻豆久久| 日韩美女18网站久久精品| 四虎国产精品免费久久5151| 久久天天躁狠狠躁夜夜躁2O2O| 婷婷久久综合九色综合九七| 久久精品国产福利国产秒| 人妻无码精品久久亚瑟影视| 久久男人中文字幕资源站| 国产真实乱对白精彩久久| 久久狠狠高潮亚洲精品| 欧美一区二区三区久久综| 亚洲中文久久精品无码| 99久久无色码中文字幕人妻| 人妻无码精品久久亚瑟影视| 午夜视频久久久久一区 | 狠狠色丁香婷婷综合久久来| 午夜欧美精品久久久久久久| 中文字幕久久精品无码| 久久丫精品国产亚洲av|