From f63f9c7a1b1182e1125fd15e4ebec5b5f35c8293 Mon Sep 17 00:00:00 2001 From: Tom Scavo Date: Sat, 29 Oct 2016 15:03:53 -0400 Subject: [PATCH] Adjust exit codes --- bin/cget.sh | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/bin/cget.sh b/bin/cget.sh index 3ea5aac..fc8ad71 100755 --- a/bin/cget.sh +++ b/bin/cget.sh @@ -25,7 +25,7 @@ display_help () { This script retrieves and caches HTTP resources on disk. A previously cached resource is retrieved via HTTP Conditional GET [RFC 7232]. If the web server responds with HTTP 200 OK, - the resource is cached and written to stdout. If the web + the new resource is cached and written to stdout. If the web server responds with 304 Not Modified, the cached resource is output instead. @@ -49,15 +49,17 @@ display_help () { Option -h is mutually exclusive of all other options. - The default behavior of the script may be modified by using + The default behavior of the script is modified by using option -F or -C, which are mutually exclusive. Force Output Mode (option -F) forces the return of a fresh resource. The resource is output on stdout if and only if the server - responds with 200. If the response is 304, an error is thrown. + responds with 200. If the response is 304, the script silently + fails with exit code 1. Cache Only Mode (option -C) bypasses the GET request altogether and goes directly to cache. If the resource resides in cache, - it is output on stdout, otherwise an error is thrown. + it is output on stdout, otherwise the script silently fails + with exit code 1. LIBRARY @@ -152,8 +154,8 @@ if [ ! -d "$CACHE_DIR" ]; then /bin/mkdir "$CACHE_DIR" exit_code=$? if [ $exit_code -ne 0 ]; then - echo "ERROR: $script_name failed to create dir: $CACHE_DIR" >&2 - exit $exit_code + echo "ERROR: $script_name failed to create dir $CACHE_DIR (exit code: $exit_code)" >&2 + exit 2 fi fi $verbose_mode && printf "$script_name using cache directory: %s\n" "$CACHE_DIR" @@ -180,8 +182,8 @@ for lib_filename in $LIB_FILENAMES; do source "$lib_file" >&2 exit_code=$? if [ $exit_code -ne 0 ]; then - echo "ERROR: $script_name failed to source script $lib_file" >&2 - exit $exit_code + echo "ERROR: $script_name failed to source script $lib_file (exit code: $exit_code)" >&2 + exit 2 fi done @@ -203,10 +205,10 @@ else fi if [ ! -d "$TMP_DIR" ]; then /bin/mkdir "$TMP_DIR" - exit_status=$? - if [ $exit_status -ne 0 ]; then - echo "ERROR: $script_name failed to create dir: $TMP_DIR" >&2 - exit $exit_status + exit_code=$? + if [ $exit_code -ne 0 ]; then + echo "ERROR: $script_name failed to create dir $TMP_DIR (exit code: $exit_code)" >&2 + exit 2 fi fi @@ -217,14 +219,20 @@ tmp_file="${TMP_DIR}/http_resource_$$.xml" # Main processing ####################################################################### -# get the resource +# The conditional_get function is defined and documented +# in library file http_tools.sh. +# +$verbose_mode && printf "$script_name requesting resource: %s\n" "$location" conditional_get $local_opts -d "$CACHE_DIR" -t "$TMP_DIR" "$location" > "$tmp_file" exit_code=$? if [ $exit_code -ne 0 ]; then - echo "ERROR: $script_name failed to get resource: $location" >&2 - printf "See output log: %s\n" "$TMP_DIR/$conditional_get_log" >&2 + if [ $exit_code -gt 1 ]; then + echo "ERROR: $script_name failed to get resource: $location" >&2 + printf "See output log: %s\n" "$TMP_DIR/$conditional_get_log" >&2 + fi exit $exit_code fi +$verbose_mode && printf "$script_name successfully obtained resource: %s\n" "$tmp_file" /bin/cat "$tmp_file" exit 0