-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparadigm.sh
executable file
·65 lines (49 loc) · 1.31 KB
/
paradigm.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
62
63
64
65
#!/bin/bash
: ' paradigm.sh
This script is an example template for writing quality, reusable bash scripts.
See the examples at the bottom of the script for usage.
'
help() {
# This will get called if there is an error
echo $0 [function] [args]
echo "How to use this script"
}
foo() {
# Inputs: $1, $2, ... "$@"
# Output: whatever is echoed to STDOUT
echo $1
}
bar() {
# Call a function in this script and store the output in a variable
BAR=$(foo bar)
echo bar: $BAR
# Stored variables are available to any functions called by this function
# Temporary variables can be passed inline to called functions.
}
: '
This is where the magic is:
Running `"$@"` will execute the command passed to this script as a bash command
If there is an error parsing the command, the `help` function will be called
and the script will exit with code 1
'
"$@" || (help && exit 1)
: '
Other benefits:
- Functions can be called directly from the CLI
- Script does not have to be installed beforehand
- Leaves no trace in your source
Warning:
This script will execute any command passed to it as a bash command. Hic sunt dracones.
Examples:
./script foo bar
-> bar
./script bar
-> bar: bar
./script help
-> help message
-> (exit code 0)
./script helppp
-> helppp: command not found
-> help message
-> (exit code 1)
'