简体   繁体   English

bash脚本中将多个文件作为参数

[英]multiple files as argument in bash script

I have a script that exports log files, usually I get between 1-3 separate files depending on the size of the log for a particular day. 我有一个导出日志文件的脚本,通常我会在一天中得到1-3个单独的文件,具体取决于日志的大小。 After the export I run a report generator to spit out an html document of those logs. 导出后,我运行报告生成器以吐出这些日志的html文档。 My question is how to call the command that generates the reports depending on the number of files, I know I can use if statements and do: 我的问题是如何调用根据文件数生成报告的命令,我知道我可以使用if语句并执行以下操作:

./generateReport -i file1 -o output.html

./generateReport -i file1 file2 -o output.html

./generateReport -i file1 file2 file3 -o output.html

is there a way to loop over the files and include them as an input?? 有没有一种方法可以遍历文件并将其包含为输入?

是否有必要使用标志?

./generateReport file1 file2 file3 > output.html

The following will collect an array of inputFiles, and a single variable with the output file name: 以下将收集一个inputFiles数组,以及一个带有输出文件名的变量:

inputFiles=( )
outputFile=
while (( $# )); do
  if [[ $1 = -o ]]; then
    outputFile=$2; shift
  elif [[ $1 = -i ]]; then
    inputFiles+=( "$2" ); shift
  else
    inputFiles+=( "$1" )
  fi
  shift
done

...then, you could do something like this: ...然后,您可以执行以下操作:

# redirect stdout to the output file, if one was given
[[ $outputFile ]] && exec >"$outputFile"

# loop over the input files and process each one
for inputFile in "${inputFiles[@]}"; do
  process "$inputFile"
done

Try filename expansion : 尝试扩展文件名:

./generateReport -i file? -o output.html

or using find for all log files created in the last 24 hours : 或对过去24小时内创建的所有日志文件使用find

./generateReport -i `find . -name "file*" -mtime -1` -o output.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM