[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Using quotes in bash script parameters



Le mercredi 14 mars 2007 01:04, Marco De Vitis a écrit :
> Hi,
> this is not strictly Debian-related, but I'm doing it on Etch, so... :)
>
> Let's say I'm writing a script like this:
> > #! /bin/bash
> >
> > SUBJECT="This is a test mail"
> > WARNMSG="An error occurred"
> > WARNCMD="mail -s \"${SUBJECT}\" root"
> >
> > echo "echo $WARNMSG | $WARNCMD"
> > echo $WARNMSG | $WARNCMD
>
> Everything works fine if I enter the equivalent command line:
>
> 	echo "An error occurred" | mail -s "This is a test mail" root
>
> A mail is sent to root with subject "This is a test mail" and body "An
> error occurred".
>
> The script should produce the same effect, but instead, when I run it,
> it sends a mail with subect <"This> and body "An error occurred" to the
> following users:
>
> is
> a
> mail
> , root, test
>
> ....although the command line printed by the first echo statement looks
> perfect.
>
> It'a problem which has been bugging me for a while. I usually find some
> workaround, but I'm a bit tired now. An I suppose it should be some kind
> of FAQ... although I couldn't find anything around.
>
> I already tried with single quotes, I tried escaping them, I tried
> escaping the spaces in the subject... to no avail.
>
> Any clues?
> Thanks.

eval does the trick:

SUBJECT="This is a test mail"
WARNMSG="An error occurred"
WARNCMD="mail -s \"${SUBJECT}\" root"
echo "echo $WARNMSG | $WARNCMD"
eval $WARNMSG | $WARNCMD

-- 
Cédric Lucantis



Reply to: