There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

ocv.me

MonkderVierte , (edited ) to linux in Unix oneliners (mostly unorganized collection of bash oneliners)

Oneliner functions count too, right?


<span style="color:#323232;">binexist() {
</span><span style="color:#323232;">	##: lookup input in PATH, returns bool true/false
</span><span style="color:#323232;">#	IFS=:; find $PATH -executable -name "$1" 2>/dev/null |grep -q "/$1$" # slower
</span><span style="color:#323232;">	command -v "$1" >/dev/null 2>&1
</span><span style="color:#323232;">}
</span><span style="color:#323232;">contains() {
</span><span style="color:#323232;">	##: finds term in string, returns true/false
</span><span style="color:#323232;">	##: $ contains <search term> <string>
</span><span style="color:#323232;">	case "$1" in *${2}*) return 0;; *) return 1;; esac
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">error() {
</span><span style="color:#323232;">       ##: complain to STDERR and exit if given code 
</span><span style="color:#323232;">        printf '%sn' "$1" >&2; [ -n "$2" ] && exit "$2"
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">random() {
</span><span style="color:#323232;">	##: generate random number of <input> length
</span><span style="color:#323232;">	test "$1" -gt 1 && shuf -i 0-9 -n"$1" |tr -d 'n'
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">nfoview() {
</span><span style="color:#323232;">	##: view nfo files like intended
</span><span style="color:#323232;">	iconv -f CP866 <"$1" |less
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">mod_preset() {
</span><span style="color:#323232;">	##: print a nicely formatted preset of module options
</span><span style="color:#323232;">	modinfo -p "$1" |awk 
</span><span style="color:#323232;">		-F':' 
</span><span style="color:#323232;">		-v 'module="$1"' 
</span><span style="color:#323232;">		'{first=$1; $1="";print "n#"$0"n#options module "first"="}'
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">dpv() {
</span><span style="color:#323232;">	##: flash image to stick with progress bar, workaround for pv's flash bug #oflag=direct is fastest
</span><span style="color:#323232;">	##: https://askubuntu.com/questions/901481/writing-to-disk-using-pv-seems-to-be-fast-at-first-and-slow-at-the-end/961659#961659
</span><span style="color:#323232;">	pv "$1" |pkexec dd of="$2" bs=4M oflag=direct iflag=fullblock
</span><span style="color:#323232;">}
</span>

Not oneliners but still something i want to show off:


<span style="color:#323232;">functions() {
</span><span style="color:#323232;">	##: prints code of function files
</span><span style="color:#323232;">	function_s="$(sed '/^$/d;/##[^:]/d' "$SHELL_HOME"/functions)"
</span><span style="color:#323232;">	if [ -n "$1" ]
</span><span style="color:#323232;">		then echo "$function_s" |sed -n -e "/$1.*{/,/^}/ p" |highlight --line-numbers -qs candy --out-format=xterm256 --syntax=sh --stdout
</span><span style="color:#323232;">		else echo "$function_s" |highlight -qs candy --out-format=xterm256 --syntax=sh --stdout
</span><span style="color:#323232;">	fi
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">readconf() {
</span><span style="color:#323232;">	##: reads parameters from $config
</span><span style="color:#323232;">	##: format: readconf <parameter> <replacement> where replacement get's used if parameter is empty
</span><span style="color:#323232;">	var="$(grep "${1}=" "$config" |grep -Ev "^#|^$" |cut -d'=' -f2- |tr -d '"')"
</span><span style="color:#323232;">	if [ -z "$var" ]; then
</span><span style="color:#323232;">		var="$2"
</span><span style="color:#323232;">	fi
</span><span style="color:#323232;">	echo "$var"
</span><span style="color:#323232;">}
</span>

The readconf function is used like this:


<span style="color:#323232;">config content:
</span><span style="color:#323232;"># this a comment
</span><span style="color:#323232;">apple=red
</span><span style="color:#323232;">banana=yellow # this too
</span><span style="color:#323232;">
</span><span style="color:#323232;">config=/path/to/config.conf
</span><span style="color:#323232;">$ color_apple="$(readconf apple green)"
</span><span style="color:#323232;">$ color_banan="$(readconf banana)"
</span>

And the ‘functions’ function looks like this:

https://lemmy.ml/pictrs/image/3e6debf2-0e52-4ae3-aada-4ea3a13be245.png

Blaze , to linux in Unix oneliners (mostly unorganized collection of bash oneliners)

Thanks, crossposted to !linux

thingsiplay , to linux in Unix oneliners (mostly unorganized collection of bash oneliners)

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(gettimeofday100)/100; if($k eq “n”) {$on=0;$ts=0} $td=($ts+$t-$t0); if($on){printf “

Dirk ,
@Dirk@lemmy.ml avatar

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

thingsiplay ,

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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • lifeLocal
  • goranko
  • All magazines