Archive for May, 2008
Bash: Random in bash
Posted by JohannesTheDeveloper in Happy Hacking on May 31st, 2008
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.
Bash: Binary parameter search
Posted by JohannesTheDeveloper in Happy Hacking on May 25th, 2008
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
SVN up
Posted by JohannesTheDeveloper in fun with Linux on May 3rd, 2008
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
}
Backup compression
Posted by JohannesTheDeveloper in fun with Linux on May 31st, 2008
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.
comment
No Comments