Skip to content

Instantly share code, notes, and snippets.

View kurahaupo's full-sized avatar
🏠
Working from home

Martin Kealey kurahaupo

🏠
Working from home
  • Brisbane, Australia
  • 20:22 (UTC +10:00)
View GitHub Profile
@nicowilliams
nicowilliams / gist:f3fe2b10b380aecdef403acb246dced2
Last active September 13, 2021 22:39
getopts_long() for Bash

Long and Short CLI Options getopt_long()-like for Bash

Ever wanted to write bash scripts that can take long option names, but then struggled to have anything like a normal Unix program's handling of long and short options?

Yeah, me too. Finally I wrote a getopts_long() for Bash:

# getopts_long long_opts_assoc_array_name optstring optname args...
@kurahaupo
kurahaupo / siginfo
Last active December 29, 2015 12:19
Show signal names from Sig* lines in Linux's /proc/$PID/status
#!/bin/bash
pid=${1:-$PPID}
signals="$(trap -l)"
declare -A SIG
for s in ${signals//') '/=}
do
i=${s%%=*}
@kurahaupo
kurahaupo / readkey
Last active December 28, 2015 10:59
How to read ANSI keyboard sequences in Bash
readkey() {
local char
IFS= \
read -rs -d '' -N1 key || return $? # use -n1 on older versions
while
case "$key" in
('') key=$'\n' ; break ;; # compensate for bug
( $'\e[M'??? ) break ;; # mouse report
( $'\e' | $'\e[' | $'\e['[?O] | $'\e['*[\;0-9] | $'\e[M'* | $'\eO' ) ;;
( [$'\xc0'-\$'xfe'] \