Skip to content

Instantly share code, notes, and snippets.

View hilbix's full-sized avatar
🐢
I may be slow to respond.

Valentin Hilbig hilbix

🐢
I may be slow to respond.
View GitHub Profile
@hilbix
hilbix / COPYRIGHT.CLL
Created October 27, 2015 11:38
COPYRIGHT.CLL copied from http://permalink.de/tino/cll
#!/bin/bash
# This Works is placed under the terms of the Copyright Less License,
# see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY.
#
# COPYRIGHT.CLL can be found at http://permalink.de/tino/cll
# (CLL is CC0 as long as not covered by any Copyright)
OOPS() { echo "OOPS: $*" >&2; exit 23; }
[ -z "`pidof openssl`" ] || OOPS "openssl running, consider: killall openssl"
@hilbix
hilbix / lockrecipe.sh
Created October 22, 2015 11:00
Shell Locking Recipe for `producer > >(consumer)`
#!/bin/bash
#
# Locking example for on how to synchronize background processes.
# Here is what this is for:
# producer() { a=2; echo hello world; return 1; }
# consumer() { a=3; sleep 1; cat; return 2; }
# a=1; producer | consumer ; echo a=$a # gives hello world; a=1
# a=1; producer > >(consumer); echo a=$a # gives a=2; hello world
# a=1; consumer < <(producer); echo a=$a # gives hello world; a=3
# As seen the a=2 output case is a bit asynchronous/race conditioned.
@hilbix
hilbix / bashrc.md
Last active August 6, 2023 17:22
Timeout __git_ps1 on slow network shares

Add following to your .bashrc:

__git_ps1() { setsid -w /bin/bash -c 'sleep 1 & . /usr/lib/git-core/git-sh-prompt && __git_ps1 "$@" & wait -n; p=$(/usr/bin/ps --no-headers -opgrp $$) && [ $$ = ${p:-x} ] && /usr/bin/kill -9 0; echo "PGRP mismatch $$ $p" >&2' bash "$@"; }

1s usually is enough for local files which are in cache on modern machines, but does not delays the shell prompt too much in case network share need a bit longer.

Note: Due to timeout killing git this may leave .lock-files in your .git directory, which is a bit annoying, as git bails out if it sees such .lock when altering a .git repo. However git tells the lockfile positions and you then can(must) manually rm them. You can improve this by setting GIT_PS1_xxxx variables such, that git needs no locking when evaluating the $PS1. However I am not completely sure which options causes this issue or not. YMMV.

@hilbix
hilbix / json2bash.inc
Last active February 27, 2024 22:49
Parse JSON into BASH variables, with onedimensional array support
#!/bin/bash
#
# A slightly more secure variant of this script.
# It should be secure against primitive attacks like:
json2bash <<<'{" ":{"; rm -rf /; ":1}}'
#
# However processing JSON from untrustworthy sources still can confuse your script!
# YOU HAVE BEEN WARNED!
# Following needs bash. Use like in:
@hilbix
hilbix / avcheck.sh
Created February 1, 2015 04:11
Print runtime length of a video (and number of estimated frames)
#!/bin/bash
avcheck()
{
avprobe "$1" -show_packets 2>/dev/null |
awk -F'=' -vN="$1" '
/^\[/ { if (arm && k["codec_type"]=="video") { last=k["dts_time"]; # printf "%s", k["flags"];
}; delete k; arm=1; next; }
arm { k[$1]=$2; }
$0=="codec_type=video" { frames++; }
@hilbix
hilbix / _catch.inc
Last active November 17, 2020 04:42
Run something in current context of shell, postprocessing stdout&stderr without disturbing return code
# Redirect lines of stdin/stdout to some other function
# outfn and errfn get following arguments
# "cmd args.." "one line full of output"
: catch outfn errfn cmd args..
catch()
{
local ret o1 o2 tmp
tmp=$(mktemp "catch_XXXXXXX.tmp")
mkfifo "$tmp.out"
mkfifo "$tmp.err"
@hilbix
hilbix / _relpath.inc
Last active February 27, 2024 22:50
Calculate relative path in BASH
: relpath A B
# Calculate relative path from A to B, returns true on success
# Example: ln -s "$(relpath "$A" "$B")" "$B"
relpath()
{
local X Y A
# We can create dangling softlinks
X="$(readlink -m -- "$1")" || return
Y="$(readlink -m -- "$2")" || return
X="${X%/}/"
@hilbix
hilbix / edit-launcher.sh
Created May 25, 2014 09:31
Wrapper around desktop-file-install to edit Ubuntu launchers (`.desktop` files aka shortcuts)
#!/bin/bash
LOC="$HOME/.local/share/applications"
OOPS()
{
echo "OOPS: $*" >&2
exit 1
}
@hilbix
hilbix / exec style aliases
Last active March 10, 2019 01:21
`git exec` to exec command within GIT directory. `git top` to exec on submodule top level directory (somewhat).
git config --global alias.exec '!exec '
git config --global alias.top '!f() { GIT_TOP="${GIT_DIR%%/.git/modules/*}"; [ ".$GIT_TOP" != ".$GIT_DIR" ] && cd "$GIT_TOP"; exec "$@"; }; f'
# THIS IS PUBLIC DOMAIN.
#
# `git exec` executes something in the current GIT module's base directory. `git exec pwd` gives this base.
# Stolen from http://stackoverflow.com/questions/957928/is-there-a-way-to-get-the-git-root-directory-in-one-command/957978#comment9747528_957978
#
# `git top` is like `git exec`, but executes in the topmost GIT directory, even from within a GIT submodule.