Redirection and the Bourne Again Shell (BASH)

  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

Can a #!/bin/bash script accept input from command line redirection? For example:

myscript.sh < slocate textstring

I had hoped to write a nifty little script that deleted files returned from locate, find, etc.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

From the bash manual:

Code: [ Select ]
    read [-ers] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
       One line is read from the standard input, and the first word is assigned to the first
       name, the second word to the second name, and so on, with leftover words and their
       intervening separators assigned to the last name. If there are fewer words read from
       the standard input than names, the remaining names are assigned empty values. The
       characters in IFS are used to split the line into words. The backslash character (\)
       may be used to remove any special meaning for the next character read and for line
       continuation. Options, if supplied, have the following meanings:
       -a aname
           The words are assigned to sequential indices of the array variable aname,
           starting at 0. aname is unset before any new values are assigned. Other name
           arguments are ignored.
       -d delim
           The first character of delim is used to terminate the input line, rather than
           newline.
       -e   If the standard input is coming from a terminal, readline (see READLINE above)
           is used to obtain the line.
       -n nchars
           read returns after reading nchars characters rather than waiting for a comĀ­
           plete line of input.
       -p prompt
           Display prompt, without a trailing newline, before attempting to read any
           input. The prompt is displayed only if input is coming from a terminal.
       -r   Backslash does not act as an escape character. The backslash is considered to
           be part of the line. In particular, a backslash-newline pair may not be used
           as a line continuation.
       -s   Silent mode. If input is coming from a terminal, characters are not echoed.
       -t timeout
           Cause read to time out and return failure if a complete line of input is not
           read within timeout seconds. This option has no effect if read is not reading
           input from the terminal or a pipe.

       If no names are supplied, the line read is assigned to the variable REPLY.  The
       return code is zero, unless end-of-file is encountered or read times out.
  1.     read [-ers] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
  2.        One line is read from the standard input, and the first word is assigned to the first
  3.        name, the second word to the second name, and so on, with leftover words and their
  4.        intervening separators assigned to the last name. If there are fewer words read from
  5.        the standard input than names, the remaining names are assigned empty values. The
  6.        characters in IFS are used to split the line into words. The backslash character (\)
  7.        may be used to remove any special meaning for the next character read and for line
  8.        continuation. Options, if supplied, have the following meanings:
  9.        -a aname
  10.            The words are assigned to sequential indices of the array variable aname,
  11.            starting at 0. aname is unset before any new values are assigned. Other name
  12.            arguments are ignored.
  13.        -d delim
  14.            The first character of delim is used to terminate the input line, rather than
  15.            newline.
  16.        -e   If the standard input is coming from a terminal, readline (see READLINE above)
  17.            is used to obtain the line.
  18.        -n nchars
  19.            read returns after reading nchars characters rather than waiting for a comĀ­
  20.            plete line of input.
  21.        -p prompt
  22.            Display prompt, without a trailing newline, before attempting to read any
  23.            input. The prompt is displayed only if input is coming from a terminal.
  24.        -r   Backslash does not act as an escape character. The backslash is considered to
  25.            be part of the line. In particular, a backslash-newline pair may not be used
  26.            as a line continuation.
  27.        -s   Silent mode. If input is coming from a terminal, characters are not echoed.
  28.        -t timeout
  29.            Cause read to time out and return failure if a complete line of input is not
  30.            read within timeout seconds. This option has no effect if read is not reading
  31.            input from the terminal or a pipe.
  32.        If no names are supplied, the line read is assigned to the variable REPLY.  The
  33.        return code is zero, unless end-of-file is encountered or read times out.
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

I bought a book on Bash programming and it doesn't mention read once. :-(

Thanks much for the suggestions. :-)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 84 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.