summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2016-03-04 21:08:24 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2016-03-04 21:08:24 (GMT)
commit572a1d743a9a21ef26c19f14d058bd820e6e0f87 (patch)
tree0bed5736cf5d420c3409c3d97ee99848143cce68
parentd029a1d4aaba4c9e2b023a5011e608b53a99a4ee (diff)
downloadcode-snippets-572a1d743a9a21ef26c19f14d058bd820e6e0f87.zip
code-snippets-572a1d743a9a21ef26c19f14d058bd820e6e0f87.tar.gz
code-snippets-572a1d743a9a21ef26c19f14d058bd820e6e0f87.tar.bz2
skeleton.sh: improve param handline, add 'set -u'
* add 'set -u': abort on underfined var usage. Adapt parameter parsing accordingly. * die(): use '$*' instead of '$@' to avoid multiple quoted parameters expansion * detect and abort on wrong number of non-option parameters. * print message when --verbose.
-rwxr-xr-x[-rw-r--r--]skeleton.sh27
1 files changed, 19 insertions, 8 deletions
diff --git a/skeleton.sh b/skeleton.sh
index e6f8316..24a6c0b 100644..100755
--- a/skeleton.sh
+++ b/skeleton.sh
@@ -1,5 +1,7 @@
#!/bin/sh
+set -u #abort when undefined vars are used
+
# Frobnicator
#
# Copyright (C) 2016 Assaf Gordon <assafgordon@gmail.com>
@@ -20,7 +22,7 @@
die()
{
BASE=$(basename "$0")
- echo "$BASE: error: $@" >&2
+ echo "$BASE: error: $*" >&2
exit 1
}
@@ -37,14 +39,15 @@ Options:
-v = Be verbose.
-P POOPOO = A random option
-" exit
+"
+ exit
}
##
## Script start, parse command-line parameteres
##
-show_usage=
+show_help=
verbose=
poopoo=
@@ -55,16 +58,24 @@ do
h) show_help=1;;
v) verbose=1;;
p) poopoo="$OPTARG";;
- ?) die "unknown command line option";;
+ ?) die "unknown/invalid command line option";;
esac
done
shift $(($OPTIND - 1))
-# First non-optional parameter
-infile="$1"
-
-# Validate input parameters
test -n "$show_help" && show_help_and_exit
+
+# Ensure is one non-option parameters (the input file)
+test $# -gt 1 && die "too many parameters ($*). See -h for help."
+test $# -lt 1 && die "missing input file. See -h for help."
+
+# first parameter - filename
+infile="$1"
test -z "$infile" && die "missing input file. See -h for help."
test -e "$infile" \
|| die "input file '$infile' not found"
+
+test "$poopoo" \
+ && msg="poopoo = '$poopoo'" \
+ || msg="no poopoo"
+test "$verbose" && echo "Processing file '$infile' ($msg)"