68
top 7 comments
sorted by: hot top controversial new old
[-] thingsiplay@beehaw.org 26 points 3 months ago

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

[-] Dirk@lemmy.ml 19 points 3 months ago

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

[-] thingsiplay@beehaw.org 20 points 3 months ago

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.

[-] MonkderVierte@lemmy.ml 11 points 3 months ago* (last edited 3 months ago)

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:

[-] Blaze@lemmy.zip 2 points 3 months ago
[-] j4k3@lemmy.world 2 points 3 months ago* (last edited 3 months ago)

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

[-] MonkderVierte@lemmy.ml 2 points 3 months ago

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

this post was submitted on 29 Jul 2024
68 points (98.6% liked)

Linux

47964 readers
1063 users here now

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

Related Communities

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

founded 5 years ago
MODERATORS