-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_example
More file actions
executable file
·54 lines (44 loc) · 1.68 KB
/
parse_example
File metadata and controls
executable file
·54 lines (44 loc) · 1.68 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
#!/usr/bin/env bash
#
# Copyright (c) 2020-2023 AlertAvert.com. All rights reserved.
#
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Author: Marco Massenzio (marco@alertavert.com)
#
# This prevents `source` to be invoked at all (and most likely error out)
# when parse-args fails to parse the given arguments.
set -e
# No modified indicates an optional, "regular" --option flag, named as the argument.
# A trailing `-` denotes a bool flag (its presence will set the associated variable, no
# value expected); use `!` for a required named option, and `+` for a required positional.
# Use `?` for an optional positional arg.
PARSED=$(python ./parse_args.py keep- take counts! mount+ attach* -- $@)
# The values are stored in a temporary file called ${PARSED}
# It can be safely ignored, and will be eventually removed by the system.
# This line injects the values into this script:
source ${PARSED}
# For example:
#
# $ ./parse_example --keep --take 3 --counts 5 /dev/fpo /dev/sba /dev/sbb
#
# Keeping mount: /dev/fpo
# Take 3, counts: 5
# attach was: [/dev/sba /dev/sbb]
# Mount point: /dev/sba
# Mount point: /dev/sbb
# Here we are using the parsed arguments as ordinary bash variables.
if [[ -n ${keep} ]]; then
echo "Keeping mount: ${mount}"
fi
echo "Take ${take}, counts: ${counts}"
# The last positional, `attach` is an array, so it may be empty or not;
# and, if empty, it won't cause an error.
echo "attach was: [${attach[*]}]"
# To iterate over the array, you can use:
for mount_point in "${attach[@]}"; do
echo "Mount point: $mount_point"
done
# This is optional, and removes the temporary file.
# rm ${PARSED}