This is the mail archive of the cygwin-talk mailing list for the cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

bash scripting nightmare.


    Hey all, who's any good at bash scripting?

  I've been trying for two-and-a-half hours now to make a fairly simple shell
script work, and I've been through every imaginable combination of quoting,
and nothing I've tried works.  The bash FAQ doesn't have anything to say on
the matter, nor could I figure it out from the Advanced Bash-Scripting Guide.

  I'm not a proud man, so I don't mind begging for help.  The trouble is that
I want to:

a) declare a variable which contains the date in ANSI format, then 
b) concatenate that variable and a few others into another variable, then
c) pass that other variable to a function, then finally
d) invoke the command line specified in the variable within the function.

  Sounds easy, you think?  Not quite.  There's a catch: the date string
contains a space in it.  And I can't find any way at all to get that date
string passed to the command line from within the function without that space
breaking things totally.  If I don't try and quote it, the time part of the
datestamp is taken as a separate command line argument and cvs doesn't
understand it.  If I do try and quote it, bash mangles the variable into
gibberish.  Let me demonstrate:


--------------------------------<snip>--------------------------------
#!/bin/bash
function runCvsCommand()
{
# Execute a cvs command, logging appropriately and handling errors.
echo "CVS: ${1}"

# this can take some time
echo "(cd .; ${1})"
(cd .; ${1})
RET=$?

if [ $RET -ne 0 ]
then
	echo "CAN'T '$1' STATUS $RET"
	exit 12
fi

echo "CHECKOUT $1 TO DIRECTORY $NEWDIR OK"
}

CVSCOM="cvs co"
CVSDATE="`date +'%Y-%m-%d %H:00:00'`"

CVSOPTS="-D \"${CVSDATE}\" -P" # CVSDATE has a space so must be quoted.

CVSFILES1="ieee usb"
CVSFILES2="pci wimedia"

CVSCMD1="${CVSCOM} ${CVSOPTS} ${CVSFILES1}"
CVSCMD2="${CVSCOM} ${CVSOPTS} ${CVSFILES2}"

echo "OK... CMD1 IS ${CVSCMD1} "
runCvsCommand "${CVSCMD1}"
echo "OK;; CMD2 IS ${CVSCMD2} "
runCvsCommand "${CVSCMD2}"
--------------------------------<snip>--------------------------------

  And here's what I see:

--------------------------------<snip>--------------------------------
OK... CMD1 IS cvs co -D "2006-08-10 18:00:00" -P ieee usb
CVS: cvs co -D "2006-08-10 18:00:00" -P ieee usb
(cd .; cvs co -D "2006-08-10 18:00:00" -P ieee usb)
cvs [checkout aborted]: Can't parse date/time: "2006-08-10
CAN'T 'cvs co -D "2006-08-10 18:00:00" -P ieee usb' STATUS 1
--------------------------------<snip>--------------------------------

  If I remove the escapes on the quotes in the CVSOPTS assignment, or remove
the quotes altogether, I get

--------------------------------<snip>--------------------------------
OK... CMD1 IS cvs co -D 2006-08-10 18:00:00 -P ieee usb
CVS: cvs co -D 2006-08-10 18:00:00 -P ieee usb
(cd .; cvs co -D 2006-08-10 18:00:00 -P ieee usb)
cvs server: cannot find module `18:00:00' - ignored
cvs server: cannot find module `-P' - ignored
cvs server: cannot find module `ieee' - ignored
cvs server: cannot find module `usb' - ignored
cvs [checkout aborted]: cannot expand modules
CAN'T 'cvs co -D 2006-08-10 18:00:00 -P ieee usb' STATUS 1
--------------------------------<snip>--------------------------------

  I've tried about a million combinations now and I'm getting nowhere.  I even
tried replacing the in the date with "__space__" and using "${1//__space__/ }"
in the function invocation but that didn't help either.

  I'm just not getting it.  Can anyone explain it to me?



    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]