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

Re: 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

Attachment: signature.asc
Description: Digital signature


Reply to: