Re: bash vs. python scripts - which one is better?
On Sun, Aug 19, 2007 at 02:40:19PM -0700, Steve Lamb wrote:
> David Brodbeck wrote:
> > In particular, Bash's "test" (aka "[")
> >operator has pitfalls. Testing for an empty variable, for example, is
> >awkward. If you do:
>
> >if [ $foo == "" ]
Yeah, and the spaces between the [ $ and the " ] are critical too; I
just forget in what way.
>
> Yeah, prefer:
>
> if not foo:
> do something
>
> >-- there are few languages where it's quite so easy to test conditions
> >like, 'is this a directory?'.
>
> Bash undoubtedly is more concise than this but I'd contend it is no
> easier.
>
> import os
> if os.path.isdir(somedir):
> print "It's a dir, Jim!"
Best of all, at 2:00 a.m. a year from now, its perfectly clear what
if os.path.isdir(somedir):
means.
>
> >Bash is great if you need to glue together a bunch of existing utilities
> >to do something. You can pipe output directly from one utility to
> >another with an ease that doesn't exist in most other languages, which
> >tend to make it complicated to launch other processes. Doing complex
> >tasks with a series of simple utilities is sort of what Unix is all about.
Right, but debugging a long piece of plumbing can take a while.
>
> I contend that is more a was than an is. Shell filled a niche years
> ago that now has largely shrunk to special cases. So far in this thread
> there have been two example total of where a shell script might be better
> for more than 2 minutes over a proper scripting language. Generally by the
> time any shell script starts piping 2-3 things I just convert it to Python
> and get it done far easier and far faster. Mainly because I don't have to
> jump through the hoops the warts above present.
>
> >Besides, until operating systems start having init scripts written in
> >Perl or Python, being able to write shell scripts is going to be an
> >essential system administration skill. ;)
Anything after /usr is mounted (perhaps runlevel 2) could be in python.
The init scripts for rcS.d should be carefully written to be
understandable by non-bash gurus.
Check out the very first initscript, S01glibc.sh (exerpts below). It
may as well be written in assembler for all I can understand how it
compares kernel versions.
Doug.
if [ "`uname -s`" = Linux ]; then
# glibc kernel version check: KERNEL_VERSION_CHECK
kernel_compare_versions () {
verA=$(($(echo "$1" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1 \* 10000 + \2 \* 100 + \3/')))
verB=$(($(echo "$3" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1 \* 10000 + \2 \* 100 + \3/')))
test $verA -$2 $verB
}
exit_check () {
sleep 5
exit 1
}
# Test to make sure z < 255, in x.y.z-n form of kernel version
# Also make sure we don't trip on x.y.zFOO-n form
#kernel_rev=$(uname -r | tr -- - . | cut -d. -f3 | tr -d '[:alpha:]')
kernel_rev=$(uname -r | sed 's/\([0-9]*\.[0-9]*\.\)\([0-9]*\)\(.*\)/\2/')
if [ "$kernel_rev" -ge 255 ]
Reply to: