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 / _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 / 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 / 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 / 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 / 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.
#!/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 / COPYRIGHT.CLL
Created October 27, 2015 11:38
COPYRIGHT.CLL copied from http://permalink.de/tino/cll
@hilbix
hilbix / wtfpl
Created October 27, 2015 11:42 — forked from nielsvanderbeke/wtfpl
Do What The Fuck You Want To Public License
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@hilbix
hilbix / permanent_auth_sock_hack.sh
Last active April 24, 2017 11:39
Allow SSH_AUTH_SOCK to roam (use a working agent connection)
# Following remembers possible agent connections,
# switches to some still open connection if the current one fails,
# and pins SSH_AUTH_SOCK to a fixed location
# such that you never need to worry about breaking connections.
#
# This perhaps is bash only. Source this in your .bashrc or .bash_aliases
# Currently needs "timeout" installed as well.
# See https://gist.github.com/hilbix/7d3bacb1b91cdf6df5c6
permanent_auth_sock_hack()
@hilbix
hilbix / FIX-pipeline-import-problem.sh
Created September 2, 2016 14:41
Fix problem with GitLab not importing exported projects due to error "Failed to replace pipelines because one or more of the new records could not be saved."
#!/bin/bash
OOPS() { { printf 'OOPS:'; printf ' %q' "$@"; printf '\n'; } >&2; exit 23; }
x() { "$@"; }
o() { x "$@" || OOPS failed "$?:" "$@"; }
case "$#:$1" in
0:'') cat <<EOF
Usage: $0 ...tar.gz
Fixes following problem when an Import onto GitLab fails with following error: