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
+49
-60
@@ -49,7 +49,7 @@ shopt -s globstar # enable ** to match all files and zero or more directories an
|
||||
|
||||
# set variables referenced in the usage message
|
||||
#
|
||||
export VERSION="1.1 2024-02-17"
|
||||
export VERSION="1.1.1 2024-02-23"
|
||||
NAME=$(basename "$0")
|
||||
export NAME
|
||||
export V_FLAG=0
|
||||
@@ -57,7 +57,7 @@ GIT_TOOL=$(type -P git)
|
||||
export GIT_TOOL
|
||||
if [[ -z "$GIT_TOOL" ]]; then
|
||||
echo "$0: FATAL: git tool is not installed or not in \$PATH" 1>&2
|
||||
exit 210
|
||||
exit 5
|
||||
fi
|
||||
"$GIT_TOOL" rev-parse --is-inside-work-tree >/dev/null 2>&1
|
||||
status="$?"
|
||||
@@ -92,16 +92,14 @@ export USAGE="usage: $0 [-h] [-v level] [-V] [-d topdir] [-D docroot/] [-n] [-N]
|
||||
|
||||
Exit codes:
|
||||
0 all OK
|
||||
1 entry files to not match manifest in .entry.json
|
||||
1 some internal tool exited non-zero
|
||||
2 -h and help string printed or -V and version string printed
|
||||
3 command line error
|
||||
4 bash version is < 4.2
|
||||
5 YYYY/dir is not a entry directory
|
||||
6 bin/filelist.entry.json.awk is not a non-empty readable file
|
||||
7 error in trying to determine files used by the entry
|
||||
8 missing critical top level file
|
||||
>= 10 < 210 ((not used))
|
||||
>= 210 internal tool error
|
||||
5 some internal tool is not found or not an executable file
|
||||
6 problems found with or in the topdir or topdir/YYYY directory
|
||||
7 problems found with or in the entry topdir/YYYY/dir directory
|
||||
>= 10 internal error
|
||||
|
||||
$NAME version: $VERSION"
|
||||
|
||||
@@ -177,37 +175,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 220
|
||||
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 221
|
||||
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 222
|
||||
exit 6
|
||||
fi
|
||||
|
||||
# verify that we have an 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 223
|
||||
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 224
|
||||
exit 6
|
||||
fi
|
||||
if [[ ! -d $TOPDIR ]]; then
|
||||
echo "$0: ERROR: cannot cd to a non-directory: $TOPDIR" 1>&2
|
||||
exit 225
|
||||
exit 6
|
||||
fi
|
||||
export CD_FAILED
|
||||
if [[ $V_FLAG -ge 5 ]]; then
|
||||
@@ -216,91 +205,91 @@ fi
|
||||
cd "$TOPDIR" || CD_FAILED="true"
|
||||
if [[ -n $CD_FAILED ]]; then
|
||||
echo "$0: ERROR: cd $TOPDIR failed" 1>&2
|
||||
exit 226
|
||||
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 bin 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 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
|
||||
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 file: $ENTRY_JSON" 1>&2
|
||||
exit 5
|
||||
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 we have our awk tool
|
||||
@@ -308,19 +297,19 @@ fi
|
||||
FILELIST_ENTRY_JSON_AWK="$BIN_DIR/filelist.entry.json.awk"
|
||||
if [[ ! -e $FILELIST_ENTRY_JSON_AWK ]]; then
|
||||
echo "$0: ERROR: filelist.entry.json.awk does not exist: $FILELIST_ENTRY_JSON_AWK" 1>&2
|
||||
exit 6
|
||||
exit 5
|
||||
fi
|
||||
if [[ ! -f $FILELIST_ENTRY_JSON_AWK ]]; then
|
||||
echo "$0: ERROR: filelist.entry.json.awk is not a file: $FILELIST_ENTRY_JSON_AWK" 1>&2
|
||||
exit 6
|
||||
exit 5
|
||||
fi
|
||||
if [[ ! -r $FILELIST_ENTRY_JSON_AWK ]]; then
|
||||
echo "$0: ERROR: filelist.entry.json.awk is not a readable file: $FILELIST_ENTRY_JSON_AWK" 1>&2
|
||||
exit 6
|
||||
exit 5
|
||||
fi
|
||||
if [[ ! -s $FILELIST_ENTRY_JSON_AWK ]]; then
|
||||
echo "$0: ERROR: filelist.entry.json.awk is not a not a non-empty readable file: $FILELIST_ENTRY_JSON_AWK" 1>&2
|
||||
exit 6
|
||||
exit 5
|
||||
fi
|
||||
|
||||
# parameter debugging
|
||||
@@ -368,12 +357,12 @@ if [[ -z $NOOP ]]; then
|
||||
rm -f "$TMP_MANIFEST_LIST"
|
||||
if [[ -e $TMP_MANIFEST_LIST ]]; then
|
||||
echo "$0: ERROR: cannot remove temporary file manifest list: $TMP_MANIFEST_LIST" 1>&2
|
||||
exit 227
|
||||
exit 10
|
||||
fi
|
||||
:> "$TMP_MANIFEST_LIST"
|
||||
if [[ ! -e $TMP_MANIFEST_LIST ]]; then
|
||||
echo "$0: ERROR: cannot create temporary file manifest list: $TMP_MANIFEST_LIST" 1>&2
|
||||
exit 228
|
||||
exit 11
|
||||
fi
|
||||
elif [[ $V_FLAG -ge 3 ]]; then
|
||||
echo "$0: debug[3]: because of -n, temporary file manifest list is not used: $TMP_MANIFEST_LIST" 1>&2
|
||||
@@ -390,12 +379,12 @@ if [[ -z $NOOP ]]; then
|
||||
rm -f "$TMP_FILE_LIST"
|
||||
if [[ -e $TMP_FILE_LIST ]]; then
|
||||
echo "$0: ERROR: cannot remove temporary file manifest list: $TMP_FILE_LIST" 1>&2
|
||||
exit 227
|
||||
exit 12
|
||||
fi
|
||||
:> "$TMP_FILE_LIST"
|
||||
if [[ ! -e $TMP_FILE_LIST ]]; then
|
||||
echo "$0: ERROR: cannot create temporary file manifest list: $TMP_FILE_LIST" 1>&2
|
||||
exit 228
|
||||
exit 13
|
||||
fi
|
||||
elif [[ $V_FLAG -ge 3 ]]; then
|
||||
echo "$0: debug[3]: because of -n, temporary file manifest list is not used: $TMP_FILE_LIST" 1>&2
|
||||
@@ -409,13 +398,13 @@ awk -f "$FILELIST_ENTRY_JSON_AWK" "$ENTRY_JSON" > "$TMP_MANIFEST_LIST"
|
||||
status="$?"
|
||||
if [[ $status -ne 0 ]]; then
|
||||
echo "$0: ERROR: awk -f $FILELIST_ENTRY_JSON_AWK $ENTRY_JSON > $TMP_MANIFEST_LIST failed, error: $status" 1>&2
|
||||
exit 7
|
||||
exit 1
|
||||
fi
|
||||
sort -d -u "$TMP_MANIFEST_LIST" -o "$TMP_MANIFEST_LIST"
|
||||
status="$?"
|
||||
if [[ $status -ne 0 ]]; then
|
||||
echo "$0: ERROR: sort -d -u $TMP_MANIFEST_LIST -o $TMP_MANIFEST_LIST failed, error: $status" 1>&2
|
||||
exit 7
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# generate sorted list of found entry files
|
||||
@@ -426,13 +415,13 @@ find "$ENTRY_PATH" -type f -print > "$TMP_FILE_LIST"
|
||||
status="$?"
|
||||
if [[ $status -ne 0 ]]; then
|
||||
echo "$0: ERROR: find $ENTRY_PATH -type f -print > $TMP_FILE_LIST failed, error: $status" 1>&2
|
||||
exit 7
|
||||
exit 1
|
||||
fi
|
||||
sort -d -u "$TMP_FILE_LIST" -o "$TMP_FILE_LIST"
|
||||
status="$?"
|
||||
if [[ $status -ne 0 ]]; then
|
||||
echo "$0: ERROR: sort -d -u $TMP_FILE_LIST -o $TMP_FILE_LIST failed, error: $status" 1>&2
|
||||
exit 7
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# note if the manifest does NOT match the file list
|
||||
|
||||
Reference in new issue
Block a user