I love such collections. But calling this one a one-liner is a bit of stretch:
stty -icanon || stty -f /dev/stdin -icanon && perl -e 'use strict; use Time::HiRes qw(usleep gettimeofday); use IO::Select; my $sel=IO::Select->new(); $sel->add(*STDIN); my ($ts,$on,$t0,$td)=(0)x4; print "\n"; while (1){ my $k=0; if($sel->can_read(.02)) {sysread(STDIN,$k,1)} my $t=int(gettimeofday*100)/100; if($k eq "\n") {$on=0;$ts=0} $td=($ts+$t-$t0); if($on){printf "\033[A\r%.2f\033[J\n",$td} if($k){$on=1-$on; if($on){$t0=$t}else{$ts=($ts+$t-$t0)}}}'
Yeah, just because there is no newline character, does not make it a one liner. It does not even fit into one line and needs to be soft-broken into next line multiple times. And it includes a whole perl script. :D If anyone want to execute these complex lines, I would recommend to save as a script and let it autoformat to something readable.
Oneliner functions count too, right?
binexist() {
##: lookup input in PATH, returns bool true/false
# IFS=:; find $PATH -executable -name "$1" 2>/dev/null |grep -q "/$1$" # slower
command -v "$1" >/dev/null 2>&1
}
contains() {
##: finds term in string, returns true/false
##: $ contains <search term> <string>
case "$1" in *${2}*) return 0;; *) return 1;; esac
}
error() {
##: complain to STDERR and exit if given code
printf '%s\n' "$1" >&2; [ -n "$2" ] && exit "$2"
}
random() {
##: generate random number of <input> length
test "$1" -gt 1 && shuf -i 0-9 -n"$1" |tr -d '\n'
}
nfoview() {
##: view nfo files like intended
iconv -f CP866 <"$1" |less
}
mod_preset() {
##: print a nicely formatted preset of module options
modinfo -p "$1" |awk \
-F':' \
-v 'module="$1"' \
'{first=$1; $1="";print "\n#"$0"\n#options module "first"="}'
}
dpv() {
##: flash image to stick with progress bar, workaround for pv's flash bug #oflag=direct is fastest
##: https://askubuntu.com/questions/901481/writing-to-disk-using-pv-seems-to-be-fast-at-first-and-slow-at-the-end/961659#961659
pv "$1" |pkexec dd of="$2" bs=4M oflag=direct iflag=fullblock
}
Not oneliners but still something i want to show off:
functions() {
##: prints code of function files
function_s="$(sed '/^$/d;/##[^:]/d' "$SHELL_HOME"/functions)"
if [ -n "$1" ]
then echo "$function_s" |sed -n -e "/$1.*{/,/^}/ p" |highlight --line-numbers -qs candy --out-format=xterm256 --syntax=sh --stdout
else echo "$function_s" |highlight -qs candy --out-format=xterm256 --syntax=sh --stdout
fi
}
readconf() {
# reads parameters from config file in param=value note, where '=' is set with $separator
# format: conf_read <parameter> <replacement> where replacement get's used if parameter is empty
if [ -f "$cfg_file" ]
then parm="$(cut -d'#' -f1 "$cfg_file" |grep "$1" |cut -d${separator} -f2- |tr -d '"')"
else unset parm; fi
[ "$separator" = "=" ] && printf '%s\n' "${parm:-"$2"}"
}
The readconf function is used like this:
config content:
# this a comment
apple=red
banana=yellow # this too
config=/path/to/config.conf
$ color_apple="$(readconf apple green)"
$ color_banan="$(readconf banana)"
And the ‘functions’ function looks like this:
Thanks, crossposted to !linux@programming.dev
Nice function printer. I’m gonna steal this one, it is way better than mine.