Re: Grepping for inputted variables

From: Icarus Sparry (usenet_at_icarus.freeuk.com)
Date: 01/30/04


Date: Fri, 30 Jan 2004 03:28:31 GMT

On Thu, 29 Jan 2004 19:37:00 +0000, Michael Tosch wrote:

> In article <5f8f7d6d.0401290929.4c077b38@posting.google.com>, mckrs@hotmail.com (Garbunkel) writes:

[SNIPPED script, which looks to be based on article
        <pan.2004.01.29.03.54.56.294996@icarus.freeuk.com>]

> 1. avoid echo -n,
> use a here document instead:
> cat <<_EOT
> This will
> ...
> Enter path to search from
> _EOT

There is no reason to avoid 'echo -n' except for portability. It is certainly
more efficient that using cat and a here document on any shell which has a
builtin 'echo'.

> 2. avoid find -print | xargs,
> it doesnt handle spaces in filenames. (Nowadays there seems to be
> an unwritten rule "must contain a space".)
> Better use:
> find ${dir-.} -type f -name "*$ext" -exec fgrep -f $filename {} \;

The whole reason for using xargs is to increase efficiency. If your 'find'
and 'xargs' support it, then using '-print0' and '-0' avoids the problem
with spaces and still gives you the speed. Of course sensible operating
systems avoid the problem by not allowing you to put ASCII space
characters in the filename in the first place (but you can still have
Unicode blanks if you have to call your file "Dear Sir.doc").

> 3. IMHO your problem:
> cat > $filename
> reads from the shell's input. I am not an expert with this, but it
> might lead to some conflict.
> (Maybe it is even a bug in your OS.)

Yes it does read from the shells input, which is exactly what is required.

> It is safer (and uglier) to explicitly read from the tty input,
> in Unix this is:
> cat < /dev/tty > $filename
> and
> read ext < /dev/tty

Only if you know that you are always going to want to drive this from a
keyboard (or pty). Of course it might have been better to use

sed '/^\.$/q' > $filename

and amended the instructions to say 'end with a line containing just a
dot, or

sed '/^$/q' > $filename

and end with a blank line, rather than using EOF from the input.
Icarus



Relevant Pages