commit fb483aecbb96851904b0686d9e67de16a4a712af
parent 6e6eba887d505cc2dbf766ff411733b128efed70
Author: Alex Balgavy <alex@balgavy.eu>
Date: Thu, 20 May 2021 11:19:10 +0200
vim: shell snippets are POSIX
Diffstat:
1 file changed, 19 insertions(+), 19 deletions(-)
diff --git a/vim/ultisnips/sh.snippets b/vim/ultisnips/sh.snippets
@@ -1,16 +1,3 @@
-snippet select "Select menu" b
-select ${1:dest variable} in "${2:Option 1}" "${3:option 2}"; do
- case "$$1" in
- "$2")
- break;;$0
- "$3")
- break;;
- *)
- break;;
- esac
-done
-endsnippet
-
snippet case "Case statement" b
case "$${1:variable}" in
"${2:Option 1}")
@@ -62,11 +49,14 @@ snippet !sh "Sh shebang" b
$0
endsnippet
-snippet exists "Check if command exists" w
-if ! command -v ${1:command} 1>/dev/null 2>&1; then
- echo "$1 not installed." >&2
- exit 1
-fi
+snippet checkdeps "Check script dependencies" b
+checkdeps() {
+ for com in "$@"; do
+ command -v "$com" >/dev/null 2>&1 \
+ || { printf '%s not found.\n' "$com" >&2 && exit 1; }
+ done
+}
+checkdeps "${1:command name}"
$0
endsnippet
@@ -101,7 +91,7 @@ endsnippet
snippet confirm "Ask for confirmation" b
printf "${1:prompt}"
-read -r conf; case "$conf"
+read -r conf; case "$conf" in
Y|y)
${2:# code if yes}
;;
@@ -150,3 +140,13 @@ cat <<-'EOF' > "${1:filename}"
${2:heredoc contents}
EOF
endsnippet
+
+snippet readinput "Get input from arguments or stdin" b
+if ! [ -t 0 ]; then
+ while read -r line; do
+ ${1:# handle stdin}
+ done
+elif [ $# -gt 0 ]; then
+ ${2:#handle arguments}
+fi
+endsnippet