Re: what is wrong here?

2012-03-28 Thread mhenn
Am 08.08.2011 11:33, schrieb Francky Leyn:
> Hello,
> 
> consider the following code:
> 
> EXTENSION=jpg
> INPUT_FILES=*.$EXTENSION

echo "$INPUT_FILES" #obviously wrong
#instead maybe
INPUT_FILES="$(ls *.$EXTENSION)"

> 
> if [ -z "$INPUT_FILES" ]; then
>   echo "No .$EXTENSION input files in this directory"
>   exit -1
> fi
> 
[...]
> 
> What is wrong here?
> 
> Best regards,
> 
> Francky Leyn

I think there are even better ways to do this, but this is one that came
to my mind at the moment.

btw:

man bash | less -p 'Pathname expansion'



Re: what is wrong here?

2012-03-28 Thread mhenn
Am 08.08.2011 14:17, schrieb mhenn:
> Am 08.08.2011 11:33, schrieb Francky Leyn:
>>[...]
> 
> echo "$INPUT_FILES" #obviously wrong
> #instead maybe
> INPUT_FILES="$(ls *.$EXTENSION)"
> 
>> [...]

That actually wasn't such a good advice:
<http://mywiki.wooledge.org/ParsingLs>

Better use find or or a for loop like this:

for file in *.$EXTENSION; do
...
#but attention here: if no such a file exists,
# $file is "*.jpg" (with a literal asterisk)

#maybe break after the first found file?
done


Re: what is wrong here?

2012-03-28 Thread mhenn
Am 08.08.2011 15:22, schrieb Francky Leyn:
> On 8/8/2011 2:17 PM, mhenn wrote:
>> Am 08.08.2011 11:33, schrieb Francky Leyn:
>>> Hello,
>>>
>>> consider the following code:
>>>
>>> EXTENSION=jpg
>>> INPUT_FILES=*.$EXTENSION
>>
>> echo "$INPUT_FILES" #obviously wrong
>> #instead maybe
>> INPUT_FILES="$(ls *.$EXTENSION)"
> 
> I tried something similar:
> 
> INPUT_FILES=`ls *.$EXTENSION`
> 
> This worked when there were files.
> But when there were no matching files, it didn't.
> In that case the words of the ls error message were
> considered as files, which is obviously wrong.
> 
> I'm new to bash, and have never seen a construct like yours.
> Could you briefly explain what the dollar and the () mean (subshell)?

`` also creates a subshell :)
and $() actually does the same. See
man bash | less -p '\$\(command'

> 
> Btw: how can I get rid off the ls error message in your construct?
> I tried already by postponing with >/dev/null, and 2>/dev/null, but
> that all doesn't help. Is ls writting to /dev/tty? and in that case,
> how can I suppress the ls error message?

INPUT_FILES="$(ls *.$EXTENSION 2>/dev/null)"
if [ -z "$INPUT_FILES" ]; then echo "no such file"; fi

> 
> Anyway, your construct works fine for files without spaces or newlines.
> Now up to the link you have me.
> 
> Thanks for the help!

np



Re: how to write this in bash

2012-03-28 Thread mhenn
Am 05.08.2011 17:47, schrieb Maverick:

> I also tried already
> 
> $CMD || echo 'Error...' && exit -1

see
man bash | less -p Compound

$CMD || { echo 'Error...'; exit -1; }