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/run_with_cache.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
155 lines (141 sloc)
2.75 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 | |
####################################### | |
OUTPUT_FILE= | |
INPUT_FILE= | |
CACHE=1 | |
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 " Run CMD optionally with '\$INPUT' and '\$OUTPUT' expanded as given by OPTIONS" | |
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-file INPUT: if specified, expand '\$INPUT' when executing CMD" | |
echo " -o|--output-file OUTPUT: if specified, expand '\$OUTPUT' when executing CMD" | |
echo " -c|--cache CACHE: if true, only execute if OUTPUT doesn't exist" | |
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" | |
} | |
####################################### | |
# parse command line arguments | |
####################################### | |
while [[ $# > 0 ]]; do | |
key="$1" | |
case $key in | |
-h | --help) | |
print_help | |
exit 0 | |
;; | |
-i|--input-file) | |
shift | |
INPUT_FILE="$1" | |
shift | |
;; | |
-o|--output-file) | |
shift | |
OUTPUT_FILE="$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=( "$@" ) | |
fi | |
if [[ "$OUTPUT_FILE" = "" ]]; then | |
print_help | |
exit 1 | |
fi | |
if [[ "$VERBOSE" != "" ]]; then | |
echo "creating '"$(dirname "$LOG_FILE")"'" | |
fi | |
mkdir -p $(dirname "$LOG_FILE") | |
if [[ "$INPUT_FILE" != "" ]]; then | |
INPUT="$INPUT_FILE" | |
fi | |
OUTPUT="$OUTPUT_FILE" | |
if [[ $CACHE ]] && [[ -e $OUTPUT ]]; then | |
echo -n "(skipping) '" | |
eval echo -n ${CMD[@]} | |
if [[ "$LOG_FILE" != "" ]]; then | |
echo -n " >'$LOG_FILE'" | |
echo -n " 2>&1" | |
fi | |
echo "'" | |
else | |
if [[ "$DONT_RUN" = "" ]]; then | |
if [[ $PRINT_CMD ]]; then | |
echo -n "executing: '" | |
eval echo -n ${CMD[@]} | |
if [[ "$LOG_FILE" != "" ]]; then | |
echo -n " 2>&1" | |
echo -n " >'$LOG_FILE'" | |
fi | |
echo "'" | |
fi | |
if [[ "$LOG_FILE" != "" ]]; then | |
CMD+=( ">" "'$LOG_FILE'" ) | |
CMD+=( "2>&1" ) | |
fi | |
eval "${CMD[@]}" || exit 1 | |
fi | |
fi |