2012-05-11 53 views
0

所以我需要運行一堆(maven)測試,測試文件作爲參數提供給maven任務。腳本從給定目錄輸入運行某個程序

事情是這樣的:

mvn clean test -Dtest=<filename>

而且測試文件通常被組織成不同的目錄。所以我試圖編寫一個腳本來執行上述'命令',並自動將給定目錄中的所有文件的名稱提供給-Dtest

於是我開始了一個名爲「RUN_TEST」的shell:

#!/bin/sh 
if test $# -lt 2; then 
    echo "$0: insufficient arguments on the command line." >&1 
    echo "usage: $0 run_test dirctory" >&1 
    exit 1 
fi 
for file in allFiles <<<<<<< what should I put here? Can I somehow iterate thru the list of all files' name in the given directory put the file name here? 
    do mvn clean test -Dtest= $file 

exit $? 

的部分在哪裏卡住了是如何得到的文件名列表。 謝謝,

回答

1

假設$1包含目錄名(用戶輸入驗證是一個單獨的問題),然後

for file in $1/* 
do 
    [[ -f $file ]] && mvn clean test -Dtest=$file 
done 

將運行上的所有文件COMAND。如果你想遞歸到子目錄,那麼你需要使用find命令

for file in $(find $1 -type f) 
do 
    etc... 
done 
+0

如果什麼<目錄名>參數只給我的目錄的名稱,而不是位置。換句話說,我肯定知道給定的目錄在'/'中。但它可能在任何地方。那麼我應該使用$ for $(find $ 1 -type d)'(d for directory?)? –

+0

'[[-f $ file]]'是什麼意思? –

+0

將上面的代碼換成'for $(find。-type d -name $ 1)中的for dir。做...內部循環...完成' –

1
#! /bin/sh 
# Set IFS to newline to minimise problems with whitespace in file/directory 
# names. If we also need to deal with newlines, we will need to use 
# find -print0 | xargs -0 instead of a for loop. 
IFS=" 
" 
if ! [[ -d "${1}" ]]; then 
    echo "Please supply a directory name" > &2 
    exit 1 
else 
    # We use find rather than glob expansion in case there are nested directories. 
    # We sort the filenames so that we execute the tests in a predictable order. 
    for pathname in $(find "${1}" -type f | LC_ALL=C sort) do 
    mvn clean test -Dtest="${pathname}" || break 
    done 
fi 
# exit $? would be superfluous (it is the default)