FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 04-24-2008, 03:52 PM
michael
 
Default scripting - how to handle blanks

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.

Ta, Michael


#!/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
echo Looping until
echo " '${STRING}'"
echo found in $FILE

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



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-24-2008, 04:11 PM
Ron Johnson
 
Default scripting - how to handle blanks

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 04/24/08 10:52, michael 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.
>
> Ta, Michael
>
>
> #!/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
> echo Looping until
> echo " '${STRING}'"
> echo found in $FILE
>
> while [[ `grep -e ${STRING} ${FILE} |wc -l` -lt 1 ]];do
> echo nope
> sleep 60
> done

What happens when you do this?
$ grep -e "After NEXTIME"
UK108_N2O5aeroHet_varGamma-hybridCMU_INI.log

- --
Ron Johnson, Jr.
Jefferson LA USA

We want... a Shrubbery!!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIELEyS9HxQb37XmcRAildAJ9v8S9UaFRQBc9vm0YPsf mPbNJlEQCg3ope
roY1MD6LY97JVHTj6CDBwr4=
=/9VP
-----END PGP SIGNATURE-----


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-24-2008, 04:25 PM
michael
 
Default 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$



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-24-2008, 04:36 PM
"Owen Townend"
 
Default scripting - how to handle blanks

On 25/04/2008, michael <cs@networkingnewsletter.org.uk> wrote:

while [[ `grep -e ${STRING} ${FILE} |wc -l` -lt 1 ]];do
**This should fix it:
while [[ `grep -e "${STRING}" "${FILE}" |wc -l` -lt 1 ]];do

**The shell still replaces variable within "" quotes, but not within '.

**e.g. this:
$*TEST="Testing 1 2 3"
$*echo*"Single:*" '${TEST}'*
$ echo "Double: " "${TEST}"
**Returns this:
Single: *${TEST}
Double: *Testing 1 2 3


**Or in a script like your case:
$*cat tmp.sh
#!/bin/sh
grep -e "$1" "$2"

**Running this:
$ echo "1 2 3 4 5" > tmp.txt
$ ./tmp.sh "1 2 3" tmp.txt
**Returns the line.


cheers,
Owen.


****echo nope
****sleep 60
done




--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-26-2008, 07:24 PM
 
Default scripting - how to handle blanks

michael wrote:
> echo Usage: $0 [string file]
> echo To loop until $string found in $file

Better to quote the entire line. Because [...] is special to the
shell the [string file] will try to file glob match against files in
the local directory. It is unlikely to match in this case but it will
slow down and on a networked directory may be very slow and in other
cases it might actually match creating a directory data dependent
failure mode. And besides, ':' doesn't need to be quoted there.

Also the synopsis line would normally be written without requiring the
user to know that $variable is a variable. They don't need to know
that the implementation is a shell script. It might be C or Ruby.

echo "Usage: $0 [STRING FILE]"
echo "To loop until STRING found in FILE."

> while [[ `grep -e "${STRING}" ${FILE} |wc -l` -lt 1 ]];do

Using grep piped to wc -l is the same as using grep -c. In many cases
the savings in character I/O of piping large amounts of output from
one command to the next can be very large. And it is just a good
idea. And since you are using the new style [[...]] already you
might as well use the new style $(...) with the more regular and
easier to understand quoting rules.

while [[ $(grep -c -e "${STRING}" ${FILE}) -lt 1 ]];do

Bob
 

Thread Tools




All times are GMT. The time now is 03:19 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org