improve exit code use in bin scripts
The use of exit codes and exit code ranges has been made more consistent across most bin tools.
This commit is contained in:
1 parent
de0619a8b6
commit
09b8de5081
21 files changed
+1071
-1164
No files matched your search
@@ -55,7 +55,7 @@ shopt -s globstar # enable '**' to match all files and zero or more directories
|
||||
|
||||
# set variables referenced in the usage message
|
||||
#
|
||||
export VERSION="1.1 2024-02-11"
|
||||
export VERSION="1.1.1 2024-02-23"
|
||||
NAME=$(basename "$0")
|
||||
export NAME
|
||||
export V_FLAG=0
|
||||
@@ -63,7 +63,7 @@ GIT_TOOL=$(type -P git)
|
||||
export GIT_TOOL
|
||||
if [[ -z "$GIT_TOOL" ]]; then
|
||||
echo "$0: FATAL: git tool is not installed or is not in PATH" 1>&2
|
||||
exit 210
|
||||
exit 5
|
||||
fi
|
||||
"$GIT_TOOL" rev-parse --is-inside-work-tree >/dev/null 2>&1
|
||||
status="$?"
|
||||
@@ -113,8 +113,7 @@ Exit codes:
|
||||
4 bash version is < 4.2
|
||||
5 YYYY/dir is not a entry directory
|
||||
6 pandoc tool not found or not executable
|
||||
>= 10 < 210 ((not used))
|
||||
>= 210 internal tool error
|
||||
>= 10 internal error
|
||||
|
||||
$NAME version: $VERSION"
|
||||
|
||||
@@ -210,55 +209,28 @@ export REPO_NAME
|
||||
if [[ -z $TOPDIR ]]; then
|
||||
echo "$0: ERROR: cannot find top of git repo directory" 1>&2
|
||||
echo "$0: Notice: if needed: $GIT_TOOL clone $REPO_URL; cd $REPO_NAME" 1>&2
|
||||
exit 211
|
||||
exit 6
|
||||
fi
|
||||
if [[ ! -e $TOPDIR ]]; then
|
||||
echo "$0: ERROR: TOPDIR does not exist: $TOPDIR" 1>&2
|
||||
echo "$0: Notice: if needed: $GIT_TOOL clone $REPO_URL; cd $REPO_NAME" 1>&2
|
||||
exit 212
|
||||
exit 6
|
||||
fi
|
||||
if [[ ! -d $TOPDIR ]]; then
|
||||
echo "$0: ERROR: TOPDIR is not a directory: $TOPDIR" 1>&2
|
||||
echo "$0: Notice: if needed: $GIT_TOOL clone $REPO_URL; cd $REPO_NAME" 1>&2
|
||||
exit 213
|
||||
exit 6
|
||||
fi
|
||||
|
||||
# verify that we have an author subdirectory
|
||||
#
|
||||
export AUTHOR_PATH="$TOPDIR/author"
|
||||
if [[ ! -d $AUTHOR_PATH ]]; then
|
||||
echo "$0: ERROR: author is not a directory under topdir: $AUTHOR_PATH" 1>&2
|
||||
exit 214
|
||||
fi
|
||||
export AUTHOR_DIR="author"
|
||||
|
||||
# verify that we have an inc subdirectory
|
||||
#
|
||||
export INC_PATH="$TOPDIR/inc"
|
||||
if [[ ! -d $INC_PATH ]]; then
|
||||
echo "$0: ERROR: inc is not a directory under topdir: $INC_PATH" 1>&2
|
||||
exit 215
|
||||
fi
|
||||
export INC_DIR="inc"
|
||||
|
||||
# verify that we have a bin subdirectory
|
||||
#
|
||||
export BIN_PATH="$TOPDIR/bin"
|
||||
if [[ ! -d $BIN_PATH ]]; then
|
||||
echo "$0: ERROR: bin is not a directory under topdir: $BIN_PATH" 1>&2
|
||||
exit 216
|
||||
fi
|
||||
export BIN_DIR="bin"
|
||||
|
||||
# cd to topdir
|
||||
#
|
||||
if [[ ! -e $TOPDIR ]]; then
|
||||
echo "$0: ERROR: cannot cd to non-existent path: $TOPDIR" 1>&2
|
||||
exit 217
|
||||
exit 6
|
||||
fi
|
||||
if [[ ! -d $TOPDIR ]]; then
|
||||
echo "$0: ERROR: cannot cd to a non-directory: $TOPDIR" 1>&2
|
||||
exit 218
|
||||
exit 6
|
||||
fi
|
||||
export CD_FAILED
|
||||
if [[ $V_FLAG -ge 5 ]]; then
|
||||
@@ -267,96 +239,113 @@ fi
|
||||
cd "$TOPDIR" || CD_FAILED="true"
|
||||
if [[ -n $CD_FAILED ]]; then
|
||||
echo "$0: ERROR: cd $TOPDIR failed" 1>&2
|
||||
exit 219
|
||||
exit 6
|
||||
fi
|
||||
if [[ $V_FLAG -ge 3 ]]; then
|
||||
echo "$0: debug[3]: now in directory: $(/bin/pwd)" 1>&2
|
||||
fi
|
||||
|
||||
# verify that ENTRY_PATH is a entry directory
|
||||
# verify that we have an author subdirectory
|
||||
#
|
||||
# ENTRY_PATH must be in YYYY/dir form
|
||||
# YYYY must be a directory
|
||||
# YYYY must be a writable directory
|
||||
# YYYY/.year must be a non-empty file
|
||||
# YYYY/dir must be a directory
|
||||
# YYYY/dir/.path must be a non-empty file
|
||||
# ENTRY_PATH must match the contents of YYYY/dir/.path
|
||||
# YYYY/dir/.entry.json must be a non-empty file
|
||||
export AUTHOR_PATH="$TOPDIR/author"
|
||||
if [[ ! -d $AUTHOR_PATH ]]; then
|
||||
echo "$0: ERROR: author is not a directory under topdir: $AUTHOR_PATH" 1>&2
|
||||
exit 6
|
||||
fi
|
||||
export AUTHOR_DIR="author"
|
||||
|
||||
# verify that we have an inc subdirectory
|
||||
#
|
||||
export INC_PATH="$TOPDIR/inc"
|
||||
if [[ ! -d $INC_PATH ]]; then
|
||||
echo "$0: ERROR: inc is not a directory under topdir: $INC_PATH" 1>&2
|
||||
exit 6
|
||||
fi
|
||||
export INC_DIR="inc"
|
||||
|
||||
# verify that we have a bin subdirectory
|
||||
#
|
||||
export BIN_PATH="$TOPDIR/bin"
|
||||
if [[ ! -d $BIN_PATH ]]; then
|
||||
echo "$0: ERROR: bin is not a directory under topdir: $BIN_PATH" 1>&2
|
||||
exit 6
|
||||
fi
|
||||
export BIN_DIR="bin"
|
||||
|
||||
# verify that ENTRY_PATH is a entry directory
|
||||
#
|
||||
if [[ ! -d $ENTRY_PATH ]]; then
|
||||
echo "$0: ERROR: arg is not a directory: $ENTRY_PATH" 1>&2
|
||||
exit 5
|
||||
exit 3
|
||||
fi
|
||||
if [[ ! -w $ENTRY_PATH ]]; then
|
||||
echo "$0: ERROR: arg is not a writable directory: $ENTRY_PATH" 1>&2
|
||||
exit 5
|
||||
exit 3
|
||||
fi
|
||||
export YEAR_DIR=${ENTRY_PATH%%/*}
|
||||
if [[ -z $YEAR_DIR ]]; then
|
||||
echo "$0: ERROR: arg not in YYYY/dir form: $ENTRY_PATH" 1>&2
|
||||
exit 5
|
||||
exit 3
|
||||
fi
|
||||
export ENTRY_DIR=${ENTRY_PATH#*/}
|
||||
if [[ -z $ENTRY_DIR ]]; then
|
||||
echo "$0: ERROR: arg: $ENTRY_PATH not in $YEAR_DIR/dir form: $ENTRY_PATH" 1>&2
|
||||
exit 5
|
||||
exit 3
|
||||
fi
|
||||
if [[ $ENTRY_DIR = */* ]]; then
|
||||
echo "$0: ERROR: dir from arg: $ENTRY_PATH contains a /: $ENTRY_DIR" 1>&2
|
||||
exit 5
|
||||
exit 3
|
||||
fi
|
||||
if [[ ! -d $YEAR_DIR ]]; then
|
||||
echo "$0: ERROR: YYYY from arg: $ENTRY_PATH is not a directory: $YEAR_DIR" 1>&2
|
||||
exit 5
|
||||
exit 3
|
||||
fi
|
||||
export ENTRY_ID="${YEAR_DIR}_${ENTRY_DIR}"
|
||||
export DOT_YEAR="$YEAR_DIR/.year"
|
||||
if [[ ! -s $DOT_YEAR ]]; then
|
||||
echo "$0: ERROR: not a non-empty file: $DOT_YEAR" 1>&2
|
||||
exit 5
|
||||
exit 6
|
||||
fi
|
||||
# Now that we have moved to topdir, form and verify YYYY_DIR is a writable directory
|
||||
export YYYY_DIR="$YEAR_DIR/$ENTRY_DIR"
|
||||
if [[ ! -e $YYYY_DIR ]]; then
|
||||
echo "$0: ERROR: YYYY/dir from arg: $ENTRY_PATH does not exist: $YYYY_DIR" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
if [[ ! -d $YYYY_DIR ]]; then
|
||||
echo "$0: ERROR: YYYY/dir from arg: $ENTRY_PATH is not a directory: $YYYY_DIR" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
if [[ ! -w $YYYY_DIR ]]; then
|
||||
echo "$0: ERROR: YYYY/dir from arg: $ENTRY_PATH is not a writable directory: $YYYY_DIR" 1>&2
|
||||
exit 5
|
||||
fi
|
||||
if [[ ! -d $YYYY_DIR ]]; then
|
||||
echo "$0: ERROR: YYYY/dir from arg: $ENTRY_PATH is not a directory: $YYYY_DIR" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
export DOT_PATH="$YYYY_DIR/.path"
|
||||
if [[ ! -s $DOT_PATH ]]; then
|
||||
echo "$0: ERROR: not a non-empty file: $DOT_PATH" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
DOT_PATH_CONTENT=$(< "$DOT_PATH")
|
||||
if [[ $ENTRY_PATH != "$DOT_PATH_CONTENT" ]]; then
|
||||
echo "$0: ERROR: arg: $ENTRY_PATH does not match $DOT_PATH contents: $DOT_PATH_CONTENT" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
export ENTRY_JSON="$YYYY_DIR/.entry.json"
|
||||
if [[ ! -e $ENTRY_JSON ]]; then
|
||||
echo "$0: ERROR: .entry.json does not exist: $ENTRY_JSON" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
if [[ ! -f $ENTRY_JSON ]]; then
|
||||
echo "$0: ERROR: .entry.json is not a regular file: $ENTRY_JSON" 1>&2
|
||||
exit 5
|
||||
echo "$0: ERROR: .entry.json is not a file: $ENTRY_JSON" 1>&2
|
||||
exit 7
|
||||
fi
|
||||
if [[ ! -r $ENTRY_JSON ]]; then
|
||||
echo "$0: ERROR: .entry.json is not a readable file: $ENTRY_JSON" 1>&2
|
||||
exit 5
|
||||
exit 7
|
||||
fi
|
||||
|
||||
# verify pandoc wrapper tool
|
||||
#
|
||||
if [[ ! -e $PANDOC_WRAPPER ]]; then
|
||||
echo "$0: ERROR: pandoc wrapper tool does not exist: $PANDOC_WRAPPER" 1>&2
|
||||
exit 6
|
||||
@@ -439,12 +428,12 @@ if [[ -z $NOOP ]]; then
|
||||
rm -f "$TMP_FILE"
|
||||
if [[ -e $TMP_FILE ]]; then
|
||||
echo "$0: ERROR: cannot remove temporary markdown file: $TMP_FILE" 1>&2
|
||||
exit 220
|
||||
exit 10
|
||||
fi
|
||||
:> "$TMP_FILE"
|
||||
if [[ ! -e $TMP_FILE ]]; then
|
||||
echo "$0: ERROR: cannot create temporary markdown file: $TMP_FILE" 1>&2
|
||||
exit 221
|
||||
exit 11
|
||||
fi
|
||||
elif [[ $V_FLAG -ge 3 ]]; then
|
||||
echo "$0: debug[3]: because of -n, temporary markdown file is not used: $TMP_FILE" 1>&2
|
||||
@@ -461,12 +450,12 @@ if [[ -z $NOOP ]]; then
|
||||
rm -f "$TMP_MANIFEST"
|
||||
if [[ -e $TMP_MANIFEST ]]; then
|
||||
echo "$0: ERROR: cannot remove temporary manifest file: $TMP_MANIFEST" 1>&2
|
||||
exit 222
|
||||
exit 12
|
||||
fi
|
||||
:> "$TMP_MANIFEST"
|
||||
if [[ ! -e $TMP_MANIFEST ]]; then
|
||||
echo "$0: ERROR: cannot create temporary manifest file: $TMP_MANIFEST" 1>&2
|
||||
exit 223
|
||||
exit 13
|
||||
fi
|
||||
elif [[ $V_FLAG -ge 3 ]]; then
|
||||
echo "$0: debug[3]: because of -n, temporary manifest file is not used: $TMP_MANIFEST" 1>&2
|
||||
@@ -479,7 +468,7 @@ if [[ -z $NOOP ]]; then
|
||||
status="$?"
|
||||
if [[ $status -ne 0 ]]; then
|
||||
echo "$0: ERROR: manifest.entry.json.awk failed, error: $status" 1>&2
|
||||
exit 224
|
||||
exit 1
|
||||
fi
|
||||
{
|
||||
# load numerically sorted inventory_order lines into the temporary markdown file
|
||||
@@ -514,7 +503,7 @@ if [[ -z $NOOP ]]; then
|
||||
status="$?"
|
||||
if [[ $status -ne 0 ]]; then
|
||||
echo "$0: ERROR: pandoc failed, error: $status" 1>&2
|
||||
exit 225
|
||||
exit 1
|
||||
fi
|
||||
echo
|
||||
echo "<!-- END: next line ends content generated by: bin/$NAME -->"
|
||||
|
||||
Reference in new issue
Block a user