Skip to content

InCommon/bash-library

52766f6044
Switch branches/tags

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?
Go to file
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
May 7, 2017 17:41

Bash Library

A library of re-usable bash scripts

Installation

Download the source, change directory to the source directory, and install the source into /tmp as follows:

$ export BIN_DIR=/tmp/bin
$ export LIB_DIR=/tmp/lib
$ ./install.sh $BIN_DIR $LIB_DIR

or install into your home directory:

$ export BIN_DIR=$HOME/bin
$ export LIB_DIR=$HOME/lib
$ ./install.sh $BIN_DIR $LIB_DIR

A given target directory will be created if one doesn't already exist. In any case, the following files will be installed:

$ ls -1 $BIN_DIR
cget.sh

$ ls -1 $LIB_DIR 
compatible_date.sh
config_tools.sh
core_lib.sh
entity_endpoints_txt.xsl
entity_identifiers_txt.xsl
entity_idp_names_txt.xsl
extract_entity.xsl
http_tools.sh
md_tools.sh
saml_tools.sh

Overview

cget.sh

Bash script cget.sh 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 server responds with 304 Not Modified, the cached resource is output instead.

First let's configure logging. For convenience, we will log directly to the terminal:

$ export LOG_FILE=/dev/tty
$ export LOG_LEVEL=3

Various log levels are supported:

LOG_LEVEL
TRACE 5
DEBUG 4
INFO 3
WARN 2
ERROR 1
FATAL 0

The default logging level is INFO (i.e., if you do not explicitly set the LOG_LEVEL environment variable, the value LOG_LEVEL=3 is assumed by default).

Next we define a couple of HTTP resources and a cache:

$ url1=http://md.incommon.org/InCommon/InCommon-metadata-preview.xml
$ url2=http://md.incommon.org/InCommon/InCommon-metadata-fallback.xml
$ export CACHE_DIR=/tmp/http_cache

Now GET the first resource:

$ $BIN_DIR/cget.sh $url1 > /dev/null
2017-05-02T15:29:35Z INFO cget.sh requesting resource: http://md.incommon.org/InCommon/InCommon-metadata-preview.xml
2017-05-02T15:29:41Z INFO conditional_get received response code: 200
2017-05-02T15:29:41Z INFO conditional_get writing cached content file: /tmp/http_cache/1e6b844a49d1850b82feded72cf83ed7_content
2017-05-02T15:29:41Z INFO conditional_get reading cached content file: /tmp/http_cache/1e6b844a49d1850b82feded72cf83ed7_content
$ echo $?
0
$ ls -1 $CACHE_DIR
1e6b844a49d1850b82feded72cf83ed7_content
1e6b844a49d1850b82feded72cf83ed7_headers
$ cat $CACHE_DIR/1e6b844a49d1850b82feded72cf83ed7_headers
HTTP/1.1 200 OK
Date: Tue, 02 May 2017 15:29:34 GMT
Server: Apache
Last-Modified: Mon, 01 May 2017 19:02:13 GMT
ETag: "29bcdb4-54e7b0fa340c5"
Accept-Ranges: bytes
Content-Length: 43765172
Content-Type: application/samlmetadata+xml

Assuming the resource doesn't change on the server, subsequent requests will return the cached resource. To bypass the network altogether, use the -C option:

$ $BIN_DIR/cget.sh -C $url1 | wc -c
2017-05-02T15:30:39Z INFO cget.sh requesting resource: http://md.incommon.org/InCommon/InCommon-metadata-preview.xml
2017-05-02T15:30:39Z INFO conditional_get reading cached content file: /tmp/http_cache/1e6b844a49d1850b82feded72cf83ed7_content
 43765172

Of course the -C option will fail if the resource is not cached:

# illustrate "quiet failure mode"
$ $BIN_DIR/cget.sh -C $url2
2017-05-02T15:31:07Z INFO cget.sh requesting resource: http://md.incommon.org/InCommon/InCommon-metadata-fallback.xml
2017-05-02T15:31:07Z ERROR conditional_get: resource not cached: http://md.incommon.org/InCommon/InCommon-metadata-fallback.xml
$ echo $?
1

OTOH, the -F option forces the return of a fresh resource from the server. If the resource is cached and unchanged on the server (304), such a request will fail, however:

# further illustrate "quiet failure mode"
$ $BIN_DIR/cget.sh -F $url1
2017-05-02T15:31:42Z INFO cget.sh requesting resource: http://md.incommon.org/InCommon/InCommon-metadata-preview.xml
2017-05-02T15:31:48Z INFO conditional_get received response code: 304
2017-05-02T15:31:48Z ERROR conditional_get: resource not modified: http://md.incommon.org/InCommon/InCommon-metadata-preview.xml
$ echo $?
1

The -F option will work on the other URL, however:

$ $BIN_DIR/cget.sh -F $url2 > /dev/null
2017-05-02T15:32:27Z INFO cget.sh requesting resource: http://md.incommon.org/InCommon/InCommon-metadata-fallback.xml
2017-05-02T15:32:32Z INFO conditional_get received response code: 200
2017-05-02T15:32:32Z INFO conditional_get writing cached content file: /tmp/http_cache/1727196e5b7593f3b7528c539e7169d2_content
2017-05-02T15:32:32Z INFO conditional_get reading cached content file: /tmp/http_cache/1727196e5b7593f3b7528c539e7169d2_content
$ echo $?
0
$ ls -1 $CACHE_DIR
1727196e5b7593f3b7528c539e7169d2_content
1727196e5b7593f3b7528c539e7169d2_headers
1e6b844a49d1850b82feded72cf83ed7_content
1e6b844a49d1850b82feded72cf83ed7_headers
$ cat $CACHE_DIR/1727196e5b7593f3b7528c539e7169d2_headers
HTTP/1.1 200 OK
Date: Tue, 02 May 2017 15:32:26 GMT
Server: Apache
Last-Modified: Mon, 01 May 2017 19:02:13 GMT
ETag: "29bcdb4-54e7b0fa246c3"
Accept-Ranges: bytes
Content-Length: 43765172
Content-Type: application/samlmetadata+xml

See the inline help file for details:

$ $BIN_DIR/cget.sh -h

Compatibility

Shell scripts are compatible with both GNU/Linux and Mac OS. XSLT scripts are written in XSLT 1.0.

Dependencies

None

About

Library of re-usable bash scripts

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published