-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__script_template.sh
More file actions
executable file
·128 lines (101 loc) · 3.07 KB
/
__script_template.sh
File metadata and controls
executable file
·128 lines (101 loc) · 3.07 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
#!/bin/bash
# __script_template.sh
#
# Script template and `bash-tools` demo.
# -------------------------- HEADER -------------------------------------------
set -eEo pipefail
shopt -s inherit_errexit
this_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
source "$this_dir/bash-tools.sh"
usage="Usage: $(basename "${BASH_SOURCE[0]}") [options]
Script template and \`bash-tools\` demo.
Options:
--the-secret-of-life ${underline}val${nounderline} Why are we here? Just to suffer? (default: 42)
--no-banner For the esteemed, joyless professional
--verbose, -v Sets log-level to 'trace'
--help, -h Show this message"
# `getopt` arg parsing
# see: file:///usr/share/doc/util-linux/examples/getopt-example.bash
_parsed_args=$(getopt \
--options='h,v' \
--longoptions='help,no-banner,verbose,the-secret-of-life:' \
--name "$(basename "${BASH_SOURCE[0]}")" -- "$@")
eval set -- "$_parsed_args"
unset _parsed_args
the_secret_of_life=42
no_banner=false
while true; do
case "$1" in
--the-secret-of-life)
the_secret_of_life="$2"
shift 2
continue
;;
-v | --verbose)
set_loglevel trace
shift 1
continue
;;
--no-banner)
no_banner=true
shift 1
continue
;;
-h | --help)
stdout "$usage"
exit 0
;;
--)
shift
break
;;
*)
log error "unknown argument: $1"
exit 1
;;
esac
done
# -------------------------- PRECONDITIONS ------------------------------------
assert_not_on_host 'ARPANET'
assert_not_sourced
reset_checks
check_is_positive_integer the_secret_of_life
check_is_boolean no_banner
check_is_defined USER
for _command in awk grep curl sleep; do
check_command_exists_on_path _command
done
print_failed_checks --error || exit
# -------------------------- BANNER -------------------------------------------
if [[ $no_banner == false ]]; then
# see: https://www.asciiart.eu/text-to-ascii-art
show_banner "${theme_value}${bold}" <<'EOF'
__ __ __ __
/ /_ ____ ______/ /_ / /_____ ____ / /____
/ __ \/ __ `/ ___/ __ \______/ __/ __ \/ __ \/ / ___/
/ /_/ / /_/ (__ ) / / /_____/ /_/ /_/ / /_/ / (__ )
/_.___/\__,_/____/_/ /_/ \__/\____/\____/_/____/
EOF
# -------------------------- PREAMBLE ---------------------------------------
cat <<-EOF
This file is a starter template for new scripts, and a light demo of ${theme_value}bash-tools${color_reset}.
EOF
press_any_key_to_continue
fi
# -------------------------- RECONNAISSANCE -----------------------------------
if [[ ! -d "$this_dir" ]]; then
log warn "Existential crisis detected!"
continue_or_exit
fi
# -------------------------- EXECUTION ----------------------------------------
trap 'on_err' ERR
log info "Hello, ${theme_value}$USER${color_reset}!"
if yes_or_no --default-yes "Can I tell you something?"; then
log warn "The secret of life is: ${theme_value}$the_secret_of_life${color_reset}"
else
log trace "User chose to remain uninitiated, base, wallowing."
fi
# -------------------------- POSTCONDITIONS -----------------------------------
stderr -n "We are feeling..."
sleep 1
stderr "OK"