[sclug] Bash scripting help needed

Dickon Hood sclug at splurge.fluff.org
Thu Apr 23 09:46:58 UTC 2009


On Tue, Apr 21, 2009 at 20:30:30 +0100, Neil Haughton wrote:
: Hi,

: I'm starting out (it had to happen eventually) on a bit of bash scripting.
: It's simple task really, but I want to save myself a bit of repetitive
: labour (as you do).

[...]

: #! /bin/bash
    ^-- style only, but I'd get rid of that space.

: if ["$1" -eq ""]; then
: echo "Use: command codectype outputfilename"
: exit
: fi

: if ["$2" -eq ""]; then
: echo "Use: command codectype outputfilename"
: exit
: fi

: echo "codec type=$1"
: echo "output file=$2"

: #build a list of the files in the current directory
: fileList=""
: for f in $(ls ./); do
:  fileList="$fileList \"$f\""
: done

That probably won't do what you want if you have spaces anywhere.  I'd
use:

for i in "*.$1"; do

instead.  This still doesn't do what you want, though.

: #now join them all together into one big audio file
: `shnjoin -o $1 $fileList`

: #rename the result as asked
: mv joined.$1 $2.$1
: #end of script

: So I execute it with (say):

: $audiojoin flac Act1

I'd *hope* you don't have . in $PATH...

: and I should end up with all the .flac files in the current directory
: concatenated into a single file called Act1.flac

: Here's the console output:

: /usr/local/bin/audiojoin: line 3: [flac: command not found
: /usr/local/bin/audiojoin: line 8: [Act1: command not found
: codec type=flac
: output file=Act1
: shnjoin: warning: cannot open non-existent file: ["01.flac"]
: shnjoin: error: could not open file: ["01.flac"]
: mv joined.flac Act1.flac

: Questions:

: 1. Why the two "command not found' messages?

Because you're trying to run the commands '[flac' and '[Act1', which oddly
enough don't exist in your PATH.  What you're missing is the fact that '['
is actually a command, not a piece of syntax to the 'if' statement, and is
usually a hardlink to (/usr)/bin/test.  These days it's a shell built-in,
but it's still a command, and needs to be treated as such.

Your lines should look like:

if [ "x$1" = "x" ]; then
...

as otherwise you'll get an error from strict shells complaining that '='
is a binary operator, and you've only passed it one argument.

'if' is a shell statement,
'[' is the test command,
'x$1' is the first parameter,
'=' is the second parameter,
'""' is a null, so produces an error; '"x"' doesn't, and strictly doesn't
	need the quotes,
']' is the end-of-parameter-list marker for '[',
';' is the shell end-of-command marker.

: 2. Why the "cannot open non-existent file" warnings when the files do indeed
: exist in the current directory (from where I launch the command), which by
: the way is in my home folder?

Becaue you've tried to protect yourself against spaces by quoting the
arguments.  Those are being passed to the command as the strings
'"01.flac"', not '01.flac', which isn't what you want.

: I should add that if I 'echo' the shnjoin line and then copy and paste the
: resulting echoed output at the prompt, it executes correctly - just not from
: the script. eg

: shnjoin -o flac "01.flac" "02.flac"

: works fine if I do it myself.

Yes, because the shell is stripping the '"' characters before passing them
to the command, as it should.

: It's almost as if the script executes in the context of its own directory,
: not the directory where I launch it, which would explain why it cannot find
: the files listed in the $fileList variable.

No.

-- 
Dickon Hood

Due to digital rights management, my .sig is temporarily unavailable.
Normal service will be resumed as soon as possible.  We apologise for the
inconvenience in the meantime.

No virus was found in this outgoing message as I didn't bother looking.



More information about the Sclug mailing list