Created
March 10, 2023 17:29
-
-
Save pr0PM/18e228ac0ec5cb85b30694cecabf553c to your computer and use it in GitHub Desktop.
bash snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// 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: | |
"shebang": { | |
"prefix": "shebang bash", | |
"body": [ | |
"#!/bin/bash", | |
"set -euo pipefail" | |
], | |
"description": "bash shebanag + set euo" | |
}, | |
// source : https://github.com/CastelloDev/bash-snippets/blob/main/snippets/snippets.code-snippets | |
// "echo": { | |
// "prefix": "e", | |
// "body": [ | |
// "echo \"${0:Hello World}\"" | |
// ], | |
// "description": "Echo" | |
// }, | |
// "function": { | |
// "prefix": "fn", | |
// "body": [ | |
// "${1:function_name}() {", | |
// "\t$0", | |
// "}" | |
// ], | |
// "description": "Function" | |
// }, | |
"function with definitions": { | |
"prefix": "fnd", | |
"body": [ | |
"#######################################", | |
"# Description", | |
"# Globals:", | |
"# - EXPORT_1 : which contains ...", | |
"# Arguments:", | |
"# - \\$1 : the first paramter (eg. param1)", | |
"# Outputs:", | |
"# Returns:", | |
"#######################################", | |
"${1:lib}::${2:function_name}() {", | |
" local -r ${3:PARAM_1}=\"\\${1:?\"${2:function_name} is missing a parameter\"}\"", | |
" $0", | |
" export ${4:EXPORT_1}", | |
"}" | |
], | |
"description": "Function in a libary with definitions of params and exports" | |
}, | |
// "if": { | |
// "prefix": "if", | |
// "body": [ | |
// "if [[ $1 ]]; then", | |
// "\t$0", | |
// "fi" | |
// ], | |
// "description": "if block" | |
// }, | |
"if else": { | |
"prefix": "ife", | |
"body": [ | |
"if [[ $1 ]]; then", | |
"\t$2", | |
"else", | |
"\t$0", | |
"fi" | |
], | |
"description": "if else block" | |
}, | |
"elif": { | |
"prefix": "elif", | |
"body": [ | |
"elif [[ $1 ]]; then", | |
"\t$0" | |
], | |
"description": "elif block" | |
}, | |
// "until": { | |
// "prefix": "until", | |
// "body": [ | |
// "until [[ $1 ]]; do", | |
// "\t$0", | |
// "done" | |
// ], | |
// "description": "until block" | |
// }, | |
"main": { | |
"prefix": "main", | |
"body": [ | |
"main() {", | |
"}", | |
"", | |
"main \"$@\"" | |
], | |
"description": "a main function declaration with passthrough of all paramters passed to the file" | |
}, | |
"const": { | |
"prefix": "const", | |
"body": [ | |
"local -r ${1:VARIABLE}=\"${0}\"" | |
], | |
"description": "creates a local readonly variable ie. a constant (this is meant for use within functions only)" | |
}, | |
"TODO": { | |
"prefix": "todo", | |
"body": [ | |
"# TODO: (${1:author}) ${0:description}" | |
], | |
"description": "creates a local readonly variable ie. a constant" | |
}, | |
// source: https://github.com/DeepInThought/vscode-shell-snippets/blob/master/snippets/tests.json | |
"test_string_empty": { | |
"prefix": "test_string_empty", | |
"body": "test -z \"$${0:VAR}\"", | |
"description": "A test to check the lengh of VAR is zero (i.e. it is empty) and returns TRUE if so." | |
}, | |
"test_string_equal": { | |
"prefix": "test_string_equal", | |
"body": "test \"$${0:VAR1}\" = \"$${1:VAR2}\"", | |
"description": "A test to compare two different STRINGS and returns TRUE if they are of equal value." | |
}, | |
"test_string_not_empty": { | |
"prefix": "test_string_not_empty", | |
"body": "test -n \"$${0:VAR}\"", | |
"description": "A test that checks the length of a STRING is greater than zero and returns TRUE if so." | |
}, | |
"test_string_not_equal": { | |
"prefix": "test_string_not_equal", | |
"body": "test \"$${0:VAR1}\" != \"$${1:VAR2}\"", | |
"description": "A test to compare two different STRINGS. Returns TRUE if they are NOT of equal value." | |
}, | |
"test_int_equal": { | |
"prefix": "test_int_equal", | |
"body": "test \"$${0:VAR1}\" -eq \"$${1:VAR2}\"", | |
"description": "A test to compare two different INTEGERS. Returns TRUE if they are of equal value." | |
}, | |
"test_int_great_equal": { | |
"prefix": "test_int_great_equal", | |
"body": "test \"$${0:VAR1}\" -ge \"$${1:VAR2}\"", | |
"description": "A test to compare two different INTEGERS. Returns TRUE if INTEGER1 is of equal or greater value than INTEGER2." | |
}, | |
"test_int_great_than": { | |
"prefix": "test_int_great_than", | |
"body": "test \"$${0:VAR1}\" -gt \"$${1:VAR2}\"", | |
"description": "A test to compare two different INTEGERS. Returns TRUE if INTEGER1 is greater than INTEGER2 in value." | |
}, | |
"test_int_less_equal": { | |
"prefix": "test_int_less_equal", | |
"body": "test \"$${0:VAR1}\" -le \"$${1:VAR2}\"", | |
"description": "A test to compare two different INTEGERS. Returns TRUE if INTEGER1 is less than or equal to INTEGER2 in value." | |
}, | |
"test_int_less_than": { | |
"prefix": "test_int_less_than", | |
"body": "test \"$${0:VAR1}\" -lt \"$${1:VAR2}\"", | |
"description": "A test to compare two different INTEGERS. Returns TRUE if INTEGER1 is less than INTEGER2 in value." | |
}, | |
"test_int_not_equal": { | |
"prefix": "test_int_not_equal", | |
"body": "test \"$${0:VAR1}\" -ne \"$${1:VAR2}", | |
"description": "A test to compare two different INTEGERS. Returns TRUE if INTEGER1 is NOT equal to INTEGER2 in value." | |
}, | |
"test_exist": { | |
"prefix": "test_exist_file", | |
"body": "test -e \"$${0:VAR}\"", | |
"description": "A test to check if FILE exists." | |
}, | |
"test_exist_file": { | |
"prefix": "test_exist_regular_file", | |
"body": "test -f \"$${0:VAR}\"", | |
"description": "A test to check if FILE exists and is a regular file." | |
}, | |
"test_exist_directory": { | |
"prefix": "test_exist_directory", | |
"body": "test -d \"$${0:VAR}\"", | |
"description": "A test to check if a FILE exists and is a directory." | |
}, | |
"test_exist_readable": { | |
"prefix": "test_exist_readable", | |
"body": "test -r \"$${0:VAR}\"", | |
"description": "A test to check if FILE exists and read permission is granted." | |
}, | |
"test_exist_writeable": { | |
"prefix": "test_exist_writeable", | |
"body": "test -w \"$${0:VAR}\"", | |
"description": "A test to check if FILE exists and write permission is granted." | |
}, | |
"test_exist_executable": { | |
"prefix": "test_exist_executable", | |
"body": "test -x \"$${0:VAR}\"", | |
"description": "A test to check if FILE exists and execute (or search) permission is granted." | |
}, | |
"test_exist_link": { | |
"prefix": "test_exist_link", | |
"body": "test -h \"$${0:VAR}\"", | |
"description": "A test to check if FILE exists and is a symbolic link (same as -L)." | |
}, | |
"test_file_equal": { | |
"prefix": "test_file_equal", | |
"body": "test \"$${0:VAR}\" -ef \"$${1:VAR}\"", | |
"description": "A test to check if FILE1 and FILE2 have the same device and inode numbers." | |
}, | |
"test_file_new_than": { | |
"prefix": "test_file_new_than", | |
"body": "test \"$${0:VAR}\" -nt \"$${1:VAR}\"", | |
"description": "A test to check if FILE1 is newer (modification date) than FILE2." | |
}, | |
"test_file_old_than": { | |
"prefix": "test_file_old_than", | |
"body": "test \"$${0:VAR}\" -ot \"$${1:VAR}\"", | |
"description": "A test to check if FILE1 is older than FILE2." | |
}, | |
// source: https://github.com/DeepInThought/vscode-shell-snippets/blob/master/snippets/shellscript.json | |
"bash": { | |
"prefix": [ | |
"bash", | |
"#!", | |
"shebang" | |
], | |
"body": "${1|#!/bin/bash,#!/usr/bin/env bash|}\n", | |
"description": [ | |
"Option 1:\n", | |
"#!/bin/bash\n", | |
"Description: Shebang Bash executor.\n", | |
"Option 2:\n", | |
"#!/usr/bin/env bash\n", | |
"Description: Shell searchs for the first match of bash in the $PATH environment variable.\n", | |
"It can be useful if you aren't aware of the absolute path or don't want to search for it.\n" | |
] | |
}, | |
"echo": { | |
"prefix": "echo", | |
"body": "echo \"${0:message}\"", | |
"description": "Echo a message." | |
}, | |
"read": { | |
"prefix": "read", | |
"body": "read -r ${0:VAR}", | |
"description": "Read input of ${VAR}." | |
}, | |
"if": { | |
"prefix": "if", | |
"body": "if [[ ${0:condition} ]]; then\n\t${1}\nfi", | |
"description": "An IF statement." | |
}, | |
"elseif": { | |
"prefix": "elseif", | |
"body": "elif [[ ${0:condition} ]]; then\n\t${1}", | |
"description": "Add an elseif to an if statement." | |
}, | |
"else": { | |
"prefix": "else", | |
"body": "else\n\t${0:command}", | |
"description": "else" | |
}, | |
"for_in": { | |
"prefix": "for_in", | |
"body": "for ${0:VAR} in $${1:LIST}\ndo\n\techo \"$${0:VAR}\"\ndone\n", | |
"description": "for loop in list" | |
}, | |
"for_i": { | |
"prefix": "for_i", | |
"body": "for ((${0:i} = 0; ${0:i} < ${1:10}; ${0:i}++)); do\n\techo \"$${0:i}\"\ndone\n", | |
"description": "An index-based iteration for loop." | |
}, | |
"while": { | |
"prefix": "while", | |
"body": "while [[ ${1:condition} ]]; do\n\t${0}\ndone\n", | |
"description": "A while loop by condition." | |
}, | |
"until": { | |
"prefix": "until", | |
"body": "until [[ ${1:condition} ]]; do\n\t${0}\ndone\n", | |
"description": "until loop by condition" | |
}, | |
"function": { | |
"prefix": "function", | |
"body": "${1:name} ()\n{\n\t${0}\n}", | |
"description": [ | |
"This defines a function named name.\n", | |
"The reserved word function is optional.\n", | |
"If the function reserved word is supplied, the parentheses are optional.\n", | |
"1. Recommended way:\n", | |
"name() {}\n", | |
"2. C-like-way:\nfunction name [()] {}" | |
] | |
}, | |
"case": { | |
"prefix": "case", | |
"body": "case \"$${0:VAR}\" in\n\t${1:1}) echo 1\n\t;;\n\t${2:2|3}) echo 2 or 3\n\t;;\n\t*) echo default\n\t;;\nesac\n", | |
"description": [ | |
"case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac\n", | |
"A case command first expands word, and tries to match it against each pattern in turn." | |
] | |
}, | |
"break": { | |
"prefix": "break", | |
"body": "break ${0}", | |
"description": [ | |
"The break command tells Bash to leave the loop straight away.\n", | |
"Enter the break or break (n) where n=number of loops." | |
] | |
}, | |
"expr": { | |
"prefix": "expr", | |
"body": "expr ${0:1 + 1}", | |
"description": "Calculate numbers with Bash." | |
}, | |
// source: https://github.com/DeepInThought/vscode-shell-snippets/blob/master/snippets/bulk-snippets.json | |
"file_read_by_line":{ | |
"prefix": "file_read_by_line", | |
"body": "{\nwhile IFS= read -r \"lineNum\"\ndo\n\techo \"\\${lineNum}\"\ndone\n} < \"${0:FILE_NAME}\"", | |
"description": "Read a text file line by line." | |
}, | |
"file_batch_rename":{ | |
"prefix": "file_batch_rename", | |
"body": "ls | xargs -i mv {} {}.${1:old}", | |
"description": "Rename all files in a directory." | |
}, | |
"awk_substr":{ | |
"prefix": "awk_substr", | |
"body": "echo \"${1:VAR_OR_STRING}\" | awk '{print substr(\\$0,0,length(\\$0)-${2|AMOUNT_TO_CUT_BY,1,2,3|}'", | |
"description": "A sub string with awk. Change AMOUNT_TO_CUT_BY to a valid integer." | |
}, | |
"awk_printf":{ | |
"prefix": "awk_printf", | |
"body": "awk -F ':' '{printf(\"filename:%10s,linenumber:%s,columns:%s,linecontent:%s\\n\",FILENAME,NR,NF,\\$0)\\}' /etc/passwd", | |
"description": "A printf example inside of awk." | |
}, | |
"awk_for_loop":{ | |
"prefix": "awk_for_loop", | |
"body": "awk -F ':' 'BEGIN {count=0;} {name[count] = \\$1;count++;\\}; END{for (i = 0; i < NR; i++) print i, name[i]\\}' /etc/passwd", | |
"description": "A loop example in awk to show all users in /etc/passwd" | |
}, | |
// todo: parse and add stuff from https://github.com/Onelinerhub/onelinerhub/tree/main/bash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment