[CentOS] shell script strangeness...

Fri May 16 05:00:02 UTC 2008
Garrick Staples <garrick at usc.edu>

On Fri, May 16, 2008 at 02:07:10PM +0930, Ian Blackwell alleged:
> Craig White wrote:
> >On my CentOS 4.6 server, this works...
> >
> >if [ -z `grep entry_chooser.js /var/log/httpd/access_log` ]
> >
> >On my CentOS 5.1 server, this gives me the following error...
> >
> >./test_file.scr: line 3: [: too many arguments
> >
> >Can anyone explain why the difference and suggest something that makes
> >both cases happy?
> >
> >Thanks
> >
> >Craig
> >
> >_______________________________________________
> >CentOS mailing list
> >CentOS at centos.org
> >http://lists.centos.org/mailman/listinfo/centos
> >
> >  
> Not sure why 4.6 is allowing it, but your if test will be resolving to 
> something like this:-
> if [ -z this line from your log file and another line and even more 
> lines until your if test fails with too many arguments I hope you don't 
> want me to keep typing to make the point ]
> 
> To fix it, wrap the grep instead double quotes as well as the ticks - e.g.
> if [ -z "`grep entry_chooser.js /var/log/httpd/access_log`" ]
> Then the output from the grep will be seen as one long string, not a 
> multitude of words from your log file.

Even better is to use 'grep -q' to avoid possibly large string expansions...
   if grep -q entry_chooser.js /var/log/httpd/access_log ;then

-- 
Garrick Staples, GNU/Linux HPCC SysAdmin
University of Southern California

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.centos.org/pipermail/centos/attachments/20080515/61bd13a5/attachment-0004.sig>