2014-01-28 78 views
0

我寫了一個bash腳本,它接受一組參數,並根據那些我們做不同的事情。所以在這種情況下,我想說:不被識別爲命令 - bash

./acorn.sh aisiscore install 

但它關係到我的錯誤消息,指出第一個參數是不是reconized,我應該使用aisiscore或資產。我檢查了拼寫,我是正確的,我沒有正確檢查參數?

#!/bin/bash 

set -e 

function checkCoreArgument(){ 
    if [[ $1 == 'aisiscore' ]] 
    then 
    checkAisisArguments 
    elif [[ $1 == 'assets' || $1 == 'asset' ]] 
    then 
    checkAssetsArguments 
    else 
    echo "$1 is not recognized. Please try aisiscore or assets" 
    fi 
} 

function checkAisisArguments(){ 
    if [[ $2 == 'install' ]] 
    then 
    installAisisCore 
    elif [[ $2 == 'update' ]] 
    then 
    updateAisisCore 
    elif [[ $2 == 'components' ]] 
    then 
    callCompomnentsCheck 
    else 
    echo "$2 is not recognized. Please try install or update." 
    fi 
} 

function checkAssetsArguments(){ 
    if [[ $2 == 'install' ]] 
    then 
    installAssets 
    elif [[ $2 == 'update' ]] 
    then 
    updateAisisCore 
    else 
    echo "$2 is not recognized. Please use install or update." 
    fi 
} 

function installAisisCore(){ 
    cd scripts/install/ 
    chmod +x InstallAisisCore.sh 
    sudo InstallAisisCore.sh 
} 

function updateAisisCore(){ 
    cd scripts/update/ 
    chmod +x UpdateAisisCore.sh 
    sudo UpdateAisisCore.sh 
} 

function installAssets(){ 
    cd scripts/install/ 
    chmod +x InstallAssets.sh 
    sudo InstallAssets.sh 
} 

function updateAisisCore(){ 
    cd scripts/update/ 
    chmod +x UpdateAssets.sh 
    sudo UpdateAssets.sh 
} 

function callCompomnentsCheck(){ 
    cd scripts/install 
    chmod +x InstallComponents.sh 
    sudo ./InstallComponents.sh 
} 

###### =============================== [ Application run ] =============== ###### 

checkCoreArgument #run the app! 

這是自上而下,所以它更容易閱讀。

+0

蘇ggestion:對每個case使用'while [[$#-gt 0]]'循環與'case'語句和'shift'循環所有參數,隨時設置變量以跟蹤哪些任務應該被執行(以及你需要的任何其他數據)。然後將實際執行任務的代碼放在由變量控制的if塊中。 –

回答

2

這僅僅是約翰Kugelman的答案的擴展

  • 你不需要同時function關鍵字和()括號
  • 有很多的cut'n'paste代碼可以是 「參數」
#!/bin/bash 

set -e 

function checkArgs { 
    case $1 in 
    aisiscore) 
     case $2 in 
     install) install "AisisCore" ;; 
     update)  update "AisisCore" ;; 
     components) install "Components" ;; 
     *) echo "$2 is not recognized. Please try install or update." ;; 
     esac 
     ;; 
    assets | asset) 
     case $2 in 
     install) install "Assets" ;; 
     update) update "Assets" ;; 
     *) echo "$2 is not recognized. Please use install or update." ;; 
     esac 
     ;; 
    *) echo "$1 is not recognized. Please try aisiscore or assets" ;; 
    esac 
} 

function install { cd scripts/install/ && callScript "Install${1}.sh"; } 
function update  { cd scripts/update/ && callScript "Update${1}.sh"; } 
function callScript { chmod +x "$1" && sudo "$1"; } 

###### =============================== [ Application run ] =============== ###### 

checkArgs "[email protected]" #run the app! 
5

功能得到自己的論點$1$2等,如果你希望他們來解析你必須通過他們在腳本的參數。

checkCoreArgument "[email protected]" 

這同樣適用於相互調用的功能。這些函數調用需要顯式傳遞參數。

function checkCoreArgument(){ 
    if [[ $1 == 'aisiscore' ]] 
    then 
    checkAisisArguments "[email protected]" 
    elif [[ $1 == 'assets' || $1 == 'asset' ]] 
    then 
    checkAssetsArguments "[email protected]" 
    else 
    echo "$1 is not recognized. Please try aisiscore or assets" 
    fi 
} 

而且,一個小小的建議:你可以使用一個case聲明,使這些檢查更好一點。

case $1 in 
    aisiscore) checkAisisArguments "[email protected]";; 
    asset|assets) checkAssetsArguments "[email protected]";; 

    *) echo "$1 is not recognized. Please try aisiscore or assets" >&2;; 
esac