Skip to content

Instantly share code, notes, and snippets.

View l0b0's full-sized avatar

Victor Engmark l0b0

View GitHub Profile
@l0b0
l0b0 / gist:671067
Created November 10, 2010 16:24
test_bash_set.sh
#!/bin/bash
echo ' ${var-_} ${var:-_} $var ${!var[@]}'
test_all()
{
test "${var-_}" && echo -n 'T ' || echo -n 'F '
test "${var:-_}" && echo -n 'T ' || echo -n 'F '
test "$var" && echo -n 'T ' || echo -n 'F '
[[ "${!var[@]}" ]] && echo 'T' || echo 'F'
@l0b0
l0b0 / gist:672244
Created November 11, 2010 09:22
Bash logic test script by Dennis Williamson
#!/bin/bash
# by Dennis Williamson
# 2010-10-06, revised 2010-11-10
# for http://stackoverflow.com/questions/3869072/test-for-non-zero-length-string-in-bash-n-var-or-var
# designed to fit an 80 character terminal
dw=5 # description column width
w=6 # table column width
t () { printf "%-${w}s" "true"; }
@l0b0
l0b0 / gist:715225
Created November 25, 2010 11:16
Compress a path, but exclude all mount points in it
tar -X <(cat /etc/fstab | cut -d ' ' -f 2 | grep ^$dir) -zcvf dir.tar.gz $dir/*
@l0b0
l0b0 / safe-find.sh
Created November 26, 2010 14:17
Completely safe find
#!/bin/bash
# Filenames can contain *any* character except only null (\0) and slash (/);
# here's some general rules to handle them:
#
# $'...' can be used to create human readable strings with escape sequences.
#
# ' -- ' in commands is necessary to separate arguments from filenames, since
# filenames can start with '--', and would therefore be handled as parameters.
# To handle parameters properly (like GNU tools) use `getopt`.
#
@l0b0
l0b0 / safe-count-files.sh
Created November 26, 2010 14:49
Count the number of files in a directory
# This works even with really weird filenames like $'--$`\! *@ \a\b\e\E\f\n\r\t\v\\\"\' '
file_count()
{
if [ ! -e "$1" ]
then
exit 1
fi
local -i files=$(find "$(readlink -f -- "$1")" -type f -print0 | grep -cz -- -)
echo $files
@l0b0
l0b0 / gist:719872
Created November 29, 2010 11:53
Count the number of zero bytes in a string
# Can be used to for example count the number of files returned by `find ... -print0`
grep -cz . -- -
@l0b0
l0b0 / jail-mtab.sh
Created December 6, 2010 14:52
Make df work in a chroot jail
grep -v '^\(none\|rootfs\)' /proc/mounts > /etc/mtab
@l0b0
l0b0 / stdout_and_stderr.sh
Created December 7, 2010 11:27
Output to *both* stdout and stderr
command | tee /dev/fd/2
# Can be used to debug variable computations - Just plonk the tee at the end:
# $ uid=$(id -u | tee /dev/fd/2)
# 1234
# $ echo $uid
# 1234
@l0b0
l0b0 / test_asap.sh
Created December 7, 2010 14:22
Test when something changes
while true
do
inotifywait -e modify,attrib,move,delete .
make test
done
@l0b0
l0b0 / makefile-missing.sh
Created December 7, 2010 16:15
Find files missing from the local Makefile
find . -maxdepth 1 -print0 | \
sort --zero-terminated | \
while IFS= read -rd $'\0' path
do
grep "$(basename -- "${path:2}")" -- Makefile >/dev/null || echo "Could not find $path"
done