0

所以我需要运行一堆(maven)测试,并将 testfiles 作为参数提供给 maven 任务。

像这样的东西:

mvn clean test -Dtest=<filename>

并且测试文件通常被组织到不同的目录中。因此,我正在尝试编写一个脚本,该脚本将执行上述“命令”并自动将给定目录中所有文件的名称提供给-Dtest.

所以我从一个名为'run_test'的shellscript开始:

#!/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 $?

我卡住的部分是如何获取文件名列表。谢谢,

4

2 回答 2

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)
于 2012-05-11T19:50:44.263 回答
1

假设$1包含目录名称(用户输入的验证是一个单独的问题),那么

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

将对所有文件运行命令。如果要递归到子目录,则需要使用find命令

for file in $(find $1 -type f)
do
    etc...
done
于 2012-05-11T19:52:38.723 回答