/
sh.json
68 lines (68 loc) · 1.65 KB
/
sh.json
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
{
"File header": {
"prefix": ["new", "sh"],
"body": ["#!/bin/bash", "set -Eeuo pipefail"]
},
"Empty cleanup function": {
"prefix": ["trap", "cleanup"],
"body": ["cleanup() {", " $0", "}", "trap cleanup SIGINT SIGTERM EXIT"]
},
"Cleanup function that kills child pids": {
"prefix": ["cleanupkill"],
"body": [
"cleanup() {",
" $0",
"}",
"trap cleanup SIGINT SIGTERM EXIT",
"endsnippet",
"",
"snippet cleanup \"trap that kills child pids\" b!",
"_kill_tree() {",
" local pid",
" for pid in \\$*; do",
" local subpids=\"$(ps -o pid,ppid | awk '{if (\\$2 == '$pid') {print \\$1}}')\"",
" local subpid",
" for subpid in \\$subpids; do",
" _kill_tree \\$subpid",
" done",
" kill \\$pid || true",
" done",
"}",
"cleanup() {",
" _kill_tree \\$(jobs -p)",
"}",
"trap cleanup SIGINT SIGTERM EXIT"
]
},
"Main function": {
"prefix": "main",
"body": ["main() {", " $0", "}", "", "main \"\\$@\""]
},
"getopts": {
"prefix": "getopts",
"body": [
"local usage=\"$0\"",
"while getopts \"h-:\" opt; do",
" case \\$opt in",
" -)",
" case \\$OPTARG in",
" *)",
" echo \"\\$usage\"",
" exit 1",
" ;;",
" esac",
" ;;",
" h)",
" echo \"\\$usage\"",
" exit 0",
" ;;",
" \\?)",
" echo \"\\$usage\"",
" exit 1",
" ;;",
" esac",
"done",
"shift \\$((\\$OPTIND-1))"
]
}
}