2012-11-17 17 views
1

我想建立一個調用其它子慶典主bash腳本scritps我怎樣才能structprogramme?

腳本應該運行如下: ./scriptname.sh 10001

10001:是定義數量的參數腳本運行(總共5個)。如果參數爲1,則啓動子腳本並且在0時不執行任何操作。

我創建了一個表子腳本,如:

subscript1=source "${DOCROOT}/script1.sh" subscript2=source "${DOCROOT}/script1.sh" ....
array=(${subscript1} ${subscript2} ${subscript3});

第一個問題是如何,我可以用正則表達式或其他每個位匹配作爲參數(參數之間刪除空間)。
第二個問題是我不知道如何將我的參數(10001)關聯到表下面的腳本。

a「切換案例」太長,無法在程序演進步驟中實施和維護。

能幫我構建主腳本?

+1

腳本採取所有參數使用$ *和和說法計數$#和每個參數$ 1 $ 2 $ 3 ...等等 –

+0

你需要包含bash嗎? '。 script.name' –

+0

請發佈您當前使用的代碼以實際調用子腳本;沒有它,我們只是在猜測什麼是錯的。目前尚不清楚你想通過什麼論點。 – je4d

回答

1

這裏有一個可能性:

#!/bin/bash 

die() { 
    echo >&2 "[email protected]" 
    exit 1 
} 

arg="$1" 

# Check that argument is valid (only 0's and 1's) 
[[ $arg =~ ^[01]+$ ]] || die "Wrong argument \`$1'" 

nargs=${#arg} 

for ((i=1;i<=nargs;++i)); do 
    valuei=${arg:i-1:1} 
    # If value is 0, continue loop 
    ((valuei)) || continue 
    # Execute script 
    echo "Executing subscript $i" 
done 

輸出示例:

$ ./scriptname 10001 
Executing script 1 
Executing script 5 

編輯。當你問如何執行標,這裏有一個(完全工作的)可能性:

#!/bin/bash 

# Maximum number of subscripts 
nsub=5 

die() { 
    echo >&2 "[email protected]" 
    exit 1 
} 

arg="$1" 

# Check that we have argument of correct length 
((${#arg}==nsub)) || die "Must give argument of length $nsub" 

# Check that argument is valid (only 0's and 1's) 
[[ $arg =~ ^[01]+$ ]] || die "Wrong argument \`$1'" 

for ((i=1;i<=nsub;++i)); do 
    # If value is 0, continue loop 
    ((${arg:i-1:1})) || continue 
    # Execute script 
    source "${DOCROOT}/script$i.sh" 
done 
+0

感謝您的回覆。我仍然沒有找到如何處理參數的解決方案。任何supositon? – ylas

+0

@ylas編輯給你一個可能的可能性'';-)這 –

+0

解決方案可以WORF只有當子腳本的名稱是:script1.sh,script2.sh,script3.sh ....如果子腳本名稱是:script_check.sh,script_tar.sh,script_crypt.sh,它不能工作? – ylas