首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用给定目录中的输入来运行特定程序的脚本

使用给定目录中的输入来运行特定程序的脚本
EN

Stack Overflow用户
提问于 2012-05-12 03:43:23
回答 2查看 72关注 0票数 0

因此,我需要运行一系列( maven )测试,并将testfiles作为参数提供给maven任务。

如下所示:

mvn clean test -Dtest=<filename>

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

所以我从一个名为‘run_test’的So脚本开始:

代码语言:javascript
运行
复制
#!/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 $?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-12 03:52:39

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

代码语言:javascript
运行
复制
for file in $1/*
do
    [[ -f $file ]] && mvn clean test -Dtest=$file
done

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

代码语言:javascript
运行
复制
for file in $(find $1 -type f)
do
    etc...
done
票数 1
EN

Stack Overflow用户

发布于 2012-05-12 03:50:44

代码语言:javascript
运行
复制
#! /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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10557615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档