2012-05-11 23 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)