Archive for May, 2008

Bash: Random in bash

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Just discovered $RANDOM in bash.
I usually just did something like “grep –text -Eo ‘[0-9]{2,3}’ /dev/urandom” which is fine for most cases.

No Comments

Backup compression

I compressed a backup with various tools:

1606M original tarfile
1226M bzip2 –best
1.3G gzip –best (sorry, no exact MB)
1152M lzma -7 (default)
1131M lzma -8

LZMA (7-zip) is definitly a algorithm heavy on cpu and memory, but achieved great compression.
This shouldn’t be a benchmark (as I didn’t measure time), only a perception in this specific use case.

No Comments

Bash: Binary parameter search

Suppose you look for a specific numeric parameter n for a program.
It behaves in one way if the parameter is less than n and in another when it is greater than n.
Binary search can be applied.

Examples when you have such a scenario are:

  • A web application, and you fill in GET/POST-values with curl or wget
  • You use a revision control system and a bug was introduced at some version, and you wrote a test for that bug. (git has a command for that: git-bisect)

Anyway, here a simple binarytest.sh.
It calls the program (third parameter) with a number as parameter.

#!/bin/bash
START=$(($1))
STOP=$(($2))
TEST="$3"
if [ "$START" == "" ] || [ "$STOP" == "" ] || [ "$START" == "$STOP" ] || [ "$TEST" == "" ]; then
echo "SYNAPSIS: $0 <start> <stop> <testprog>"
exit
fi
while [ "$START" != "$STOP" ]; do
N=$(( ($START + $STOP)/2 ))
echo "Testing against $N"
if $TEST $N; then
if [ $(($START-$STOP)) == "-1" ]; then
START="$STOP"
else
START="$N"
fi
echo "selecting upper half: $START:$STOP"
else
if [ $(($START-$STOP)) == "-1" ]; then
STOP="$START"
else
STOP="$N"
fi
echo "selecting lower half: $START:$STOP"
fi
done

No Comments

SVN up

If you do a svn up, you want to know what happened in between, right?

Log entries, Files changed. You can either go step by step through the revisions or let this bash script do the svn up for you:

#!/bin/bash
oldrev=$(svn info |grep '^Revision: '|sed 's/Revision: //g')
svn up
newrev=$(svn info |grep '^Revision: '|sed 's/Revision: //g')
[ "$oldrev" == "$newrev" ] || {
echo "Made the step from $oldrev to $newrev; Log: "
for i in $(seq $oldrev $newrev); do
svn log -r $i
echo Files:
svn diff --diff-cmd diff -x -q -c $i | grep '^Index:' | sed 's/^Index://g'
echo
done
}

2 Comments