From f3b2b40b91ae97a6ad166382c593497efbb458ea Mon Sep 17 00:00:00 2001 From: Tom Scavo Date: Sun, 28 May 2017 17:28:12 -0400 Subject: [PATCH] Accept input from stdin --- bin/run_xslt_scripts.sh | 85 +++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/bin/run_xslt_scripts.sh b/bin/run_xslt_scripts.sh index bc6b39f..707db6b 100755 --- a/bin/run_xslt_scripts.sh +++ b/bin/run_xslt_scripts.sh @@ -30,13 +30,14 @@ display_help () { The script depends on cached metadata. It will not fetch a metadata file from the server. - Usage: ${0##*/} [-hv] -u MD_LOCATION -d OUT_DIR "XSL_FILENAME [OUTPUT_FILENAME]" ... + Usage: ${0##*/} [-hv] -u MD_LOCATION -d OUT_DIR ["XSL_FILENAME [OUTPUT_FILENAME]" ...] The script takes an arbitrary sequence of compound command-line arguments. The second sub-argument of each compound argument is OPTIONAL. If no OUTPUT_FILENAME is given for a particular XSL_FILENAME, the script computes an output filename as best - it can. + it can. If there are no command-line arguments, the script takes + its input from stdin. Options: -h Display this help message @@ -214,21 +215,26 @@ if [ ! -d "$out_dir" ]; then exit 2 fi -# check command-line arguments -shift $(( OPTIND - 1 )) -if [ $# -eq 0 ]; then - echo "ERROR: $script_name: wrong number of arguments: $# (at least one required)" >&2 - exit 2 -fi - ####################################################################### # Functions ####################################################################### # process a compound command-line argument -# present that argument to this function as a pair of arguments: +# +# present a pair of arguments to this function: # XSL_FILENAME [OUTPUT_FILENAME] -# if the latter is omitted, the function computes a suitable output filename +# if the latter is omitted, the function computes an output +# filename according to the following algorithm: +# +# if the XSL filename is of the form +# base_filename_csv.xsl +# or +# base_filename_json.xsl +# then construct the output filename with the indicated extension: +# base_filename.csv +# or +# base_filename.json +# otherwise the output filename is the XSL filename sans the .xsl extension process_arg () { local xsl_file @@ -254,7 +260,7 @@ process_arg () { xsl_file="$LIB_DIR/$1" if [ ! -f "$xsl_file" ]; then echo "ERROR: $FUNCNAME: lib file does not exist: $xsl_file" >&2 - exit 4 + return 4 fi # capture array elements @@ -266,15 +272,6 @@ process_arg () { if [ -z "$output_filename" ]; then # compute the output filename - - # if the XSL filename is of the form - # base_filename_csv.xsl - # or - # base_filename_json.xsl - # then construct the output filename with the corresponding extension: - # base_filename.csv - # base_filename_.son - # otherwise the output filename is the XSL filename sans the .xsl extension base_filename="${1%%.*}" if [[ $base_filename =~ _(csv|json)$ ]]; then output_filename=$( echo "$base_filename" | $_SED -e 's/^\(.*\)_\([^_]*\)$/\1.\2/' ) @@ -312,30 +309,42 @@ if [ $status_code -ne 0 ]; then exit 2 fi -# specify input file +# specify output file for SAML metadata xml_file="${tmp_dir}/saml-metadata.xml" +# read the input arguments into a temporary file +in_file="${tmp_dir}/tmp_file_in.txt" +shift $(( OPTIND - 1 )) +if [ "$#" -gt 0 ]; then + # read input from the command line + while (( "$#" )); do + # copy command-line arg into the temp file + echo "$1" >> "$in_file" + shift + done +else + # read input from stdin + /bin/cat - > "$in_file" +fi + # declare arrays -xsl_filenames=() -xsl_files=() -out_filenames=() -out_files=() - -# iterate over the command-line arguments -num_args=$# # TODO: simplify this loop -for (( i = 0; i < num_args; i++ )); do - - # process the next command-line arg - # (do not quote the arg) - process_arg $1 +declare -a xsl_filenames +declare -a xsl_files +declare -a out_filenames +declare -a out_files + +# iterate over the input arguments +while read line; do + + # process the next line + # (do not quote the line) + process_arg $line status_code=$? if [ $status_code -ne 0 ]; then - echo "ERROR: $script_name failed ($status_code) to process arg $1" >&2 + echo "ERROR: $script_name failed ($status_code) to process data: $line" >&2 exit 2 fi - - shift -done +done < "$in_file" ####################################################################### # Main processing