Re: Problem using grep through the unix shell: doesn't accept a regular expression in filename

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/09/04

  • Next message: Juhan Leemet: "Re: Collect errors from a file"
    Date: Tue, 9 Nov 2004 20:21:09 +0000
    
    

    2004-11-9, 11:59(-08), Francois:
    [...]
    > #######
    > #\bin\bash

    Should be:

    #! /bin/bash -

    > grep "mypattern" < $1
    >
    > #######
    >
    > However it works perfectly when i execute it on the command prompt by
    > replacing $1 by *.txt for example, but when I call the script using
    >
    > myscript.sh *.txt
    >
    > The *.txt only takes into account the first file matching the regular
    > expression and not the others! It seems quite weird, does anyone had a
    > similar problem?
    [...]

    It's normal.

    In:

    myscript.sh *.txt

    The shell parsing that command line expands *.txt to the list of
    .txt files in the current directory and passes as many arguments
    to myscript.sh.

    Moreover, the "<" operator accepts only one file (it opens a
    file for reading, and affects that to the standard input of grep,
    the standard input is one file descriptor, there is only one
    standard input, so you can have only one file open. If you want
    the concatenation of the files, then use cat), so it's no use
    trying to pass several ones.

    What you probably want to do is:

    #! /bin/bash -
    exec grep 'mypattern' -- "$@"

    "$@" means the list of arguments verbatim (not just the first
    one).

    Or, if you want grep to work on its standard input being the
    concatenation of the files:

    #! /bin/bash -
    cat -- "$@" | grep 'mypattern'

    (but beware you'll have troubles for files named '-')

    (note that with both solutions, if the script is not given any
    argument, then the script (actually grep or cat) will read from
    its standard input).

    -- 
    Stephane
    

  • Next message: Juhan Leemet: "Re: Collect errors from a file"

    Relevant Pages

    • Re: Importing selected data from a fixed length text file.
      ... downloading the Gnu utilities from http://unxutils.sourceforge.net/ ... Then use a command like this at a command prompt to run grep and copy ... each type containing diffrent field information and sizes. ...
      (microsoft.public.access.externaldata)
    • Re: strict grep when patterns are presented on standard input
      ... > Can strict option/quotes etc. be applied to the grep when it becomes ... > its arguments from standard input in the pipe (i.e. as standard output from ... I can add just another sed command to the pipe ...
      (comp.unix.shell)
    • Re: strict grep when patterns are presented on standard input
      ... >> standard input but both are presented on the standard output ... I can add just another sed command to the pipe ... > Dum question, now I know it. ... -w option of grep will do it. ...
      (comp.unix.shell)
    • Re: strict grep when patterns are presented on standard input
      ... > Can strict option/quotes etc. be applied to the grep when it becomes ... > its arguments from standard input in the pipe (i.e. as standard output from ... I can add just another sed command to the pipe ...
      (comp.unix.shell)
    • Re: windows cmd
      ... batch file, i wanted to know how can i read the values returned by ... command prompt, if any one can help me then please ... You can communicate with a process by standard input, output and error streams. ...
      (comp.lang.java.programmer)