On Tue, Nov 13, 2007 at 03:32:39PM +0000, cs wrote:
> Folks, I have a bash script and I wanted to be able to catch a situation
> and then send an email to the user flagging this. However, the -s to
> `mail` doesn't allow spaces and I cannot work out, despite several
> attempts, how to escape/quote etc in order to do what I want. Here's
> what I'd like to do:
> 
> #!/bin/bash -x
> TEMPFILE=`mktemp /tmp/chk_procs_XXX`
> trap 'date|mail -s "$0 error" [EMAIL PROTECTED];echo Error - aborting;\
> exit' ERR
> 
> ### only allow one instance of this per user to run at a given time
> ps -elf|grep $0|grep -v grep > $TEMPFILE
> if [[ `cat $TEMPFILE | wc -l` -gt 1 ]]; then 
>     #cat $TEMPFILE
>     date | mail -s '${0} already Running' [EMAIL PROTECTED]
>     rm $TEMPFILE
>     exit -1
> fi
> rm $TEMPFILE
> 
> 
> but that mails the string $0 not the value. Removing the quotes results
> in an error.
> 
> Any ideas?
> Thanks, M

The convention in Bash (also other shells, Perl, etc.) is to use double
quotes (") and single quotes (') to group a bunch of text into a single
token, so one or the other (*) is needed to have a single value to go with
the -s option in your example.  Inside double quotes, though, shell variables
and \ escapes are expanded, so that would give you the result you're after.

There's a section in the bash(1) manpage on quoting that covers this in 
typically excrutiating detail...

(*) You can alternatively escape the spaces in your example by preceding
them with a backslash, e.g,.

      date | mail -s ${0}\ already\ Running [EMAIL PROTECTED]

would also work.

Ken

-- 
Ken Irving, [EMAIL PROTECTED]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to