Skip to content

Instantly share code, notes, and snippets.

@tcely
tcely / qnap-crypt-plaintext.sh
Last active September 18, 2017 02:25
QNAP LUKS password hashing
#!/bin/sh
docker run --rm -it perl:5-threaded \
perl -e 'print(q{Enter pass phrase: }); system("stty -echo"); chomp($pt = <>); system("stty echo"); print(qq{\n}, crypt($pt, q{$1$YCCaQNAP$}), qq{\n});'
@tcely
tcely / defensive_bash.sh
Created December 9, 2017 21:36
Clean bash aliases and functions
#!/bin/bash
# *Temporarily* force Bash into POSIX compatibility mode, where `unset` cannot
# be shadowed, which allows us to undefine any `unset` *function* as well
# as other functions that may shadow crucial commands.
# Note: Fortunately, POSIXLY_CORRECT= works even without `export`, because
# use of `export` is not safe at this point.
# By contrast, a simple assignment cannot be tampered with.
POSIXLY_CORRECT=
@tcely
tcely / keybase.md
Created December 11, 2017 20:21
keybase.io: keybase prove github tcely

Keybase proof

I hereby claim:

  • I am tcely on github.
  • I am tcely (https://keybase.io/tcely) on keybase.
  • I have a public key ASCepDrTFuNbJ51KamisH6cg9iNJN90f5vFYyIQYJ4nTWgo

To claim this, I am signing this object:

@tcely
tcely / conditional_get_etag.sh
Created January 16, 2018 16:35
Download a file when its ETag changes. Useful for GitHub (which doesn't respect If-Modified-Since).
#!/bin/bash
conditional_get_etag() {
local _url _file _tmpdir
local _awk_program='/^ETag:/ {$1=""; printf "If-None-Match: %s", substr($0, 2, length($0)-2); exit;}'
for _url; do
if [ '--file=' = "${_url:0:7}" ]; then
_file="${_url:7}"
continue
@tcely
tcely / min_max.awk
Last active May 13, 2022 23:28
min & max functions for awk. I was a bit surprised these weren't already in the numeric functions section of the man page already.
#!/usr/bin/awk -f
function limit_seeker(a, b, direction) {
_record=b;
if (isarray(a)) {
for (k in a) {
_record=limit_seeker(a[k], _record, direction);
}
} else {
@tcely
tcely / shebang.awk
Last active January 12, 2023 19:42
A good portable awk shebang is not easy to find.
#!/usr/bin/awk -f
#!/usr/bin/awk -E
#
# If you have an awk version that doesn't support the -f flag,
# then you are just out of luck.
#
# If you just have no clue where awk will be, or you prefer to use -E,
# then you can try this bash snippet to launch awk for you.
#
# You might think the -E tests below are overly complex. You'd be wrong.
@tcely
tcely / README.md
Last active May 27, 2019 00:27
Restic "Pull" Solution

As I originally outlined in this comment there is a work around for the problem of having shared credentials and keys on the various backup "clients" so that they push their contents to your restic server.

I have implemented this solution, but it took a fair amount of work. I am putting this work up for bounty, so if you are interested please contact me to arrange payment and when the goal is reached, I will send my solution to everyone who chipped in.

Thank you very much!

#!/bin/sh
github_email_to_login() {
local url='https://api.github.com/search/users'
local filter='.items|.[]|select(.type == "User")|.login'
local email="${1}"
curl -sSL "${url}?q=in:email+${email}" | jq -r "${filter}"
}
@tcely
tcely / README.md
Last active June 26, 2019 00:21
AlpineLinux aports style decisions
  1. Shell (ash from busybox) variables are always quoted and use curly braces.

    "Optimize for the inexperience newcomers rather than the experience gurus." As explained in this post there are a few quirks when dealing with shell variables that do not use the curly braces. For example: $pkgver

    Instead of expecting new contributors to already know about all of the pitfalls involved with using that syntactic sugar, it is better to just always use the braces. Shell variables are quoted when assigned and used, when accessed they are quoted and surrounded by curly braces. For example: pkgname='bind' or pkgdesc="${pkgname} tools" and "${pkgname}"

The characters that do and do not extend the name of the variable are not a concern when the intention of what the variable name should be is made clear by using this explicit syntax. Users don't need to remember which of _, -, or . will change the variable name and lead to an empty value be

@tcely
tcely / simple-echo.function.sh
Created September 22, 2020 00:04
A simple echo using the shell printf for consistent behavior
#!/usr/bin/env sh
secho() {
_arg="${1}";
_fmt='%s';
_sentinel='--';
case "${_arg}" in
(-e|-en|-ne) _fmt='%b'; shift ;;
(-n|-En|-nE) shift ;;