Ubuntu 下運行 Shell 指令碼時顯示 “declare not found” 的問題

來源:互聯網
上載者:User
    -------------------------------------------------------------------------------
    declare not found 錯誤(ubuntu裡的dash):    #why,#dash:     @2010-Mar-25
    -------------------------------------------------------------------------------

    此種問題只在 Ubuntu 下才會出現!安裝 debian 系統根本不存在此類問題,因為只有
    Ubuntu 把預設的 bash 替換成了 dash. 在使用 debian 一天后感覺:Ubuntu 好像要
    弄出來一個 Linux 世界裡的"Windows 7". 自己 Linux 剛入門,不知見解是否有問題,歡
    迎提出不同意見。謝謝!
    - - - - - - - - - - - - - - - - -

    在學習 Shell 指令碼的時候,有兩個例子在運行時出現異常。比如,我在 test-dash.sh 腳
    本輸入如下內容:
        echo -e "Trekshot. \a \n"
        declare -i number=7
        echo $number
    在 Ubuntu 系統下使用 sh test-bash.sh 運行將出現如下提示資訊:
        -e Trekshot.

        test-dash.sh: 2: declare: not found:

    有兩個問題:
      a. echo 的參數 '-e' 怎麼也列印出來了?
      b. 一個簡單的整數聲明語句為何提示 "declare: not found"?
    
    1.
    出現此問題的原因並非代碼本身有誤,而是 Ubuntu 系統的問題。我們在編寫指令碼時,第一
    行經常要寫這麼一行內容:#!/bin/bash. 這是對該指令碼所使用的 shell 進行聲明,因為
    Linux 上的 shell 並非只有一種,而各個 shell 的文法是由差別的。之所以把 echo 命令
    的參數誤打出來,就是因為 sh 命令沒有理解該文法,即當前使用的並不是 bash.下面來證
    明:

    擷取 /bin 中相關檔案的屬性:
        -rwxr-xr-x 1 root root 725136 2008-05-13 02:48 bash
        -rwxr-xr-x 1 root root  87924 2008-06-21 00:07 dash
        lrwxrwxrwx 1 root root      4 2010-03-25 14:29 sh -> dash

    這表明在執行 sh test-dash.sh 的時候,我們使用的是一個叫 "dash" 的命令(注意 sh
    實為連結檔案),而並不是指令碼第一行中聲明的 bash. 這就是上面現象的原因。 其實,如
    果使用 bash test-dash.sh 命令執行的話,上面兩個錯誤根本不會出現。
    
    2.
    dash(Debian Almquist SHell) Ubuntu 自 6.10 後,將先前的 bashshell 更換成了
    dash (有待考證). 在設定 dash 的說明中有下面文字:

        The default /bin/sh shell on Debian and Debian-based systems is bash.

        However, since the default shell is required to be POSIX-compliant, any
        shell that conforms to POSIX, such as dash, can serve as /bin/sh. You may
        wish to do this because dash is faster and smaller than bash.
    
    大致意思是說預設的 shell 是 bash shell, 但只要是能相容 POSIX 的 shell 都可以,而
    dash 比 bash 速度更快、更小巧,因此 Ubuntu 安裝了 dash.
    
    3.
    怎麼能把 dash 去掉而使用預設的 bash?使用如下命令:
        sudo dpkg-reconfigure dash
    此命令是對已安裝的包進行重新設定,在菜單(dash-bash-sh.png)中選擇是否將 sh 連結

    到 dash (Install dash as /bin/sh?) 選擇否即可。

    附圖一:Ubuntu 下設定 dash 介面

  

From:http://www.cnblogs.com/Trekshot/archive/2010/03/29/ubuntu-shell-bash-dash.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.