-
Notifications
You must be signed in to change notification settings - Fork 8
/
argument-parser.sh
executable file
·61 lines (54 loc) · 1.08 KB
/
argument-parser.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
usage()
{
echo "Usage: $0 [-s|-r] [ -d DB_DUMP ] [ -f TARBALL ]"
exit 2
}
# assigne argument to variable
set_variable()
{
local varname=$1
shift
if [ -z "${!varname}" ]; then
# string as command, execute string
eval "$varname=\"$@\""
else
echo "Error: $varname already set"
usage
fi
}
#########################
# Main script starts here
unset DB_DUMP TARBALL ACTION
while getopts 'srd:f:?h' c
do
case $c in
s) set_variable ACTION SAVE ;;
r) set_variable ACTION RESTORE ;;
d)
# process variable with argument
set_variable DB_DUMP $OPTARG
;;
f)
# process variable with argument
set_variable TARBALL $OPTARG
;;
h|?)
# error processing
usage
;;
esac
done
[ -z "$ACTION" ] && usage
[ -z "$DB_DUMP" ] && [ -z "$TARBALL" ] && usage
if [ -n "$DB_DUMP" ]; then
case $ACTION in
SAVE) save_database $DB_DUMP ;;
RESTORE) restore_database $DB_DUMP ;;
esac
fi
if [ -n "$TARBALL" ]; then
case $ACTION in
SAVE) save_files $TARBALL ;;
RESTORE) restore_files $TARBALL ;;
esac
fi