-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparameters-using-bash.sh
More file actions
executable file
·130 lines (100 loc) · 3.56 KB
/
Copy pathparameters-using-bash.sh
File metadata and controls
executable file
·130 lines (100 loc) · 3.56 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# This script demonstrates how to handle parameter with bash.
# This approach allows long name redundancy (for readability
# purposes).
usage() {
echo "
Usage: $(basename $0) [-d|--debug [0-3]] [-h|--human] <-o|--output filename> filelist
Display information about a file.
Options:
--help Display this usage info
-d, --debug Enable debug (default level 1) - Optional param with optional value
-o, --output Output file - Mandatory param with mandatory value
-h, --human Make info human-readable - Flag, no value expected
filelist Input files separated by space - Value after all options
"
}
# Variables to store the value read from params
PARAM_DEBUG=0
PARAM_OUTPUT=""
PARAM_HUMAN=0
PARAM_FILELIST=""
# Iterates over the params to process each one.
while (( "$#" )); do # $# = size of params array.
# decrements on every call to `shift`
ARG="$1" # $1 is the pointer to the current param item (move it forward with `shift`)
NEXT_ARG="$2"
HAS_NEXT_ARG="0" && [ ! -z $2 ] && HAS_NEXT_ARG="1"
IS_NEXT_ARG_PARAM="0" && [[ $2 = -* ]] && IS_NEXT_ARG_PARAM="1" # check if starts with "-"
case "$ARG" in
# --help takes most priority, nothing else should happen
# when it is present.
--help)
usage
exit 0
;;
-h | --human) PARAM_HUMAN="1" ;; # one-liner for simple flags
-d | --debug)
PARAM_DEBUG="1" # set default value to 1
# value is optional
if [ "$HAS_NEXT_ARG" = "0" ] || [ "$IS_NEXT_ARG_PARAM" = "1" ]; then
shift
continue
fi
# value must be number
if ! [[ $NEXT_ARG =~ ^[0-9]+$ ]]; then
echo "Error: debug level must be a number. \"$NEXT_ARG\" provided."
usage
exit 1
fi
# check value range
if (( $NEXT_ARG < 1 )) || (( $NEXT_ARG > 3 )); then
echo "Error: debug level must be from 1 to 3"
usage
exit 1
fi
PARAM_DEBUG="$NEXT_ARG"
shift
;;
-o | --output)
if [ "$HAS_NEXT_ARG" = "0" ] || [ $IS_NEXT_ARG_PARAM = "1" ]; then
echo "The argument $ARG expects one output file"
usage
exit 1
fi
PARAM_OUTPUT=$NEXT_ARG
shift
;;
-?*)
echo "Unknow option: $ARG"
usage
exit 1
;;
*)
# not matching any of the above, must be the end of filelist
break;
esac
shift # iterates the $1 pointer in the arguments list
done
# gets what's left after the current position of the arguments pointer
# if nothing is provided, read from stdin in case it is coming from a pipe
REST="$*" && [ -z "$REST" ] && REST=$(cat -)
(( $PARAM_DEBUG > 0 )) && echo "[DEBUG] Debug enabled (level = $PARAM_DEBUG)"
(( $PARAM_DEBUG >= 1 )) && echo "[DEBUG] The human readable flag is "$PARAM_HUMAN
if [ -z "$PARAM_OUTPUT" ]; then
echo "Error: The output file is mandatory argument"
usage
exit 1
fi
(( $PARAM_DEBUG >= 1 )) && echo "[DEBUG] output file set as $PARAM_OUTPUT"
if [ -z "$REST" ]; then
echo "Error: no file list provided"
usage
exit 1
else
echo "File list:"
for file in $REST; do
echo " - $file"
done
fi
# From here the script can do its magic using the params :)