diff --git a/.vscode/settings.json b/.vscode/settings.json index 63290c1..2bddbe7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,6 +56,7 @@ "semvers", "setaf", "shasum", + "shellcheck", "shellman", "sitm", "somefile", diff --git a/CHANGELOG.md b/CHANGELOG.md index abe03b7..fd2b1b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## 5.6.0 + +- `shellcheck` compatible (99.99%). +- Added `array print` snippet. + ## 5.5.1 - `build.go` now generates `COMMANDS.md` as well. diff --git a/COMMANDS.md b/COMMANDS.md index 3686bef..4c3f323 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -36,6 +36,8 @@ - [array length](#array-length) + - [array print , echo array](#array-print-,-echo-array) + - [array push , array add](#array-push-,-array-add) - [array slice , array range](#array-slice-,-array-range) @@ -144,8 +146,6 @@ - [remove old/new files/directories](#remove-old/new-files/directories) - - [remove old files](#remove-old-files) - ### float - [if float = , if double =](#if-float-=-,-if-double-=) @@ -258,7 +258,7 @@ - [function , func](#function-,-func) - - [function return value , func return value](#function-return-value-,-func-return-value) + - [function return value , func return value , func ret val](#function-return-value-,-func-return-value-,-func-ret-val) ### git @@ -436,8 +436,6 @@ - [argument parsing , parse args](#argument-parsing-,-parse-args) - - [echo array](#echo-array) - - [echo text , print text](#echo-text-,-print-text) - [echo variable , print variable](#echo-variable-,-print-variable) @@ -646,6 +644,10 @@ iterate array elements [↑](#Commands) length of an array [↑](#Commands) +## array print , echo array + +iterate array elements [↑](#Commands) + ## array push , array add push new item to the end of array [↑](#Commands) @@ -842,10 +844,6 @@ if path exists (file, directory, link...) [↑](#Commands) find and remove files(f)/directories(d) older(+)/newer(-) than x days [↑](#Commands) -## remove old files - -find and remove files older than x days [↑](#Commands) - ## if float = , if double = if numbers are equal [↑](#Commands) @@ -1054,7 +1052,7 @@ number of function arguments [↑](#Commands) function [↑](#Commands) -## function return value , func return value +## function return value , func return value , func ret val last function/command return code [↑](#Commands) @@ -1378,10 +1376,6 @@ define animation frame [↑](#Commands) parse command line arguments (flags/switches) [↑](#Commands) -## echo array - -print array elements [↑](#Commands) - ## echo text , print text print text, variable or both [↑](#Commands) diff --git a/README.md b/README.md index 070fe5d..7e4fbdf 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,7 @@ Sometimes doing a job takes more than a `snippet`. `Shellman` has ready to use f ### Latest release Notes -## 5.5.1 +## 5.6.0 -- `build.go` now generates `COMMANDS.md` as well. -- Fixed #34 (Argument parsing for switches may result in infinite loop). -- Numerous bug fixed in `fn`s. +- `shellcheck` compatible (99.99%). +- Added `array print` snippet. diff --git a/nsroot/array/contains.json b/nsroot/array/contains.json index 0399472..9a0c696 100644 --- a/nsroot/array/contains.json +++ b/nsroot/array/contains.json @@ -1,7 +1,7 @@ { "prefix": "array contains", "body": [ - "if [[ \"\\${${1:myArray}[@]}\" =~ ${2|'element',\"${value}\"|} ]]; then", + "if [[ \"\\${${1:myArray}[*]}\" =~ ${2|'element',\"${value}\"|} ]]; then", "\techo 'array contains element'", "fi\n" ], diff --git a/nsroot/array/delete-at.json b/nsroot/array/delete-at.json index 4f2c4e7..4b0ee19 100644 --- a/nsroot/array/delete-at.json +++ b/nsroot/array/delete-at.json @@ -1,5 +1,5 @@ { "prefix": "array delete at", - "body": "unset ${1:myArray}[${2:index}]\n", + "body": "unset \"${1:myArray}[${2:index}]\"\n", "description": "delete element at index from array" } \ No newline at end of file diff --git a/nsroot/array/filter.json b/nsroot/array/filter.json index 916837a..a820532 100644 --- a/nsroot/array/filter.json +++ b/nsroot/array/filter.json @@ -1,5 +1,5 @@ { "prefix": "array filter", - "body": "${1:filtered}=(\\$(for i in ${${2:myArray}[@]} ; do echo \\${i\\}; done | grep ${3|',\"|}${4:pattern}${3}))\n", + "body": "readarray -t ${1:filtered} < <(for i in \"${${2:myArray}[@]}\" ; do echo \"\\${i\\}\"; done | grep ${3|',\"|}${4:pattern}${3})\n", "description": "filter elements of an array based on given grep pattern" } \ No newline at end of file diff --git a/nsroot/array/print.json b/nsroot/array/print.json new file mode 100644 index 0000000..8f3b440 --- /dev/null +++ b/nsroot/array/print.json @@ -0,0 +1,8 @@ +{ + "prefix": [ + "array print", + "echo array" + ], + "body": "echo \"\\${${1:myArray}[@]}\"\n", + "description": "iterate array elements" +} \ No newline at end of file diff --git a/nsroot/array/range.json b/nsroot/array/range.json index e7b2789..8f63542 100644 --- a/nsroot/array/range.json +++ b/nsroot/array/range.json @@ -3,6 +3,6 @@ "array slice", "array range" ], - "body": "${1:newArray}=\"${${2:myArray}[@]:${3:fromIndex}:${4:n}}\"\n", + "body": "${1:newArray}=\"${${2:myArray}[*]:${3:fromIndex}:${4:n}}\"\n", "description": "n elements of an array from specified index (zero based)" } \ No newline at end of file diff --git a/nsroot/array/replace.json b/nsroot/array/replace.json index f60de3f..c7f77de 100644 --- a/nsroot/array/replace.json +++ b/nsroot/array/replace.json @@ -1,5 +1,5 @@ { "prefix": "array replace", - "body": "${1:newArray}=${${2:myArray}[@]//${3:find}/${4:replace}}\n", + "body": "${1:newArray}=${${2:myArray}[*]//${3:find}/${4:replace}}\n", "description": "find and replace elements in array using regex" } \ No newline at end of file diff --git a/nsroot/array/reverse.json b/nsroot/array/reverse.json index 079f51e..19f8658 100644 --- a/nsroot/array/reverse.json +++ b/nsroot/array/reverse.json @@ -1,5 +1,13 @@ { "prefix": "array reverse", - "body": "${1:reversedArray}=( \\$(echo \"\\${${2:array}[@]}\" | rev) )\n", + "body": [ + "for((i=\\${#${1:myArray}[@]}-1;i>=0;i--)); do", + "\t${2:reversed}+=(\"\\${${1:myArray}[i]}\")", + "done", + "", + "unset \"${1:myArray}\" # optional", + "echo \"\\${${2:reversed}[@]}\"", + "" + ], "description": "reverse order of array elements" } \ No newline at end of file diff --git a/nsroot/command/failure-check.json b/nsroot/command/failure-check.json index 5682b61..4717409 100644 --- a/nsroot/command/failure-check.json +++ b/nsroot/command/failure-check.json @@ -4,8 +4,10 @@ "cmd failure check" ], "body": [ - "if (( \\$? != 0 )); then", - "\t${1:echo \"Last command failed\"}", + "if ! ${1:command} >/dev/null 2>&1; then", + "\techo \"failed\"", + "else", + "\techo \"succeed\"", "fi\n" ], "description": "check if last command failed" diff --git a/nsroot/command/if-exists.json b/nsroot/command/if-exists.json index f4e5eb0..670f8ee 100644 --- a/nsroot/command/if-exists.json +++ b/nsroot/command/if-exists.json @@ -4,7 +4,7 @@ "if cmd exists" ], "body": [ - "if [ \\$(command -v ${1:command}) ]; then", + "if [ \"\\$(command -v ${1:command})\" ]; then", "\t${2:echo \"command \\\"${1:command}\\\" exists on system\"}", "fi\n" ], diff --git a/nsroot/command/success-check.json b/nsroot/command/success-check.json index 7d37cb1..ff68e5d 100644 --- a/nsroot/command/success-check.json +++ b/nsroot/command/success-check.json @@ -4,8 +4,10 @@ "cmd success check" ], "body": [ - "if (( \\$? == 0 )); then", - "\t${1:echo \"Last command succeed\"}", + "if ${1:command} >/dev/null 2>&1; then", + "\techo \"succeed\"", + "else", + "\techo \"failed\"", "fi\n" ], "description": "check if last command succeed" diff --git a/nsroot/filesystem/file-read.json b/nsroot/filesystem/file-read.json index b667e26..e1972d7 100644 --- a/nsroot/filesystem/file-read.json +++ b/nsroot/filesystem/file-read.json @@ -1,9 +1,9 @@ { "prefix": "file read", "body": [ - "cat \"${1|/path/to/file,${filePath}|}\" | while read ${2:line}; do", - "\t${3:echo \"\\${${2:line}\\}\"}", - "done\n" + "while IFS= read -r line; do", + "\techo \"\\$line\"", + "done < ${1|/path/ro/file,\"${inputFile}\"|}\n" ], "description": "read a file" } \ No newline at end of file diff --git a/nsroot/filesystem/file-write.json b/nsroot/filesystem/file-write.json index 0dc0ada..aa3936e 100644 --- a/nsroot/filesystem/file-write.json +++ b/nsroot/filesystem/file-write.json @@ -2,8 +2,9 @@ "prefix": "file write", "body": [ "echo \"${1:sample header}\" > \"${2|/path/to/file,${pathToFile}|}\"", - "for ${3:line} in \\${${4:lines}[@]}; do", - "\techo \"\\${${3:line}\\}\" >> \"${2}\"", + "echo # Empty line", + "for ${3:line} in \"\\${${4:lines}[@]}\"; do", + "\techo \"\\${${3:line}\\}\" >> \"${2}\" # >> means append", "done\n" ], "description": "write to a file" diff --git a/nsroot/filesystem/files-iterate.json b/nsroot/filesystem/files-iterate.json index 29c4c97..b5b3836 100644 --- a/nsroot/filesystem/files-iterate.json +++ b/nsroot/filesystem/files-iterate.json @@ -1,8 +1,8 @@ { "prefix": "iterate files", "body": [ - "# Make sure path ends with /", - "for file in ${1|'/path/to/files/',\"${pathToFiles}\"|}*.{jpg,png\\}; do", + "# Searching in the current directory", + "for file in ${1|*,*.{jpg\\,png}|}; do", "\t${2:echo \"\\${file\\}\"}", "done\n" ], diff --git a/nsroot/filesystem/find-files-or-directories.json b/nsroot/filesystem/find-files-or-directories.json index bf87d9d..1edf9f0 100644 --- a/nsroot/filesystem/find-files-or-directories.json +++ b/nsroot/filesystem/find-files-or-directories.json @@ -3,6 +3,6 @@ "file find", "directory find" ], - "body": "${1:resultArray}=(\\$(find \"${2|/path/to/search,${pathToSearch}|}\" -maxdepth ${3|1,2,3,4,5,6,7,8,9|} -type ${4|f,d|} -name \"${5:criteria}\"))\n", + "body": "readarray -t resultArray < <(find \"/path/to/search\" -maxdepth 1 -type f -name \"criteria\")\n", "description": "find files (-type f) or directories (-type d) by name or pattern (*.jpg)" } \ No newline at end of file diff --git a/nsroot/filesystem/remove-files-newer-than.json b/nsroot/filesystem/remove-files-older-newer-than.json similarity index 76% rename from nsroot/filesystem/remove-files-newer-than.json rename to nsroot/filesystem/remove-files-older-newer-than.json index 4c46922..b84aaf9 100644 --- a/nsroot/filesystem/remove-files-newer-than.json +++ b/nsroot/filesystem/remove-files-older-newer-than.json @@ -1,5 +1,5 @@ { "prefix": "remove old/new files/directories", - "body": "find \"${1|/path/to/directory/,${pathToDirectory}|}\"* -type ${2|f,d|} -mtime ${3|-,+|}${4:days} | xargs rm -f\n", + "body": "find \"${1|/path/to/directory/,${pathToDirectory}|}\"* -type ${2|f,d|} -mtime ${3|-,+|}${4:days} -delete\n", "description": "find and remove files(f)/directories(d) older(+)/newer(-) than x days" } \ No newline at end of file diff --git a/nsroot/filesystem/remove-files-older-than.json b/nsroot/filesystem/remove-files-older-than.json deleted file mode 100644 index 09ec921..0000000 --- a/nsroot/filesystem/remove-files-older-than.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "prefix": "remove old files", - "body": "find \"${1|/path/to/directory,${pathToDirectory}|}\" -mtime +${2:days} | xargs rm -f\n", - "description": "find and remove files older than x days" -} \ No newline at end of file diff --git a/nsroot/fn-fx/fn-math-product.json b/nsroot/fn-fx/fn-math-product.json index 74917a9..4ad72e3 100644 --- a/nsroot/fn-fx/fn-math-product.json +++ b/nsroot/fn-fx/fn-math-product.json @@ -4,10 +4,10 @@ "# Usage: product int1 int2 ...", "function product () {", "\tlocal result=1", - "\tfor int in $@; do", + "\tfor int in \"$@\"; do", "\t\t((result *= int))", "\tdone", - "\techo \\${result\\}", + "\techo \"\\${result\\}\"", "}\n" ], "description": "calculate product of given integers" diff --git a/nsroot/fn-fx/fn-math-sum.json b/nsroot/fn-fx/fn-math-sum.json index 20f6831..da5c02f 100644 --- a/nsroot/fn-fx/fn-math-sum.json +++ b/nsroot/fn-fx/fn-math-sum.json @@ -4,10 +4,10 @@ "# Usage: sum int1 int2 ...", "function sum () {", "\tlocal result=0", - "\tfor int in \\$@; do", + "\tfor int in \"\\$@\"; do", "\t\t((result += int))", "\tdone", - "\techo \\${result\\}", + "\techo \"\\${result\\}\"", "}\n" ], "description": "calculate sum of given integers" diff --git a/nsroot/fn-fx/fn-progress.json b/nsroot/fn-fx/fn-progress.json index c2f27f2..d91c611 100644 --- a/nsroot/fn-fx/fn-progress.json +++ b/nsroot/fn-fx/fn-progress.json @@ -4,7 +4,7 @@ "# Usage: progressBar \"message\" currentStep totalSteps", "function progressBar() {", "\tlocal bar='████████████████████'", - "\tlocal space=' '", + "\tlocal space='....................'", "\tlocal wheel=('\\' '|' '/' '-')", "", "\tlocal msg=\"\\${1\\}\"", @@ -14,7 +14,7 @@ "\tlocal position=\\$((100 * current / total))", "\tlocal barPosition=\\$((position / 5))", "", - "\techo -ne \"\\r|\\${bar:0:\\$barPosition}\\${space:\\$barPosition:20}| \\${wheel[wheelIndex]} \\${position}% [ \\${msg} ] \"", + "\techo -ne \"\\r|\\${bar:0:\\$barPosition}\\$(tput dim)\\${space:\\$barPosition:20}\\$(tput sgr0)| \\${wheel[wheelIndex]} \\${position}% [ \\${msg} ] \"", "}\n" ], "description": "progress bar function" diff --git a/nsroot/fn-fx/fn-scan.json b/nsroot/fn-fx/fn-scan.json index a2238e0..5d0d690 100644 --- a/nsroot/fn-fx/fn-scan.json +++ b/nsroot/fn-fx/fn-scan.json @@ -5,7 +5,7 @@ "function scan () {", "\tlocal openPortsArray=()", "\tfor ((port=\\${3\\}; port<=\\${4\\}; port++)); do", - "\t\t(echo >/dev/\\${1\\}/\\${2\\}/\\${port\\}) >/dev/null 2>&1 && openPortsArray+=(\"\\${port}\")", + "\t\t(echo >/dev/\"\\${1\\}\"/\"\\${2\\}\"/\"\\${port\\}\") >/dev/null 2>&1 && openPortsArray+=(\"\\${port}\")", "\tdone", "\techo \"\\${openPortsArray[@]}\"", "}\n" diff --git a/nsroot/fn-fx/fn-time-format-seconds.json b/nsroot/fn-fx/fn-time-format-seconds.json index 16223f2..08d4ee8 100644 --- a/nsroot/fn-fx/fn-time-format-seconds.json +++ b/nsroot/fn-fx/fn-time-format-seconds.json @@ -11,10 +11,10 @@ "\tlocal S=\\$((T%60))", "\tlocal result=\"\"", "", - "\t(( \\$D > 0 )) && result=\"\\${D}d \"", - "\t(( \\$H > 0 )) && result=\"\\${result}\\${H}h \"", - "\t(( \\$M > 0 )) && result=\"\\${result}\\${M}m \"", - "\t(( \\$S > 0 )) && result=\"\\${result}\\${S}s \"", + "\t(( D > 0 )) && result=\"\\${D}d \"", + "\t(( H > 0 )) && result=\"\\${result}\\${H}h \"", + "\t(( M > 0 )) && result=\"\\${result}\\${M}m \"", + "\t(( S > 0 )) && result=\"\\${result}\\${S}s \"", "\techo -e \"\\${result}\" | sed -e 's/[[:space:]]*\\$//'", "}\n" ], diff --git a/nsroot/fn-fx/fn-urlencode.json b/nsroot/fn-fx/fn-urlencode.json index 29563b1..ee6db4c 100644 --- a/nsroot/fn-fx/fn-urlencode.json +++ b/nsroot/fn-fx/fn-urlencode.json @@ -8,7 +8,7 @@ "\tfor (( i = 0; i < length; i++ )); do", "\t\tlocal c=\"\\${1:i:1}\"", "\t\tcase \"\\${c\\}\" in", - "\t\t\t[a-zA-Z0-9.~_-]) printf \"\\${c\\}\" ;;", + "\t\t\t[a-zA-Z0-9.~_-]) printf \"%s\" \"\\${c\\}\" ;;", "\t\t\t*) printf '%%%02X' \"'\\${c\\}\" ;;", "\t\tesac", "\tdone", diff --git a/nsroot/fn-fx/fn-version-compare.json b/nsroot/fn-fx/fn-version-compare.json index f8a7b47..d7e3a08 100644 --- a/nsroot/fn-fx/fn-version-compare.json +++ b/nsroot/fn-fx/fn-version-compare.json @@ -7,8 +7,7 @@ "# Usage: versionCompare \"1.2.3\" \"1.1.7\"", "function versionCompare () {", "\tfunction subVersion () {", - "\t\ttemp=${1%%\".\"*} && indexOf=\\$(echo ${1%%\".\"*} | echo ${#temp})", - "\t\techo -e \"\\${1:0:indexOf}\"", + "\t\techo -e \"\\${1%%\".\"*}\"", "\t}", "\tfunction cutDot () {", "\t\tlocal offset=${#1}", @@ -18,16 +17,20 @@ "\tif [ -z \"\\${1\\}\" ] || [ -z \"\\${2\\}\" ]; then", "\t\techo \"=\" && exit 0", "\tfi", - "\tlocal v1=\\$(echo -e \"\\${1}\" | tr -d '[[:space:]]')", - "\tlocal v2=\\$(echo -e \"\\${2}\" | tr -d '[[:space:]]')", - "\tlocal v1Sub=\\$(subVersion \\$v1)", - "\tlocal v2Sub=\\$(subVersion \\$v2)", + "\tlocal v1", + "\tv1=\\$(echo -e \"\\${1}\" | tr -d '[:space:]')", + "\tlocal v2", + "\tv2=\\$(echo -e \"\\${2}\" | tr -d '[:space:]')", + "\tlocal v1Sub", + "\tv1Sub=\\$(subVersion \"\\$v1\")", + "\tlocal v2Sub", + "\tv2Sub=\\$(subVersion \"\\$v2\")", "\tif (( v1Sub > v2Sub )); then", "\t\techo \">\"", "\telif (( v1Sub < v2Sub )); then", "\t\techo \"<\"", "\telse", - "\t\tversionCompare \\$(cutDot \\$v1Sub \\$v1) \\$(cutDot \\$v2Sub \\$v2)", + "\t\tversionCompare \"\\$(cutDot \"\\$v1Sub\" \"\\$v1\")\" \"\\$(cutDot \"\\$v2Sub\" \"\\$v2\")\"", "\tfi", "}\n" ], diff --git a/nsroot/fn-fx/fx-progress.json b/nsroot/fn-fx/fx-progress.json index 8d42a7b..c5020d9 100644 --- a/nsroot/fn-fx/fx-progress.json +++ b/nsroot/fn-fx/fx-progress.json @@ -6,7 +6,7 @@ "", "for ((${3:currentStep}=${4:1}; ${3} <= ${1}; ${3}++)); do", "\tsleep 0.1 # simulating one step of job", - "\tprogressBar \"Installing foo...\" \\${${3}\\} \\${${1}\\}", + "\tprogressBar \"Installing foo...\" \"\\${${3}\\}\" \"\\${${1}\\}\"", "done\n", "echo\n" ], diff --git a/nsroot/fn-fx/fx-scan.json b/nsroot/fn-fx/fx-scan.json index a75aa00..128d618 100644 --- a/nsroot/fn-fx/fx-scan.json +++ b/nsroot/fn-fx/fx-scan.json @@ -2,7 +2,7 @@ "prefix": "fx scan local", "body": [ "# Usage: scan proto host fromPort toPort", - "openPorts=( \\$(scan ${1|tcp,udp|} \"${2|localhost,127.0.0.1,::1|}\" ${3:fromPort} ${4:toPort}) )", + "readarray -t openPorts < <(scan ${1|tcp,udp|} \"${2|localhost,127.0.0.1,::1|}\" ${3:fromPort} ${4:toPort})", "${5:echo \"\\${openPorts[@]\\}\"}\n" ], "description": "call scan function to scan localhost over a port range" diff --git a/nsroot/fn-fx/fx-time-format-seconds.json b/nsroot/fn-fx/fx-time-format-seconds.json index aa38041..7d91b51 100644 --- a/nsroot/fn-fx/fx-time-format-seconds.json +++ b/nsroot/fn-fx/fx-time-format-seconds.json @@ -2,8 +2,8 @@ "prefix": "fx time format seconds", "body": [ "# Usage: formatSeconds 70 -> 1m 10s", - "${1:result}=($(formatSeconds ${2:seconds}))", - "echo ${${1}[@]}\n" + "readarray -t ${1:result} < <(formatSeconds ${2:seconds})", + "echo \"${${1}[@]}\"\n" ], "description": "call formatSeconds function" } \ No newline at end of file diff --git a/nsroot/function/return-value.json b/nsroot/function/return-value.json index 7033abd..9dfb216 100644 --- a/nsroot/function/return-value.json +++ b/nsroot/function/return-value.json @@ -1,7 +1,8 @@ { "prefix": [ "function return value", - "func return value" + "func return value", + "func ret val" ], "body": "echo \"$?\"", "description": "last function/command return code" diff --git a/nsroot/http/cookie.json b/nsroot/http/cookie.json index 6da10c5..96351ee 100644 --- a/nsroot/http/cookie.json +++ b/nsroot/http/cookie.json @@ -2,7 +2,7 @@ "prefix": "http cookie", "body": [ "curl --request ${1|GET,POST,DELETE,PUT|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--cookie ${3|'key=value',\"${key}=${value}\"|} \\", "\t--url ${4|'http://example.com',\"${url}\"|}\n" ], diff --git a/nsroot/http/download.json b/nsroot/http/download.json index ca8d9c2..042cd24 100644 --- a/nsroot/http/download.json +++ b/nsroot/http/download.json @@ -2,7 +2,7 @@ "prefix": "http download", "body": [ "curl --request ${1|GET,POST|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--output ${3|'/path/to/file',\"${pathToFile}\"|} \\", "\t--url ${4|'http://example.com/file.zip',\"${downloadUrl}\"|}\n" ], diff --git a/nsroot/http/get.json b/nsroot/http/get.json index 2fcf0b3..0269a56 100644 --- a/nsroot/http/get.json +++ b/nsroot/http/get.json @@ -5,7 +5,7 @@ ], "body": [ "curl --request ${1|GET,DELETE|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--url ${3|'http://example.com',\"${url}\"|}\n" ], "description": "send http GET/DELETE request using curl" diff --git a/nsroot/http/header.json b/nsroot/http/header.json index 7773b6e..1dddfbc 100644 --- a/nsroot/http/header.json +++ b/nsroot/http/header.json @@ -2,7 +2,7 @@ "prefix": "http header", "body": [ "curl --request ${1|GET,POST,DELETE,PUT|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--header ${3|'key: value',\"${key}\": \"${value}\"|} \\", "\t--url ${4|'http://example.com',\"${url}\"|}\n" ], diff --git a/nsroot/http/post-send-file.json b/nsroot/http/post-send-file.json index 3f26d91..3e35f90 100644 --- a/nsroot/http/post-send-file.json +++ b/nsroot/http/post-send-file.json @@ -2,7 +2,7 @@ "prefix": "http POST file", "body": [ "curl --request POST -sL \\", - "\t--user-agent '${1|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${1|Shellman,Your custom user agent|}' \\", "\t--url ${2|'http://example.com',\"${url}\"|} \\", "\t--form ${3|'key=value',\"${key}\"=\"${value}\"|} \\", "\t--form ${4|'file=@/path/to/file',file=@\"${pathToFile}\"|}\n" diff --git a/nsroot/http/post.json b/nsroot/http/post.json index 806202c..bd5535f 100644 --- a/nsroot/http/post.json +++ b/nsroot/http/post.json @@ -5,7 +5,7 @@ ], "body": [ "curl --request ${1|POST,PUT|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--url ${3|'http://example.com',\"${url}\"|} \\", "\t--data ${4|'key=value',\"${key}\"=\"${value}\"|} \n" ], diff --git a/nsroot/input/password.json b/nsroot/input/password.json index 5848c70..48aef76 100644 --- a/nsroot/input/password.json +++ b/nsroot/input/password.json @@ -2,7 +2,7 @@ "prefix": "input password", "body": [ "echo \"${1:Please enter your password: }\"", - "read -s ${2:password}", + "read -rs ${2:password}", "${3:echo \"\\${password\\}\"}\n" ], "description": "get text as input from user without showing characters" diff --git a/nsroot/input/text.json b/nsroot/input/text.json index 42988e9..c3ceb7b 100644 --- a/nsroot/input/text.json +++ b/nsroot/input/text.json @@ -4,7 +4,7 @@ "ask question" ], "body": [ - "read -ep \"${1:Question here? }\" -i \"${2:Default answer}\" answer", + "read -rep \"${1:Question here? }\" -i \"${2:Default answer}\" answer", "${3:echo \"\\${answer\\}\"}\n" ], "description": "get text as input from user" diff --git a/nsroot/misc/echo-array.json b/nsroot/misc/echo-array.json deleted file mode 100644 index 3e2e58a..0000000 --- a/nsroot/misc/echo-array.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "prefix": "echo array", - "body": "echo \\${${1:myArray}[@]}\n", - "description": "print array elements" -} \ No newline at end of file diff --git a/nsroot/misc/echo-variable.json b/nsroot/misc/echo-variable.json index 5657ee1..1895318 100644 --- a/nsroot/misc/echo-variable.json +++ b/nsroot/misc/echo-variable.json @@ -3,6 +3,6 @@ "echo variable", "print variable" ], - "body": "echo \"\\${${1|result,variable,X|}\\}\"\n", + "body": "echo \"\\${${1|result,variable|}\\}\"\n", "description": "print text, variable or both" } \ No newline at end of file diff --git a/nsroot/process/process-id.json b/nsroot/process/process-id.json index 42b05bb..3b3d4cf 100644 --- a/nsroot/process/process-id.json +++ b/nsroot/process/process-id.json @@ -1,7 +1,7 @@ { "prefix": "process ID(s)", "body": [ - "${1:processIDsArray}=(\\$(pgrep ${2:processName}))", + "readarray -t ${1:processIDsArray} < <(pgrep ${2|processName, \"${processName}\"|})", "${3:echo \"\\${${1}[@]\\}\"}\n" ], "description": "find process ID(s) aka PIDs" diff --git a/nsroot/process/process-instances.json b/nsroot/process/process-instances.json index 6724e83..8a0b943 100644 --- a/nsroot/process/process-instances.json +++ b/nsroot/process/process-instances.json @@ -1,5 +1,5 @@ { "prefix": "process instances", - "body": "${1:processInstances}=\\$(ps -A | grep \"\\${${2:processName}\\}\")\n", + "body": "${1:processInstances}=\\$(pgrep -l \"\\${${2:processName}\\}\")\n", "description": "list processes" } \ No newline at end of file diff --git a/nsroot/process/process-kill.json b/nsroot/process/process-kill.json index 1a5a85f..74959d1 100644 --- a/nsroot/process/process-kill.json +++ b/nsroot/process/process-kill.json @@ -1,5 +1,5 @@ { "prefix": "process kill", - "body": "sudo kill -9 \\$(pgrep ${1:processName})\n", + "body": "sudo kill -9 \"\\$(pgrep ${1:processName})\"\n", "description": "kill process by name" } \ No newline at end of file diff --git a/nsroot/string/trim-all.json b/nsroot/string/trim-all.json index b9bbb5c..6d97586 100644 --- a/nsroot/string/trim-all.json +++ b/nsroot/string/trim-all.json @@ -1,5 +1,5 @@ { "prefix": "string trim all", - "body": "${1:trimmed}=\\$(echo -e \"\\${${2:string}}\" | tr -d '[[:space:]]')\n", + "body": "${1:trimmed}=\\$(echo -e \"\\${${2:string}}\" | tr -d '[:space:]')\n", "description": "remove all white space(s)" } diff --git a/nsroot/system/memory-info.json b/nsroot/system/memory-info.json index bf2c1d9..f11d02e 100644 --- a/nsroot/system/memory-info.json +++ b/nsroot/system/memory-info.json @@ -1,7 +1,7 @@ { "prefix": "system memory info", "body": [ - "sysMemory${1|MemTotal,MemFree,MemAvailable,Cached,Buffers,Active,Inactive,SwapTotal,SwapFree,SwapCached|}=\\$(cat /proc/meminfo | grep '${1}' | awk '{print \\$2}' | head -n 1)", + "sysMemory${1|MemTotal,MemFree,MemAvailable,Cached,Buffers,Active,Inactive,SwapTotal,SwapFree,SwapCached|}=\\$(grep '${1}' < /proc/meminfo | awk '{print \\$2}' | head -n 1)", "${2:echo \"\\${sysMemory${1}\\}\"}\n" ], "description": "system memory information in kilobytes (KB)" diff --git a/nsroot/system/service-manage.json b/nsroot/system/service-manage.json index 0f4437a..b12268c 100644 --- a/nsroot/system/service-manage.json +++ b/nsroot/system/service-manage.json @@ -3,6 +3,6 @@ "service manage", "systemd manage" ], - "body": "sudo systemctl ${1|enable,disable,start,stop,reload,restart,status|} ${2|'serviceName',\"${serviceName}\"|}\n", + "body": "sudo systemctl ${1|enable,enable --now,disable,disable --now,start,stop,reload,restart,status|} ${2|'serviceName',\"${serviceName}\"|} # use '--user' for user services\n", "description": "manage service operations" } \ No newline at end of file diff --git a/nsroot/time/time-utc.json b/nsroot/time/time-utc.json index df66f88..2d74820 100644 --- a/nsroot/time/time-utc.json +++ b/nsroot/time/time-utc.json @@ -2,7 +2,7 @@ "prefix": "time now UTC", "body": [ "${1:timeNowUTC}=\\$(date -u +%R)", - "${2:echo \\${${1}\\}}\n" + "${2:echo \"\\${${1}\\}}\"\n" ], "description": "current UTC time" } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 6a0bfac..dae6ca3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "shellman", - "version": "5.5.1", + "version": "5.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "5.5.1", + "version": "5.6.0", "license": "SEE LICENSE IN LICENSE.md", "dependencies": { "ovsx": "0.2.0", diff --git a/package.json b/package.json index 8cc6969..337d455 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "shellman", "displayName": "shellman", "description": "Shell script snippet", - "version": "5.5.1", + "version": "5.6.0", "publisher": "Remisa", "icon": "images/icon.png", "license": "SEE LICENSE IN LICENSE.md", diff --git a/snippets/snippets.json b/snippets/snippets.json index fc07da8..b41a328 100755 --- a/snippets/snippets.json +++ b/snippets/snippets.json @@ -65,7 +65,7 @@ "array.contains": { "prefix": "array contains", "body": [ - "if [[ \"\\${${1:myArray}[@]}\" =~ ${2|'element',\"${value}\"|} ]]; then", + "if [[ \"\\${${1:myArray}[*]}\" =~ ${2|'element',\"${value}\"|} ]]; then", "\techo 'array contains element'", "fi\n" ], @@ -84,7 +84,7 @@ }, "array.delete-at": { "prefix": "array delete at", - "body": "unset ${1:myArray}[${2:index}]\n", + "body": "unset \"${1:myArray}[${2:index}]\"\n", "description": "delete element at index from array" }, "array.delete": { @@ -94,7 +94,7 @@ }, "array.filter": { "prefix": "array filter", - "body": "${1:filtered}=(\\$(for i in ${${2:myArray}[@]} ; do echo \\${i\\}; done | grep ${3|',\"|}${4:pattern}${3}))\n", + "body": "readarray -t ${1:filtered} < <(for i in \"${${2:myArray}[@]}\" ; do echo \"\\${i\\}\"; done | grep ${3|',\"|}${4:pattern}${3})\n", "description": "filter elements of an array based on given grep pattern" }, "array.iterate": { @@ -114,6 +114,14 @@ "body": "${1:length}=${#${2:myArray}[@]}\n", "description": "length of an array" }, + "array.print": { + "prefix": [ + "array print", + "echo array" + ], + "body": "echo \"\\${${1:myArray}[@]}\"\n", + "description": "iterate array elements" + }, "array.push": { "prefix": [ "array push", @@ -127,17 +135,25 @@ "array slice", "array range" ], - "body": "${1:newArray}=\"${${2:myArray}[@]:${3:fromIndex}:${4:n}}\"\n", + "body": "${1:newArray}=\"${${2:myArray}[*]:${3:fromIndex}:${4:n}}\"\n", "description": "n elements of an array from specified index (zero based)" }, "array.replace": { "prefix": "array replace", - "body": "${1:newArray}=${${2:myArray}[@]//${3:find}/${4:replace}}\n", + "body": "${1:newArray}=${${2:myArray}[*]//${3:find}/${4:replace}}\n", "description": "find and replace elements in array using regex" }, "array.reverse": { "prefix": "array reverse", - "body": "${1:reversedArray}=( \\$(echo \"\\${${2:array}[@]}\" | rev) )\n", + "body": [ + "for((i=\\${#${1:myArray}[@]}-1;i>=0;i--)); do", + "\t${2:reversed}+=(\"\\${${1:myArray}[i]}\")", + "done", + "", + "unset \"${1:myArray}\" # optional", + "echo \"\\${${2:reversed}[@]}\"", + "" + ], "description": "reverse order of array elements" }, "array.set-element-at": { @@ -151,8 +167,10 @@ "cmd failure check" ], "body": [ - "if (( \\$? != 0 )); then", - "\t${1:echo \"Last command failed\"}", + "if ! ${1:command} >/dev/null 2>&1; then", + "\techo \"failed\"", + "else", + "\techo \"succeed\"", "fi\n" ], "description": "check if last command failed" @@ -171,7 +189,7 @@ "if cmd exists" ], "body": [ - "if [ \\$(command -v ${1:command}) ]; then", + "if [ \"\\$(command -v ${1:command})\" ]; then", "\t${2:echo \"command \\\"${1:command}\\\" exists on system\"}", "fi\n" ], @@ -209,8 +227,10 @@ "cmd success check" ], "body": [ - "if (( \\$? == 0 )); then", - "\t${1:echo \"Last command succeed\"}", + "if ${1:command} >/dev/null 2>&1; then", + "\techo \"succeed\"", + "else", + "\techo \"failed\"", "fi\n" ], "description": "check if last command succeed" @@ -347,9 +367,9 @@ "filesystem.file-read": { "prefix": "file read", "body": [ - "cat \"${1|/path/to/file,${filePath}|}\" | while read ${2:line}; do", - "\t${3:echo \"\\${${2:line}\\}\"}", - "done\n" + "while IFS= read -r line; do", + "\techo \"\\$line\"", + "done < ${1|/path/ro/file,\"${inputFile}\"|}\n" ], "description": "read a file" }, @@ -384,8 +404,9 @@ "prefix": "file write", "body": [ "echo \"${1:sample header}\" > \"${2|/path/to/file,${pathToFile}|}\"", - "for ${3:line} in \\${${4:lines}[@]}; do", - "\techo \"\\${${3:line}\\}\" >> \"${2}\"", + "echo # Empty line", + "for ${3:line} in \"\\${${4:lines}[@]}\"; do", + "\techo \"\\${${3:line}\\}\" >> \"${2}\" # >> means append", "done\n" ], "description": "write to a file" @@ -393,8 +414,8 @@ "filesystem.files-iterate": { "prefix": "iterate files", "body": [ - "# Make sure path ends with /", - "for file in ${1|'/path/to/files/',\"${pathToFiles}\"|}*.{jpg,png\\}; do", + "# Searching in the current directory", + "for file in ${1|*,*.{jpg\\,png}|}; do", "\t${2:echo \"\\${file\\}\"}", "done\n" ], @@ -405,7 +426,7 @@ "file find", "directory find" ], - "body": "${1:resultArray}=(\\$(find \"${2|/path/to/search,${pathToSearch}|}\" -maxdepth ${3|1,2,3,4,5,6,7,8,9|} -type ${4|f,d|} -name \"${5:criteria}\"))\n", + "body": "readarray -t resultArray < <(find \"/path/to/search\" -maxdepth 1 -type f -name \"criteria\")\n", "description": "find files (-type f) or directories (-type d) by name or pattern (*.jpg)" }, "filesystem.if-directory-exists": { @@ -507,16 +528,11 @@ ], "description": "if path exists (file, directory, link...)" }, - "filesystem.remove-files-newer-than": { + "filesystem.remove-files-older-newer-than": { "prefix": "remove old/new files/directories", - "body": "find \"${1|/path/to/directory/,${pathToDirectory}|}\"* -type ${2|f,d|} -mtime ${3|-,+|}${4:days} | xargs rm -f\n", + "body": "find \"${1|/path/to/directory/,${pathToDirectory}|}\"* -type ${2|f,d|} -mtime ${3|-,+|}${4:days} -delete\n", "description": "find and remove files(f)/directories(d) older(+)/newer(-) than x days" }, - "filesystem.remove-files-older-than": { - "prefix": "remove old files", - "body": "find \"${1|/path/to/directory,${pathToDirectory}|}\" -mtime +${2:days} | xargs rm -f\n", - "description": "find and remove files older than x days" - }, "float.if-equal": { "prefix": [ "if float =", @@ -994,10 +1010,10 @@ "# Usage: product int1 int2 ...", "function product () {", "\tlocal result=1", - "\tfor int in $@; do", + "\tfor int in \"$@\"; do", "\t\t((result *= int))", "\tdone", - "\techo \\${result\\}", + "\techo \"\\${result\\}\"", "}\n" ], "description": "calculate product of given integers" @@ -1008,10 +1024,10 @@ "# Usage: sum int1 int2 ...", "function sum () {", "\tlocal result=0", - "\tfor int in \\$@; do", + "\tfor int in \"\\$@\"; do", "\t\t((result += int))", "\tdone", - "\techo \\${result\\}", + "\techo \"\\${result\\}\"", "}\n" ], "description": "calculate sum of given integers" @@ -1022,7 +1038,7 @@ "# Usage: progressBar \"message\" currentStep totalSteps", "function progressBar() {", "\tlocal bar='████████████████████'", - "\tlocal space=' '", + "\tlocal space='....................'", "\tlocal wheel=('\\' '|' '/' '-')", "", "\tlocal msg=\"\\${1\\}\"", @@ -1032,7 +1048,7 @@ "\tlocal position=\\$((100 * current / total))", "\tlocal barPosition=\\$((position / 5))", "", - "\techo -ne \"\\r|\\${bar:0:\\$barPosition}\\${space:\\$barPosition:20}| \\${wheel[wheelIndex]} \\${position}% [ \\${msg} ] \"", + "\techo -ne \"\\r|\\${bar:0:\\$barPosition}\\$(tput dim)\\${space:\\$barPosition:20}\\$(tput sgr0)| \\${wheel[wheelIndex]} \\${position}% [ \\${msg} ] \"", "}\n" ], "description": "progress bar function" @@ -1044,7 +1060,7 @@ "function scan () {", "\tlocal openPortsArray=()", "\tfor ((port=\\${3\\}; port<=\\${4\\}; port++)); do", - "\t\t(echo >/dev/\\${1\\}/\\${2\\}/\\${port\\}) >/dev/null 2>&1 && openPortsArray+=(\"\\${port}\")", + "\t\t(echo >/dev/\"\\${1\\}\"/\"\\${2\\}\"/\"\\${port\\}\") >/dev/null 2>&1 && openPortsArray+=(\"\\${port}\")", "\tdone", "\techo \"\\${openPortsArray[@]}\"", "}\n" @@ -1064,10 +1080,10 @@ "\tlocal S=\\$((T%60))", "\tlocal result=\"\"", "", - "\t(( \\$D > 0 )) && result=\"\\${D}d \"", - "\t(( \\$H > 0 )) && result=\"\\${result}\\${H}h \"", - "\t(( \\$M > 0 )) && result=\"\\${result}\\${M}m \"", - "\t(( \\$S > 0 )) && result=\"\\${result}\\${S}s \"", + "\t(( D > 0 )) && result=\"\\${D}d \"", + "\t(( H > 0 )) && result=\"\\${result}\\${H}h \"", + "\t(( M > 0 )) && result=\"\\${result}\\${M}m \"", + "\t(( S > 0 )) && result=\"\\${result}\\${S}s \"", "\techo -e \"\\${result}\" | sed -e 's/[[:space:]]*\\$//'", "}\n" ], @@ -1095,7 +1111,7 @@ "\tfor (( i = 0; i < length; i++ )); do", "\t\tlocal c=\"\\${1:i:1}\"", "\t\tcase \"\\${c\\}\" in", - "\t\t\t[a-zA-Z0-9.~_-]) printf \"\\${c\\}\" ;;", + "\t\t\t[a-zA-Z0-9.~_-]) printf \"%s\" \"\\${c\\}\" ;;", "\t\t\t*) printf '%%%02X' \"'\\${c\\}\" ;;", "\t\tesac", "\tdone", @@ -1112,8 +1128,7 @@ "# Usage: versionCompare \"1.2.3\" \"1.1.7\"", "function versionCompare () {", "\tfunction subVersion () {", - "\t\ttemp=${1%%\".\"*} && indexOf=\\$(echo ${1%%\".\"*} | echo ${#temp})", - "\t\techo -e \"\\${1:0:indexOf}\"", + "\t\techo -e \"\\${1%%\".\"*}\"", "\t}", "\tfunction cutDot () {", "\t\tlocal offset=${#1}", @@ -1123,16 +1138,20 @@ "\tif [ -z \"\\${1\\}\" ] || [ -z \"\\${2\\}\" ]; then", "\t\techo \"=\" && exit 0", "\tfi", - "\tlocal v1=\\$(echo -e \"\\${1}\" | tr -d '[[:space:]]')", - "\tlocal v2=\\$(echo -e \"\\${2}\" | tr -d '[[:space:]]')", - "\tlocal v1Sub=\\$(subVersion \\$v1)", - "\tlocal v2Sub=\\$(subVersion \\$v2)", + "\tlocal v1", + "\tv1=\\$(echo -e \"\\${1}\" | tr -d '[:space:]')", + "\tlocal v2", + "\tv2=\\$(echo -e \"\\${2}\" | tr -d '[:space:]')", + "\tlocal v1Sub", + "\tv1Sub=\\$(subVersion \"\\$v1\")", + "\tlocal v2Sub", + "\tv2Sub=\\$(subVersion \"\\$v2\")", "\tif (( v1Sub > v2Sub )); then", "\t\techo \">\"", "\telif (( v1Sub < v2Sub )); then", "\t\techo \"<\"", "\telse", - "\t\tversionCompare \\$(cutDot \\$v1Sub \\$v1) \\$(cutDot \\$v2Sub \\$v2)", + "\t\tversionCompare \"\\$(cutDot \"\\$v1Sub\" \"\\$v1\")\" \"\\$(cutDot \"\\$v2Sub\" \"\\$v2\")\"", "\tfi", "}\n" ], @@ -1261,7 +1280,7 @@ "", "for ((${3:currentStep}=${4:1}; ${3} <= ${1}; ${3}++)); do", "\tsleep 0.1 # simulating one step of job", - "\tprogressBar \"Installing foo...\" \\${${3}\\} \\${${1}\\}", + "\tprogressBar \"Installing foo...\" \"\\${${3}\\}\" \"\\${${1}\\}\"", "done\n", "echo\n" ], @@ -1271,7 +1290,7 @@ "prefix": "fx scan local", "body": [ "# Usage: scan proto host fromPort toPort", - "openPorts=( \\$(scan ${1|tcp,udp|} \"${2|localhost,127.0.0.1,::1|}\" ${3:fromPort} ${4:toPort}) )", + "readarray -t openPorts < <(scan ${1|tcp,udp|} \"${2|localhost,127.0.0.1,::1|}\" ${3:fromPort} ${4:toPort})", "${5:echo \"\\${openPorts[@]\\}\"}\n" ], "description": "call scan function to scan localhost over a port range" @@ -1280,8 +1299,8 @@ "prefix": "fx time format seconds", "body": [ "# Usage: formatSeconds 70 -> 1m 10s", - "${1:result}=($(formatSeconds ${2:seconds}))", - "echo ${${1}[@]}\n" + "readarray -t ${1:result} < <(formatSeconds ${2:seconds})", + "echo \"${${1}[@]}\"\n" ], "description": "call formatSeconds function" }, @@ -1368,7 +1387,8 @@ "function.return-value": { "prefix": [ "function return value", - "func return value" + "func return value", + "func ret val" ], "body": "echo \"$?\"", "description": "last function/command return code" @@ -1500,7 +1520,7 @@ "prefix": "http cookie", "body": [ "curl --request ${1|GET,POST,DELETE,PUT|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--cookie ${3|'key=value',\"${key}=${value}\"|} \\", "\t--url ${4|'http://example.com',\"${url}\"|}\n" ], @@ -1510,7 +1530,7 @@ "prefix": "http download", "body": [ "curl --request ${1|GET,POST|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--output ${3|'/path/to/file',\"${pathToFile}\"|} \\", "\t--url ${4|'http://example.com/file.zip',\"${downloadUrl}\"|}\n" ], @@ -1523,7 +1543,7 @@ ], "body": [ "curl --request ${1|GET,DELETE|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--url ${3|'http://example.com',\"${url}\"|}\n" ], "description": "send http GET/DELETE request using curl" @@ -1532,7 +1552,7 @@ "prefix": "http header", "body": [ "curl --request ${1|GET,POST,DELETE,PUT|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--header ${3|'key: value',\"${key}\": \"${value}\"|} \\", "\t--url ${4|'http://example.com',\"${url}\"|}\n" ], @@ -1542,7 +1562,7 @@ "prefix": "http POST file", "body": [ "curl --request POST -sL \\", - "\t--user-agent '${1|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${1|Shellman,Your custom user agent|}' \\", "\t--url ${2|'http://example.com',\"${url}\"|} \\", "\t--form ${3|'key=value',\"${key}\"=\"${value}\"|} \\", "\t--form ${4|'file=@/path/to/file',file=@\"${pathToFile}\"|}\n" @@ -1556,7 +1576,7 @@ ], "body": [ "curl --request ${1|POST,PUT|} -sL \\", - "\t--user-agent '${2|Shellman,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/58.0.3029.110 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML\\, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|}' \\", + "\t--user-agent '${2|Shellman,Your custom user agent|}' \\", "\t--url ${3|'http://example.com',\"${url}\"|} \\", "\t--data ${4|'key=value',\"${key}\"=\"${value}\"|} \n" ], @@ -1566,7 +1586,7 @@ "prefix": "input password", "body": [ "echo \"${1:Please enter your password: }\"", - "read -s ${2:password}", + "read -rs ${2:password}", "${3:echo \"\\${password\\}\"}\n" ], "description": "get text as input from user without showing characters" @@ -1577,7 +1597,7 @@ "ask question" ], "body": [ - "read -ep \"${1:Question here? }\" -i \"${2:Default answer}\" answer", + "read -rep \"${1:Question here? }\" -i \"${2:Default answer}\" answer", "${3:echo \"\\${answer\\}\"}\n" ], "description": "get text as input from user" @@ -1954,11 +1974,6 @@ ], "description": "parse command line arguments (flags/switches)" }, - "misc.echo-array": { - "prefix": "echo array", - "body": "echo \\${${1:myArray}[@]}\n", - "description": "print array elements" - }, "misc.echo-text": { "prefix": [ "echo text", @@ -1972,7 +1987,7 @@ "echo variable", "print variable" ], - "body": "echo \"\\${${1|result,variable,X|}\\}\"\n", + "body": "echo \"\\${${1|result,variable|}\\}\"\n", "description": "print text, variable or both" }, "misc.exit": { @@ -2111,19 +2126,19 @@ "process.process-id": { "prefix": "process ID(s)", "body": [ - "${1:processIDsArray}=(\\$(pgrep ${2:processName}))", + "readarray -t ${1:processIDsArray} < <(pgrep ${2|processName, \"${processName}\"|})", "${3:echo \"\\${${1}[@]\\}\"}\n" ], "description": "find process ID(s) aka PIDs" }, "process.process-instances": { "prefix": "process instances", - "body": "${1:processInstances}=\\$(ps -A | grep \"\\${${2:processName}\\}\")\n", + "body": "${1:processInstances}=\\$(pgrep -l \"\\${${2:processName}\\}\")\n", "description": "list processes" }, "process.process-kill": { "prefix": "process kill", - "body": "sudo kill -9 \\$(pgrep ${1:processName})\n", + "body": "sudo kill -9 \"\\$(pgrep ${1:processName})\"\n", "description": "kill process by name" }, "process.process-list": { @@ -2254,7 +2269,7 @@ }, "string.trim-all": { "prefix": "string trim all", - "body": "${1:trimmed}=\\$(echo -e \"\\${${2:string}}\" | tr -d '[[:space:]]')\n", + "body": "${1:trimmed}=\\$(echo -e \"\\${${2:string}}\" | tr -d '[:space:]')\n", "description": "remove all white space(s)" }, "string.trim-left": { @@ -2315,7 +2330,7 @@ "system.memory-info": { "prefix": "system memory info", "body": [ - "sysMemory${1|MemTotal,MemFree,MemAvailable,Cached,Buffers,Active,Inactive,SwapTotal,SwapFree,SwapCached|}=\\$(cat /proc/meminfo | grep '${1}' | awk '{print \\$2}' | head -n 1)", + "sysMemory${1|MemTotal,MemFree,MemAvailable,Cached,Buffers,Active,Inactive,SwapTotal,SwapFree,SwapCached|}=\\$(grep '${1}' < /proc/meminfo | awk '{print \\$2}' | head -n 1)", "${2:echo \"\\${sysMemory${1}\\}\"}\n" ], "description": "system memory information in kilobytes (KB)" @@ -2370,7 +2385,7 @@ "service manage", "systemd manage" ], - "body": "sudo systemctl ${1|enable,disable,start,stop,reload,restart,status|} ${2|'serviceName',\"${serviceName}\"|}\n", + "body": "sudo systemctl ${1|enable,enable --now,disable,disable --now,start,stop,reload,restart,status|} ${2|'serviceName',\"${serviceName}\"|} # use '--user' for user services\n", "description": "manage service operations" }, "system.uptime-seconds": { @@ -2410,7 +2425,7 @@ "prefix": "time now UTC", "body": [ "${1:timeNowUTC}=\\$(date -u +%R)", - "${2:echo \\${${1}\\}}\n" + "${2:echo \"\\${${1}\\}}\"\n" ], "description": "current UTC time" },