Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
eoa-publication-model/scripts/utils/for_each_input.sh
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
192 lines (173 sloc)
3.53 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BASE_DIR="$(dirname $0)/../.." | |
SCRIPT_DIR="$(dirname $0)/.." | |
DEP_DIR="$BASE_DIR/dependencies" | |
####################################### | |
# variables | |
####################################### | |
INPUT_DIRS=() | |
NAME_PATTERN='*' | |
SUFFIX='.xml' | |
CACHE=1 | |
OUTPUT_DIR= | |
INPUT_DIR_DEFAULT="." | |
OUTPUT_DIR_DEFAULT="." | |
DONT_RUN= | |
VERBOSE= | |
LOG_FILE= | |
PRINT_CMD= | |
CMD=() | |
####################################### | |
# functions | |
####################################### | |
function print_help { | |
echo "USAGE:" | |
echo | |
echo " $ $0 [OPTIONS] [--] CMD [OPT1 ...] [ARG1 ...]" | |
echo | |
echo "DESCRIPTION:" | |
echo " for every file in INPUT_DIR matching PATTERN" | |
echo " try to generate a corresponding file" | |
echo " in OUTPUT_DIR." | |
echo " This is achieved as follows:" | |
echo " For every input file CMD is run," | |
echo " where '\$INPUT' and '\$OUTPUT' expand to" | |
echo " the current input/output filename." | |
echo | |
echo "CACHING:" | |
echo " with caching enabled, CMD is only run" | |
echo " if the output file not yet exists" | |
echo | |
echo "OPTIONS:" | |
echo " -h | --help: print this help" | |
echo " -i|--input-dir INPUT_DIR (multiple options add multiple directories). default: '$INPUT_DIR_DEFAULT'" | |
echo " -o|--output-dir OUTPUT_DIR. default: '$OUTPUT_DIR_DEFAULT'" | |
echo " -f|--filter PATTERN: only consider files matching PATTERN. default: '$NAME_PATTERN'" | |
echo " -x|--suffix SUFFIX: suffix to append to output files. default: '$SUFFIX'" | |
echo " -c|--cache CACHE: if set, only write output if not yet existing" | |
echo " -l|--log-file LOG_FILE: write output to LOG_FILE" | |
echo " -p|--print-cmd: print the COMMAND being executed" | |
echo " -n|--dont-run: don't execute command" | |
echo " -v|--verbose: print additional information" | |
echo | |
echo "ARGUMENTS:" | |
} | |
####################################### | |
# parse command line arguments | |
####################################### | |
while [[ $# > 0 ]]; do | |
key="$1" | |
case $key in | |
-h | --help) | |
print_help | |
exit 0 | |
;; | |
-i|--input-dir) | |
shift | |
INPUT_DIRS+=( "$1" ) | |
shift | |
;; | |
-o|--output-dir) | |
shift | |
OUTPUT_DIR="$1" | |
shift | |
;; | |
-f|--filter) | |
shift | |
NAME_PATTERN="$1" | |
shift | |
;; | |
-x | --suffix) | |
shift | |
SUFFIX="$1" | |
shift | |
;; | |
-c | --cache) | |
shift | |
CACHE="$1" | |
shift | |
;; | |
-l | --log-file) | |
shift | |
LOG_FILE="$1" | |
shift | |
;; | |
-p | --print-cmd) | |
shift | |
PRINT_CMD=1 | |
;; | |
-n | --dont-run) | |
shift | |
DONT_RUN=1 | |
;; | |
-v | --verbose) | |
shift | |
VERBOSE=1 | |
;; | |
--) | |
shift | |
break | |
;; | |
-* ) | |
echo "wrong syntax!" | |
print_help | |
exit 1 | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
if [[ $# = 0 ]]; then | |
print_help | |
exit 1 | |
fi | |
if [[ "$1" != "" ]]; then | |
CMD=( "$@" ) | |
shift | |
fi | |
if [[ "$OUTPUT_DIR" = "" ]]; then | |
OUTPUT_DIR="$OUTPUT_DIR_DEFAULT" | |
fi | |
if [[ ${#INPUT_DIRS[@]} = 0 ]]; then | |
INPUT_DIRS=( "$INPUT_DIR_DEFAULT" ) | |
fi | |
if [[ "$VERBOSE" != "" ]]; then | |
echo "creating '$OUTPUT_DIR'" | |
fi | |
mkdir -p "$OUTPUT_DIR" | |
if [[ "$VERBOSE" != "" ]]; then | |
echo "pattern: '$NAME_PATTERN'" | |
fi | |
while read file | |
do | |
opts=() | |
if [[ "$DONT_RUN" != "" ]]; then | |
opts+=( "--dont-run" ) | |
fi | |
if [[ "$LOG_FILE" != "" ]]; then | |
opts+=( "--log-file" "$LOG_FILE" ) | |
fi | |
if [[ "$PRINT_CMD" != "" ]]; then | |
opts+=( "--print-cmd" ) | |
fi | |
if [[ "$VERBOSE" != "" ]]; then | |
opts+=( "--verbose" ) | |
fi | |
INPUT="$file" | |
OUTPUT=${file##*/} | |
OUTPUT=${OUTPUT%.*}$SUFFIX | |
OUTPUT="$OUTPUT_DIR/$OUTPUT" | |
$SCRIPT_DIR/utils/run_with_cache.sh \ | |
--cache "$CACHE" \ | |
"${opts[@]}" \ | |
--input-file "$INPUT" \ | |
--output-file "$OUTPUT" \ | |
-- \ | |
"${CMD[@]}" \ | |
|| exit 1 | |
done < <( | |
find "${INPUT_DIRS[@]}" \ | |
\( -type f -o -type l \) \ | |
-name "$NAME_PATTERN" | |
) |