-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGETOPTS-SHELL-SCRIPT.txt
More file actions
78 lines (59 loc) · 1.43 KB
/
GETOPTS-SHELL-SCRIPT.txt
File metadata and controls
78 lines (59 loc) · 1.43 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
http://www.theunixschool.com/2012/08/getopts-how-to-pass-command-line-options-shell-script-Linux.html
https://www.lifewire.com/pass-arguments-to-bash-script-2200571
https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/ --> IMP
https://www.shellscript.sh/tips/getopts/
https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/
while getopts u:d:p:f: option
do
case "${option}"
in
u) USER=${OPTARG};;
d) DATE=${OPTARG};;
p) PRODUCT=${OPTARG};;
f) FORMAT=${OPTARG};;
esac
done
========================================
#!/bin/bash
usage()
{
echo "Usage: $0 [-s|-r] [ -d DB_DUMP ] [ -f TARBALL ]"
exit 2
}
set_variable()
{
local varname=$1
shift
if [ -z "${!varname}" ]; then
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) set_variable DB_DUMP $OPTARG ;;
f) set_variable TARBALL $OPTARG ;;
h|?) 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