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

Re: scripting - how to handle blanks



On Thu, 2008-04-24 at 16:02 +0000, Mark Clarkson wrote:
> On Thu, 24 Apr 2008 16:52:14 +0100, michael
> <cs@networkingnewsletter.org.uk> wrote:
> > Folks, I wish to do something like the following in a bash script but
> > can't work out the correct incantation of escape chars etc so any advice
> > welcome! ie what is it I need to do for env var STRING and the grep
> > command so that grep -e $STRING handles the space in STRING correctly.
> > 
> 
> Just add double quotes, as in:
> ...
> STRING="After NEXTIME"
> ...
> grep -e "${STRING}" ${FILE} |wc ...
> ...


aha, I'd tried double quotes in STRING def but only single quotes in
grep cmd (which meant it looked literally for ${STRING}) but yes, double
quotes for both works (below) - many thanks! M

~/models-3/MADRID2004/testcases/NRT_March2008/chkRestart$ cat
~/bin/checkDone.sh
#!/bin/bash -x

### script to check when STRING appears in FILE

if [[ $# -eq 0 ]]; then
            STRING="After NEXTIME"
            FILE=UK108_N2O5aeroHet_varGamma-hybridCMU_INI.log
elif [[ $# -eq 2 ]]; then
            STRING=$1
            STRING=$2
else
    echo Usage\: $0 [string file]
    echo To loop until \$string found in \$file
    exit
fi

while [[ `grep -e "${STRING}" ${FILE} |wc -l` -lt 1 ]];do 
    echo nope
    sleep 60
done

~/models-3/MADRID2004/testcases/NRT_March2008/chkRestart$
~/bin/checkDone.sh
+ [[ 0 -eq 0 ]]
+ STRING='After NEXTIME'
+ FILE=UK108_N2O5aeroHet_varGamma-hybridCMU_INI.log
++ grep -e 'After NEXTIME' UK108_N2O5aeroHet_varGamma-hybridCMU_INI.log
++ wc -l
+ [[ 2 -lt 1 ]]

~/models-3/MADRID2004/testcases/NRT_March2008/chkRestart$ 



Reply to: