-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellscript.json
496 lines (470 loc) · 13.1 KB
/
shellscript.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
{
// Place your snippets for shellscript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"Run as root":
{
"prefix": "runAsRoot",
"description": "Runs a script as root if executed by non-root user.",
"body":
[
"# If not run by root, run script as root.",
"# Options for sudo (choose what is appropriate for this script):",
"# -i: Short for --login. /root/.profile will be sourced, but /root/.bashrc",
"# will exit immediately.",
"# Thus, script path variables and \\$PATH containing kalle's scripts' bin dir",
"# exported within /root/.profile will be available to this script.",
"# -E: The environment of the calling user will be preserved; nothing will be sourced.",
"# If this is kalle, you will have the script path variables available to this",
"# script, but merely root's default \\$PATH.",
"# If no option is provided, you will merely have root's default \\$PATH and no",
"# script path variables available to this script.",
"#",
"# If this script exports variables before this boilerplate (e. g. when using the",
"# run-in-screen boilerplate) that shall be available to this script after it has",
"# been executed by root, you must use -E.",
"# If you additionally need -i, you must replace ‘sudo [-i|-E]’ below with",
"# ‘sudo -E bash -l’ because -i and -E are mutually exclusive.",
"# But by using ‘bash -l’, you get the same effect as with ‘sudo -i’",
"# (-l is for bash what -i is for sudo).",
"# If you also use the run-in-screen boilerplate, place it before this one.",
"((\\$(id -u))) && exec sudo${1| -i , -E , , -E bash -l |}\"$(cd \"\\$(dirname \"\\$0\")\"; pwd)/$(basename \\$0)\" \"$@\""
]
},
"Get absolute path of script":
{
"prefix": "scriptAbsPath",
"description": "Get absolute path of script or link to it no matter how it has been invoked.",
"body": ["$(cd \\$(dirname \"\\$0\"); pwd)/\\$(basename \"\\$0\")"]
},
"Safely use getopts in a function":
{
"prefix": "getoptsInFunction",
"body":
[
"if ((OPTIND != 1)); then",
"\techo -n \"Error: \\$(basename \\$0): \"",
"\techo -n \"In function \\${BASH_FUNCNAME[0]}, line \\${BASH_LINENO[0]}: \"",
"\techo \"OPTIND == \\$OPTIND > 1.\"",
"\techo 'Your function was about to call getopts,'",
"\techo -e \"but a previous option processing has not finished yet. Exiting\\n\"",
"\techo \"----- Call Trace -----\"",
"",
"\tlocal line file function i=0",
"",
"\twhile caller=\\$(caller \\$i); do",
"\t\tline=\\$(cut -f 1 -d ' ' <<<\\$caller)",
"\t\tfile=\\$(cut -f 3 -d ' ' <<<\\$caller)",
"\t\tfunction=\\$(cut -f 2 -d ' ' <<<\\$caller)",
"",
"\t\tif [[ \\$function != source ]]; then",
"\t\t\tfunction=\"(function: \\$function)\"",
"\t\telse",
"\t\t\tfunction=\"(\\$function)\"",
"\t\tfi",
"",
"\t\techo \"\\$file Line: \\$line \\$function\"",
"",
"\t\t((i++))",
"\tdone",
"",
"\texit 1",
"fi",
"",
"local opt",
"",
"while getopts '$1' opt; do",
"\tcase \\$opt in",
"\t\t$2)",
"\t\t\t$0",
"\t\t\t;;",
"\tesac",
"done",
"",
"# Remove option args from positional parameters.",
"# N.B.: After reading all option args, \\$OPTIND is the index of the first",
"# non-option arg, thus \\$((OPTIND - 1)) is the number of option args.",
"shift \\$((OPTIND - 1))",
"",
"# Reset \\$OPTIND to indicate we have finished using getopts.",
"OPTIND=1"
]
},
"Use getopts in a standalone script":
{
"prefix": "getoptsInStandaloneScript",
"body":
[
"local opt\n",
"while getopts '$1' opt; do",
"\tcase \\$opt in",
"\t\t$2)",
"\t\t\t$0",
"\t\t\t;;",
"\tesac",
"done\n",
"# Remove option args from positional parameters.",
"# N.B.: After reading all option args, \\$OPTIND is the index of the first",
"# non-option arg, thus \\$((OPTIND - 1)) is the number of option args.",
"shift \\$((OPTIND - 1))\n"
]
},
// See "Prompt user for choice (Yn)" in jsonc.json for a version for usage within a shell Task.
"Prompt user for choice (Yn)":
{
"prefix": "prompt_choice_Yn",
"body":
[
"echo -en \"\\e[1m\\e[97m${1:promptText}\\e[0m \\e[97m[Y/n] \\e[1m\"\n",
"command -v tput >/dev/null && haveTput=1",
"((haveTput)) && tput sc\n",
"while true; do",
"read -n 1 ans\n",
"\tcase \\$ans in",
"\t\t'')",
"\t\t\techo -en \"\\e[0m\"",
"\t\t\t;;&",
"\t\t[yYnN])",
"\t\t\techo -e \"\\e[0m\"",
"\t\t\t;;&",
"\t\t[yY]|'')",
"\t\t\t${2:commandsY}",
"",
"\t\t\tbreak",
"\t\t\t;;",
"\t\t[nN])",
"\t\t\t${3:commandsN}",
"",
"\t\t\tbreak",
"\t\t\t;;",
"\t\t*)",
"\t\t\t((haveTput)) && tput rc && tput el # Restore cursor pos and clear to end of line (see man terminfo).",
"\t\t\t;;",
"\tesac",
"done",
]
},
// See "Prompt user for choice (yN)" in jsonc.json for a version for usage within a shell Task.
"Prompt user for choice (yN)":
{
"prefix": "prompt_choice_yN",
"body":
[
"echo -en \"\\e[1m\\e[97m${1:promptText}\\e[0m \\e[97m[y/N] \\e[1m\"\n",
"command -v tput >/dev/null && haveTput=1",
"((haveTput)) && tput sc\n",
"while true; do",
"read -n 1 ans\n",
"\tcase \\$ans in",
"\t\t'')",
"\t\t\techo -en \"\\e[0m\"",
"\t\t\t;;&",
"\t\t[yYnN])",
"\t\t\techo -e \"\\e[0m\"",
"\t\t\t;;&",
"\t\t[yY])",
"\t\t\t${2:commandsY}",
"",
"\t\t\tbreak",
"\t\t\t;;",
"\t\t[nN]|'')",
"\t\t\t${3:commandsN}",
"",
"\t\t\tbreak",
"\t\t\t;;",
"\t\t*)",
"\t\t\t((haveTput)) && tput rc && tput el # Restore cursor pos and clear to end of line (see man terminfo).",
"\t\t\t;;",
"\tesac",
"done",
]
},
"Run command with nullglob (single line)":
{
"prefix": "runWithNullglobSingleLine",
"description":
[
"Runs the specified command with nullglob.",
"Leaving nullglob on for the rest of a script can cause strange side effects (but I don't remember them)."
],
"body":
[
"shopt -s nullglob; ${1:command}; shopt -u nullglob",
]
},
"Run command with nullglob":
{
"prefix": "runWithNullglob",
"description":
[
"Runs the specified command with nullglob.",
"Leaving nullglob on for the rest of a script can cause strange side effects (but I don't remember them)."
],
"body":
[
"shopt -s nullglob",
"${1:command}",
"shopt -u nullglob",
]
},
"Test if an executable is available":
{
"prefix": "testIfExecutableAvailable",
"description": "Test if the supplied executable exists in a dir contained in $PATH.",
"body":
[
"command -v ${1:executableName} >/dev/null "
]
},
// See "Prompt user to press any key (escaped)" in jsonc.json for a version for usage within a shell Task.
"Prompt user to press any key":
{
"prefix": "prompt_pressAnyKey",
"description": "Wait until any key is pressed.",
"body":
[
"echo -en \"\\n\\e[1m\\e[97mPress any key to close the window.\"; read -n 1"
]
},
"Repeat string":
{
"prefix": "repeatString",
"body":
[
"((${2:repeatTimes} > 0)) && printf \"%.0s${1:string}\" \\$(seq 1 ${2:repeatTimes})"
]
},
"Repeat string in command substitution":
{
"prefix": "repeatStringCmdSubst",
"body":
[
"$( ((${2:repeatTimes} > 0)) && printf \"%.0s${1:string}\" \\$(seq 1 ${2:repeatTimes}) )"
]
},
"Access global var with unique name via local nameref in lib (function)":
{
"prefix": "globalVarAsLocalNameref_func",
"description":
[
"",
"Insert the snippet \"Access global var with unique name via local nameref in lib (command)\"",
"into a function to create a variable."
],
"body":
[
"# Usage: eval $(globalVarAsNameref <varName>) (within a function in a sourced script)",
"#",
]
},
"Access global var with unique name via local nameref in lib (command)":
{
"prefix": "globalVarAsLocalNameref_command",
"description":
[
"The command to create a global variable with unique name that can be accessed via local nameref.",
"",
"Insert the snippet with prefix \"globalVarAsLocalNameref_source\" to use this."
],
"body":
[
"eval $(globalVarAsLocalNameref ${1:varName})"
]
},
"Access global var with unique name via local nameref in lib (source)":
{
"prefix": "globalVarAsLocalNameref_source",
"description":
[
"This sources a lib containing a function that can be used to create a global variable with unique",
"name accessible via local nameref <varName>.",
"",
"Use if you need to persist data across multiple invocations of a function or share data between",
"different functions within a lib script; just the typical use case for a variable at script level.",
"",
"The global variable will be accessible via a short, convenient name whereas its true name will be",
"_<varName>_<basename of sourcing script with - and . replaced with _, if any>,",
"so you may use the same <varName> in multiple libs sourced by the same script without collisions.",
"",
"Insert the snippet with prefix \"globalVarAsLocalNameref_command\" into a function to create a variable."
],
"body":
[
". \"$scriptsCommonInclude/libglobal-var-as-local-nameref\"",
]
},
"Quoted array elements":
{
"prefix": "arrayElementsQuoted",
"body":
[
"\"\\${${1:arrayName}[@]}\""
]
},
"Number of array elements":
{
"prefix": "arrayElementCount",
"body":
[
"\\${#${1:arrayName}[@]}"
]
},
"Get file extension":
{
"prefix": "getFileExt",
"body":
[
"\\${${1:path}##*.}",
]
},
"function error":
{
"prefix": "funcErr",
"body":
[
"# Prints an error message and exits with value 1.",
"# Arg: A string explaining the reason.",
"function error",
"{",
"\tlocal reason=\\$1",
"",
"\tlocal light_red='\\e[91m' bold='\\e[1m' norm='\\e[0m'",
"\tlocal msg=\"\\$bold\\${light_red}Error:\\$norm\\$light_red \\$reason\\$bold\"",
"",
"\tif [[ \\$reason == *. ]]; then",
"\t\tmsg+=' Exiting.'",
"\telif [[ \\$reason == *, ]]; then",
"\t\tmsg+=' exiting.'",
"\telse",
"\t\tmsg+='. Exiting.'",
"",
"\t\techo \"Warning: function error: Supplied reason doesn't end with dot or comma.\"",
"\t\techo -e \"I have appended a dot for you. Append the right punctuation mark to silence this warning.\\n\"",
"\tfi",
"",
"\techo -e \"\\$msg\\$norm\"",
"",
"\texit 1",
"}"
]
},
"Shell script doc comment quotes":
{
"prefix": "docQuotes",
"description":
[
"Inserts a pair of quotes (‘’, also used e. g. in the official Bash documentation)",
"suitable to quote code examples in a comment,",
"e. g. ‘foo='bar baz'’ or ‘echo -e \"foo\\n\"’.",
"You see, neither '' nor \"\" would be appropriate."
],
"body": "‘$1’"
},
"Multiline captures (3 captures per line, input from variable)":
{
"prefix": "multilineCaptures3Var",
"body":
[
"readarray -t captures <<<\\$(sed -En 's/${2:capRegexp}/\\1\\n\\2\\n\\3/p' <<<\\$${1:inputVarName})",
"",
"for ((i = 0; i < \\${#captures[@]}; i += 3)); do",
"\t${3:cap1}=\\${captures[i]}",
"\t${4:cap2}=\\${captures[i + 1]}",
"\t${5:cap3}=\\${captures[i + 2]}",
"",
"\t$0",
"done"
]
},
"Multiline captures (3 captures per line, input from command)":
{
"prefix": "multilineCaptures3Cmd",
"body":
[
"readarray -t captures <<<\\$(${1:command} | sed -En 's/${1:capRegexp}/\\1\\n\\2\\n\\3/p')",
"",
"for ((i = 0; i < \\${#captures[@]}; i += 3)); do",
"\t${3:cap1}=\\${captures[i]}",
"\t${4:cap2}=\\${captures[i + 1]}",
"\t${5:cap3}=\\${captures[i + 2]}",
"",
"\t$0",
"done"
]
},
"Index-based iteration over array elements":
{
"prefix": "arrayIterIndexBased",
"body":
[
"for ((i = 0; i < \\${#${1:elementVarName}s[@]}; i++)); do",
"\t${1:elementVarName}=\\${${1:elementVarName}s[i]}",
"",
"\t$0",
"done"
]
},
"function shiftArray":
{
"prefix": "funcShiftArray",
"body":
[
"# Args: <array name> <n>",
"function shiftArray",
"{",
"\tlocal -n _array_=\\$1",
"\tlocal i n=\\$2",
"",
"\tfor ((i = 0; i < ${#_array_[@]} - n; i++)); do",
"\t\tarray_[i]=${_array_[i + n]}",
"\tdone",
"",
"\tfor ((i = 0; i < n; i++)); do unset \"_array_[-1]\"; done",
"}"
]
},
"function removeArrayElement":
{
"prefix": "funcRemoveArrayElement",
"body":
[
"# Args: <arrayName> <i>",
"function removeArrayElement",
"{",
"\tlocal -n _array_=\\$1",
"",
"\tfor ((i = \\$2; i < \\${#_array_[@]} - 1; i++)); do",
"\t\t_array_[i]=${_array_[i + 1]}",
"\tdone",
"",
"\tunset _array_[-1]",
"}"
]
},
"Array element":
{
"prefix": "arrayElement",
"body":
[
"\\${${1:arrayName}[${2:subscript}]}"
]
},
"Clear current line":
{
"prefix": "clearCurrentLine",
"body":
[
"tput cr; tput el # Carriage return and clear to end of line (see man terminfo)"
]
},
}