Created
April 19, 2021 08:42
-
-
Save tedsecretsource/1ed6913445277015cf2a5f8803d73045 to your computer and use it in GitHub Desktop.
Default bash script in strict mode
This file contains hidden or 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 | |
set -euo pipefail | |
IFS=$'\n\t' | |
# Run this script with bash -x (script name) to execute commands one a time | |
# while IFS=' ', read -r col1 col2 col3 col4 col5; do echo "I got: $col1 | $col2"; done < authors.csv | |
# while IFS=' ', read LINE; do echo $LINE | cut -f 2; done < authors.csv | |
#/ Usage: ./process_authors.sh -p migration-support/authors.tsv -s '' | |
#/ | |
#/ Description: Transform authors.xml into TSV and then | |
#/ import into a WordPress installation. This scripts expects to be | |
#/ run from the root of a WordPress installation and requires all | |
#/ supporting scripts be located in a folder called | |
#/ "migration-support" in the same directory. All parameters are | |
#/ required even though they may be empty strings (''). | |
#/ Examples: (same as usage above) | |
#/ Options: | |
#/ --help: Display this help message | |
#/ -p|--path_to_input_xml: path to the TSV file | |
#/ -s|--site: the URL of the multisite being updated, empty string if | |
#/ not a multisite installation | |
usage() { grep '^#/' "$0" | cut -c4- ; exit 0 ; } | |
expr "$*" : ".*--help" > /dev/null && usage | |
# get the named parameters | |
while [ "${1:-}" != "" ]; do | |
case $1 in | |
-p | --path_to_a_file) shift | |
path_to_input_xml=${1:-} | |
;; | |
-s | --additional_input) shift | |
additional_input=${1:-} | |
;; | |
-h | --help ) usage | |
exit | |
;; | |
* ) usage | |
exit 1 | |
esac | |
shift | |
done | |
# tiny logging framework, logs have datetime stamps | |
DATEFILE='%Y%m%d%H%M%S' | |
DATESTAMP='%Y-%m-%d %H:%M:%S' | |
mkdir -p 'logs' | |
readonly LOG_FILE="logs/$(basename "$0")-$(basename "$path_to_input_xml").log" | |
echo '' > "$LOG_FILE" | |
info() { echo "[INFO] $$ $*" | tee -a "$LOG_FILE" >&2 ; } | |
warning() { echo "[WARNING] $$ $*" | tee -a "$LOG_FILE" >&2 ; } | |
error() { echo "[ERROR] $$ $*" | tee -a "$LOG_FILE" >&2 ; } | |
fatal() { echo "[FATAL] $$ $*" | tee -a "$LOG_FILE" >&2 ; exit 1 ; } | |
# what to do when the script is interrupted, Ctrl+C, for example | |
function cleanup() { | |
# Restart services | |
echo "Cleaning up $$" | |
exit 0 | |
} | |
# traps commands that exit with an error status | |
function onerror_handler() { | |
MYLINE=$1 | |
error "An error occurred on line $MYLINE." | |
exit 127 | |
} | |
# and finally, the actual script | |
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then | |
# Trap Ctrl+C and respond accordingly | |
trap 'cleanup $LINENO' INT | |
# Trap an error in the code and respond accordingly | |
trap 'onerror_handler $LINENO' ERR | |
# Rest of script goes here | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment