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
#!/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 / _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 / 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 / installheadlessfirefoxonpi.sh
Created February 17, 2024 06:52 — forked from nestukh/installheadlessfirefoxonpi.sh
Install firefox-esr + geckodriver + selenium + python3 on raspberry pi 3 and above
#!/bin/bash
pypcks="python3-pip python3 python3-all-dev python3-dev libffi-dev libssl-dev librtmp-dev python-dev python3 python3-doc python3-tk python3-setuptools tix xvfb python-bluez python-gobject python-dbus python cython python-doc python-tk python-numpy python-scipy python-qt4 python3-pyqt5 python3-pyqt5.q* python3-qtpy python-pyqt5.q* python-lxml fontconfig python-demjson qt5-default libqt5webkit5-dev build-essential libudev-dev python-lxml libxml2-dev libxslt-dev libpq-dev python-pyside python-distlib python-pip python-setuptools" # python-examples python3-examples python-vte
allgoodpcks="ca-certificates virtualenv autotools-dev cdbs git expect libnss3-tools util-linux xvfb curl bridge-utils chromium-browser chromium-chromedriver firefox-esr"
sudo apt-get install --reinstall -y $pypcks $allgoodpcks
if [[ ! -f /usr/lib/chromium-browser/chromedriver ]]; then
sudo ln -s /usr/bin/chromedriver /usr/lib/chromium-browser/chromedriver
fi
@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 / backup.sh
Created May 15, 2023 08:40
Borg Backup Script
#!/bin/bash
# vim: ft=bash
#
# borg init -enone `hostname -f`
STDOUT() { printf '%q' "$1"; printf ' %q' "${@:2}"; printf '\n'; }
STDERR() { local e=$?; STDOUT "$@" >&2; return $e; }
OOPS() { STDERR OOPS: "$@"; exit 23; }
x() { "$@"; STDERR exec $?: "$@"; }
o() { x "$@" || OOPS fail $?: "$@"; }
@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 / nginx.conf
Last active April 24, 2021 05:51
Example for Cookie-based Access Token with NginX HttpSecureLinkModule http://wiki.nginx.org/HttpSecureLinkModule and PHP (in this case for Typo3). Note that Typo3 needs to set the cookie as shown in token.php
# Excerpt of nginx config file
#..
#
set $ts 0;
if ($cookie_TOKEN ~ ",(.*)$") {
set $ts $1;
}
secure_link $cookie_TOKEN; # See TYPO3_ACCESSTOKEN
set $sec CHANGE_THIS_SHARED_SECRET; # See TYPO3_ACCESSKEY
secure_link_md5 $sec$ts$sec;
@hilbix
hilbix / deswappify.pl
Last active December 18, 2020 21:44 — forked from Someguy123/deswappify.pl
#!/usr/bin/perl
use strict;
use Fcntl qw(SEEK_SET);
sub deswappify {
my $pid = shift;
my $fh = undef;
my $start_addr;
// I recently saw http://davidwalsh.name/detect-native-function tweeted by
// @elijahmanor and was pretty jazzed about it. One of my favorite JS tricks is
// detecting native methods. Detecting native methods is handy because third
// party code can shim methods incorrectly as seen in past versions of
// Prototype.js, es5-shim, & modernizr, which can cause your code to behave in
// unexpected ways. This isn't a knock against those projects, shimming is really
// really hard to get right. Shimmed methods may also lack the performance
// benefits of their native counterparts. Lo-Dash, Dojo, Ember, & YUI,
// to name a few, detect native methods to avoid shims and rely on their own
// fallback paths, trusting their code over third-party.