25 points

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)}}}'

permalink
report
reply
19 points

This is just absurd and totally defines NOT as a one-liner in my book.

permalink
report
parent
reply
20 points

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.

permalink
report
parent
reply
11 points
*

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:

permalink
report
reply
2 points

Thanks, crossposted to !linux@programming.dev

permalink
report
reply
2 points
*

Nice function printer. I’m gonna steal this one, it is way better than mine.

permalink
report
reply
2 points

Though i just noticed that it crops the sed line of itself. Well, can’t have it all.

permalink
report
parent
reply

Linux

!linux@lemmy.ml

Create post

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word “Linux” in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

  • Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
  • No misinformation
  • No NSFW content
  • No hate speech, bigotry, etc

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

Community stats

  • 7.5K

    Monthly active users

  • 6.6K

    Posts

  • 179K

    Comments