sh.snippets (2986B)
1 snippet case "Case statement" b 2 case "$${1:variable}" in 3 "${2:Option 1}") 4 ;; 5 *) 6 ;; 7 esac 8 endsnippet 9 10 snippet argparse "Parse arguments/flags" b 11 PARAMS="" 12 13 while [ \$((\$#)) -ne 0 ]; do 14 case "\$1" in 15 -${1:letter}|--${2:long name}) 16 $2="\$2" 17 shift 2 18 ;; 19 $0 20 -h|--help) 21 echo "Usage:" 22 echo "${3:usage text}" 23 exit 0 24 ;; 25 --) # end arg parsing 26 shift 27 break 28 ;; 29 -*) # unsupported flags 30 echo "Unsupported flag \$1" >&2 31 exit 1 32 ;; 33 *) # preserve positional arguments 34 # if params is set, add space. 35 PARAMS="\$PARAMS\${PARAMS:+ }\$1" 36 shift 37 ;; 38 esac 39 done 40 set -- "\$PARAMS" 41 endsnippet 42 43 snippet !bash "Bash shebang" b 44 #!/usr/bin/env bash 45 $0 46 endsnippet 47 48 snippet !sh "Sh shebang" b 49 #!/bin/sh 50 $0 51 endsnippet 52 53 snippet checkdeps "Check script dependencies" b 54 checkdeps() { 55 for com in "$@"; do 56 command -v "$com" >/dev/null 2>&1 \ 57 || { printf '%s required but not found.\n' "$com" >&2 && exit 1; } 58 done 59 } 60 checkdeps "${1:command name}" 61 $0 62 endsnippet 63 64 snippet die "Die function" b 65 die() { printf '%s\n' "\$1" >&2 && exit 1; } 66 $0 67 endsnippet 68 69 snippet func "Function definition" b 70 ${1:function_name}() { 71 $0 72 } 73 endsnippet 74 75 snippet trap "Exit trap" b 76 trap ${1:cleanup_function} INT TERM EXIT 77 endsnippet 78 79 snippet untrap "Disable exit trap" b 80 trap - INT TERM EXIT 81 endsnippet 82 83 snippet for "POSIX for loop" b 84 : \$((${1:var}=0)) 85 while [ \$(($1 ${2:test})) -ne 0 ]; do 86 ${3:# code} 87 88 i=\$(($1+1)) 89 done 90 $0 91 endsnippet 92 93 snippet confirm "Ask for confirmation" b 94 printf "${1:prompt}" 95 stty raw && conf="$(dd bs=1 count=1 2>/dev/null)" && stty -raw 96 case "$conf" in 97 Y|y) 98 ${2:# code if yes} 99 ;; 100 *) 101 ${3:# code otherwise} 102 ;; 103 esac 104 $0 105 endsnippet 106 107 snippet osdetect "Detect OS" b 108 os=\$(uname -s | tr '[:upper:]' '[:lower:]') 109 case "\$os" in 110 linux*) 111 ${1:# code for Linux} 112 ;; 113 darwin*) 114 ${2:# code for macOS} 115 ;; 116 msys*|cygwin*|mingw*|nt|win*) 117 ${3:# code for Windows} 118 ;; 119 *) 120 printf "Operating system %s is unknown.\n" "\$os" 121 ;; 122 esac 123 endsnippet 124 125 snippet contains "String contains substring" w 126 [ -z "\${${1:strvar}##*${2:substr}*}" ]$0 127 endsnippet 128 129 snippet herevar "Store heredoc in a variable" b 130 # EOF quoted means no var expansion, dash means ignore indentation 131 unset ${1:heredoc} 132 while IFS="$(printf "\n")" read -r line; do 133 $1="$(printf "%s%s" "\$$1" "$line\n")" 134 done <<-'EOF' 135 ${2:heredoc contents} 136 EOF 137 endsnippet 138 139 snippet herefile "Store heredoc in a file" b 140 # EOF quoted means no var expansion, dash means ignore indentation 141 cat <<-'EOF' > "${1:filename}" 142 ${2:heredoc contents} 143 EOF 144 endsnippet 145 146 snippet readinput "Get input from arguments or stdin" b 147 if ! [ -t 0 ]; then 148 while read -r line; do 149 ${1:# handle stdin} 150 done 151 elif [ $# -gt 0 ]; then 152 ${2:#handle arguments} 153 fi 154 endsnippet 155 156 snippet printfq "A POSIX way to do printf %q" bw 157 printfq() { printf "'%s'\\n" "$(printf '%s' "\$1" | sed -e "s/'/'\\\\''/g")"; } 158 $0 159 endsnippet