[sclug] Bash script

Alex Butcher lug at assursys.co.uk
Wed Apr 22 20:51:01 UTC 2009


On Wed, 22 Apr 2009, Neil Haughton wrote:

> Thanks for your help. I have adopted and amended your script and it works
> perfectly. My final version is:
>
> #!/bin/bash
>
> if [ $# -ne 2 -o -z "$1" -o -z "$2" ]
> then
>       echo "Use: audoijoin.sh [codectype] [outputfilename]"
>       echo eg audiojoin.sh mp3 LargeFileName
>       exit 1
> fi
>
> echo "You have specified codec type=$1"
> echo "You have specified output file=$2.$1"

The accepted practice with UNIX scripts and commands is to stay silent
unless anything goes wrong, or --verbose (or whatever) is specified. When
you do emit an error, debug or status message, send it to stderr, rather
than stdout, so that if someone's piping your output for parsing, they don't
get confused by inline error/debug messages:

 	echo >&2 "This is a debug message"

To see the difference, run

 	echo "Hello" | less
and
 	echo >&2 "Hello" | less

> #clean up first
> rm joined.$1
>
> # Join all the files in the current directory together, into joined.$1
> shnjoin -o $1 *
>
> #rename the result as asked
> mv joined.$1 $2.$1
>
>
> I'd like to say this is my first successful bash script, but there's not
> much of my original left! But I'm happy, and I've learned quite a bit about
> the subtleties of bash scripting. It's not VBA! :-)

I've been using bash fairly extensively for a few years, and had to use VBA
for something recently and found it unnecessarily hard to use. The biggest
problems I found were a) no real standard for exit codes, so it was often
necessary to pipe the output to a file, then parse it to see whether it
failed or succeeded and b) inconsistent exporting of OS functions as
classes, meaning sometimes it was apparently necessary to execute standard
Windows commands in order to find out various aspects of the system
configuration. Oh, and in the same way that 'find' is a bit of a Swiss Army
Knife in UNIX, 'for' appears to have the same function in VBA.

> Thanks
> Neil.

HTH,
Alex.



More information about the Sclug mailing list