How to read redirected input?

How to read redirected input?

Post by Henrik Lynner » Tue, 02 Mar 1999 04:00:00



I would like to read and process the lines from a file redirected into
the script, that is processing the lines, but I have tried without
luck.

If for example the script is invoked like this:

        thescript < theinputfile

I would like to process the lines (not a binary input file) either one
line at a time or the file as a hole:

        while read line
        do
                process theline
        done

or

        cp thefile anotherfile

Anybody out there with a snip / example that shows it.

A/S KORN- OG FODERSTOF KOMPAGNIET

Henrik Lynnerup

 
 
 

How to read redirected input?

Post by Barry Margoli » Tue, 02 Mar 1999 04:00:00




>I would like to read and process the lines from a file redirected into
>the script, that is processing the lines, but I have tried without
>luck.

It should show up as the script's standard input.  File redirection
automatically connects the file to the process's standard input.

Quote:>If for example the script is invoked like this:

>    thescript < theinputfile

>I would like to process the lines (not a binary input file) either one
>line at a time or the file as a hole:

>    while read line
>    do
>            process theline
>    done

That should work.  What problem have you had?

Quote:>or

>    cp thefile anotherfile

I'm not sure what you mean by this.  What does this have to do with
redirected input?  Do you mean:

        cp theinputfile anotherfile

If so, you can do that with:

        cat > anotherfile

The "cat" command will read its standard input, which is connected to
theinputfile because of the redirection, and copy it to its standard
output, which is redirected to anotherfile.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to read redirected input?

Post by Henrik Lynner » Tue, 02 Mar 1999 04:00:00


On Mon, 01 Mar 1999 19:17:40 GMT, Barry Margolin




>>I would like to read and process the lines from a file redirected into
>>the script, that is processing the lines, but I have tried without
>>luck.

>It should show up as the script's standard input.  File redirection
>automatically connects the file to the process's standard input.

>>If for example the script is invoked like this:

>>        thescript < theinputfile

>>I would like to process the lines (not a binary input file) either one
>>line at a time or the file as a hole:

>>        while read line
>>        do
>>                process theline
>>        done

>That should work.  What problem have you had?

My problem is that I don't see the output on the tty ie. I would like
to copy my input, redirected from another program to the users current
tty:

                echo theline > `tty`

- Show quoted text -

Quote:>>or

>>        cp thefile anotherfile

>I'm not sure what you mean by this.  What does this have to do with
>redirected input?  Do you mean:

>    cp theinputfile anotherfile

>If so, you can do that with:

>    cat > anotherfile

>The "cat" command will read its standard input, which is connected to
>theinputfile because of the redirection, and copy it to its standard
>output, which is redirected to anotherfile.

I have tried this too but with out any luck. The script is simple;

1) instruct the terminal emulator that it should grap the the terminal
output and direct it to the users local printer (is working)

2) redirect the input to terminal (cat > `tty`)

3) instruct the terminal emulator that it should no longer grap the
the terminal output and direct it to the users local printer (is
working)

the script is working if the inputfile is a parameter to script and I
reference $1 but this is not how the implementation must work!

-
Henrik

-
:Lynnerup Data
:Henrik Lynnerup
:lynnerup_at_post9.tele.dk

 
 
 

How to read redirected input?

Post by Kurt J. Lanz » Tue, 02 Mar 1999 04:00:00



> I would like to read and process the lines from a file redirected into
> the script, that is processing the lines, but I have tried without
> luck.

> If for example the script is invoked like this:

>         thescript < theinputfile

> I would like to process the lines (not a binary input file) either one
> line at a time or the file as a hole:

>         while read line
>         do
>                 process theline
>         done

> or

>         cp thefile anotherfile

> Anybody out there with a snip / example that shows it.

I don't see your problem. That's exactly how it's done (with the
Bourne shell -- others may vary). Start with:

        while read L
        do
                echo $L
        done

Expand from there once you have convinced yourself that unix and
Bourne work as advertised.

 
 
 

How to read redirected input?

Post by Henrik Lynner » Tue, 02 Mar 1999 04:00:00





>> I would like to read and process the lines from a file redirected into
>> the script, that is processing the lines, but I have tried without
>> luck.

>> If for example the script is invoked like this:

>>         thescript < theinputfile

>> I would like to process the lines (not a binary input file) either one
>> line at a time or the file as a hole:

>>         while read line
>>         do
>>                 process theline
>>         done

>> or

>>         cp thefile anotherfile

>> Anybody out there with a snip / example that shows it.

>I don't see your problem. That's exactly how it's done (with the
>Bourne shell -- others may vary). Start with:

>    while read L
>    do
>            echo $L
>    done

I've done that before (but I did it again; exactly as above) :
----------------------
#!/bin/sh
 while read L
 do
    echo $L
done
-----------------------

I was expecting to see the output on my terminal but nothing happend.

Quote:>Expand from there once you have convinced yourself that unix and
>Bourne work as advertised.

-
:Lynnerup Data
:Henrik Lynnerup
:lynnerup_at_post9.tele.dk

 
 
 

How to read redirected input?

Post by Barry Margoli » Tue, 02 Mar 1999 04:00:00




>My problem is that I don't see the output on the tty ie. I would like
>to copy my input, redirected from another program to the users current
>tty:

You shouldn't need to do anything special for that.  If output isn't
redirected, it should go to the terminal.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to read redirected input?

Post by Henrik Lynner » Wed, 03 Mar 1999 04:00:00


On Mon, 01 Mar 1999 20:37:12 GMT, Barry Margolin


>You shouldn't need to do anything special for that.  If output isn't
>redirected, it should go to the terminal.

Well it does not. This is what I do inside the script:

------->
#!/bin/ksh
echo "\033[5i"                <----------- *1
cp $1 `tty`             <----------- *2
echo "\033[4i"                <----------- *3
<-------------

1) Terminal emulator instructions - is working
2) this works if I put filename as parameter to script
3) Terminal emulator instructions - is working

The above script is working when called like:

        thescript inputfile

but I haven't found a way of changing line 2 to something that works
if I call the script like:

        thescript < inputfile

Help!

TIA

Henrik

 
 
 

How to read redirected input?

Post by Kurt J. Lanz » Wed, 03 Mar 1999 04:00:00



> On Mon, 01 Mar 1999 20:37:12 GMT, Barry Margolin

> >You shouldn't need to do anything special for that.  If output isn't
> >redirected, it should go to the terminal.

> Well it does not. This is what I do inside the script:

> ------->
> #!/bin/ksh
> echo "\033[5i"          <----------- *1
> cp $1 `tty`             <----------- *2
> echo "\033[4i"          <----------- *3
> <-------------

> 1) Terminal emulator instructions - is working
> 2) this works if I put filename as parameter to script
> 3) Terminal emulator instructions - is working

> The above script is working when called like:

>         thescript inputfile

> but I haven't found a way of changing line 2 to something that works
> if I call the script like:

>         thescript < inputfile

It took me quite a few reads of your message to figurre out what
your problem is. Just change line 2 to read:

        cat $1

If you run with no argument it will take standard input. RTFM about
how the shell works, and think about the difference between 'cp' and
'cat'.

 
 
 

How to read redirected input?

Post by bmar.. » Wed, 03 Mar 1999 04:00:00



   >I would like to read and process the lines from a file redirected
   >into the script, that is processing the lines, but I have tried
   >without luck.
   >If for example the script is invoked like this:
   >thescript < theinputfile
   >I would like to process the lines (not a binary input file) either
   >one line at a time or the file as a hole:
   >while read line
   >do
   >process theline
   >done
   >or
   >cp thefile anotherfile
   >Anybody out there with a snip / example that shows it.

If you want to do both in the same script, you could have the file name
as an argument instead of redirecting to the script, or you could use
tee, for example:
tee anotherfile < theinputfile | thescript

Net-Tamer V 1.08X - Test Drive

 
 
 

How to read redirected input?

Post by Henrik Lynner » Thu, 04 Mar 1999 04:00:00




>   >I would like to read and process the lines from a file redirected
>   >into the script, that is processing the lines, but I have tried
>   >without luck.
>   >If for example the script is invoked like this:
>   >thescript < theinputfile
>   >I would like to process the lines (not a binary input file) either
>   >one line at a time or the file as a hole:
>   >while read line
>   >do
>   >process theline
>   >done
>   >or
>   >cp thefile anotherfile
>   >Anybody out there with a snip / example that shows it.

>If you want to do both in the same script, you could have the file name
>as an argument instead of redirecting to the script, or you could use
>tee, for example:
>tee anotherfile < theinputfile | thescript

I don't want to do both I want to do one of the methods but non are
working - I have to deal wiyh the inputfile as a redirected input
because of the way the input is delivered from the other program.

-
Henrik

 
 
 

How to read redirected input?

Post by Henrik Lynner » Thu, 04 Mar 1999 04:00:00





>> On Mon, 01 Mar 1999 20:37:12 GMT, Barry Margolin

>> >You shouldn't need to do anything special for that.  If output isn't
>> >redirected, it should go to the terminal.

>> Well it does not. This is what I do inside the script:

>> ------->
>> #!/bin/ksh
>> echo "\033[5i"          <----------- *1
>> cp $1 `tty`             <----------- *2
>> echo "\033[4i"          <----------- *3
>> <-------------

>> 1) Terminal emulator instructions - is working
>> 2) this works if I put filename as parameter to script
>> 3) Terminal emulator instructions - is working

>> The above script is working when called like:

>>         thescript inputfile

>> but I haven't found a way of changing line 2 to something that works
>> if I call the script like:

>>         thescript < inputfile

>It took me quite a few reads of your message to figurre out what
>your problem is. Just change line 2 to read:

>    cat $1

>If you run with no argument it will take standard input. RTFM about
>how the shell works, and think about the difference between 'cp' and
>'cat'.

The script that you see above is working as it is but as you can see
$1 is the filename as a parameter to the script. What I need is to
change line 2 so the script will do the same when inputfile is
redirected to the script instead of just a parameter.

-
Henrik

 
 
 

How to read redirected input?

Post by Kurt J. Lanz » Thu, 04 Mar 1999 04:00:00





> >> ------->
> >> #!/bin/ksh
> >> echo "\033[5i"          <----------- *1
> >> cp $1 `tty`             <----------- *2
> >> echo "\033[4i"          <----------- *3
> >> <-------------

> >> 1) Terminal emulator instructions - is working
> >> 2) this works if I put filename as parameter to script
> >> 3) Terminal emulator instructions - is working

> >> The above script is working when called like:

> >>         thescript inputfile

> >> but I haven't found a way of changing line 2 to something that works
> >> if I call the script like:

> >>         thescript < inputfile

> >It took me quite a few reads of your message to figurre out what
> >your problem is. Just change line 2 to read:

> >       cat $1

> >If you run with no argument it will take standard input. RTFM about
> >how the shell works, and think about the difference between 'cp' and
> >'cat'.

> The script that you see above is working as it is but as you can see
> $1 is the filename as a parameter to the script. What I need is to
> change line 2 so the script will do the same when inputfile is
> redirected to the script instead of just a parameter.

As I said -- learn how the shell and 'cat' work. If there is a
parameter, "cat $1" reads the named file. If not, the shell changes
"cat $1" to plain old 'cat' (since there ain't no *y $1), and
 plain old 'cat' reads standard input. Now  go back and RTFM again.
 
 
 

How to read redirected input?

Post by Kevin Ducket » Sun, 07 Mar 1999 04:00:00


You have to treat the input file as an argument to the script.
Within the script you would use "$1" to indicate the input file as the
first argument.

-kd





> >   >I would like to read and process the lines from a file redirected
> >   >into the script, that is processing the lines, but I have tried
> >   >without luck.
> >   >If for example the script is invoked like this:
> >   >thescript < theinputfile
> >   >I would like to process the lines (not a binary input file) either
> >   >one line at a time or the file as a hole:
> >   >while read line
> >   >do
> >   >process theline
> >   >done
> >   >or
> >   >cp thefile anotherfile
> >   >Anybody out there with a snip / example that shows it.

> >If you want to do both in the same script, you could have the file name
> >as an argument instead of redirecting to the script, or you could use
> >tee, for example:
> >tee anotherfile < theinputfile | thescript

> I don't want to do both I want to do one of the methods but non are
> working - I have to deal wiyh the inputfile as a redirected input
> because of the way the input is delivered from the other program.

> -
> Henrik

 
 
 

How to read redirected input?

Post by Reinier Pos » Mon, 08 Mar 1999 04:00:00



>>It took me quite a few reads of your message to figurre out what
>>your problem is. Just change line 2 to read:

>>        cat $1

>>If you run with no argument it will take standard input. RTFM about
>>how the shell works, and think about the difference between 'cp' and
>>'cat'.

>The script that you see above is working as it is but as you can see
>$1 is the filename as a parameter to the script. What I need is to
>change line 2 so the script will do the same when inputfile is
>redirected to the script instead of just a parameter.

That's exactly what it does!

You don't understand how redirection works.  It is performed by the
calling shell, not by your script.  If you call a script using

  thescript < file

the script will behave as if input was supplied from the terminal.
It does not know that input is really coming from the file 'file'.
That's something the shell takes care of before calling 'thescript'.

Now when you put

  cat $1

and no $1 has been supplied, cat will read from standard input,
just like you want.  Yes, you do.  Now please read those lovely
manpages about your shell and about cat.

As a matter of fact, shell scripts that use 'cp' on input files are
broken.  Signs of DOS disease.

Quote:>Henrik

--
Reinier
 
 
 

How to read redirected input?

Post by Reinier Pos » Mon, 08 Mar 1999 04:00:00



>I've done that before (but I did it again; exactly as above) :
>----------------------
>#!/bin/sh
> while read L
> do
>    echo $L
>done
>-----------------------

>I was expecting to see the output on my terminal but nothing happend.

If you called this script as 'thescript < filename', and the filewasn't
empty, something strange is going on.  If you just called the script
without any redirection, it was waiting for you to supply input.

--
Reinier

 
 
 

1. bash "read -a" ignores redirected input -- why?

Hi --

  I'm trying to write a bash file that copies "du" output to a bash
array variable, each line becoming a separate array element.  I've
poured over the bash documentation in info and man, and I've studied
the excellent article at

http://www.tldp.org/LDP/abs/html/

The only one-step approach I can find uses the "read -a Array"
command.  However, I can't get the "read" to accept redirected output
from the "du" (or from anything).  For example, when I run the
following code,

read  vText
echo $vText | read -a vArrT      # Redirect vText content to vArrT
elements

echo Text: $vText
echo Reply length: ${#REPLY}

echo ArrT length: ${#vArrT[0]}

and respond with AAA BB CCCC D, I get the following echo messages:

Text: AAA BB CCCC D
ArrT:
ArrT count: 0
ArrT length: 0
Reply length: 0

What am I doing wrong?  IS there a one-step way to initialize an array
from command output?

2. Unable to download more then 6 gb file from internet via squid

3. Any way to redirect /dev/tty input to read from a file?

4. Apache and content negotiation?

5. Redirecting input to a file - echo the input to screen/file?

6. Thanks for the awk help!

7. Redirecting input

8. how to repair an ext2fs

9. Redirecting input and output to serial port

10. Redirecting user input from the console

11. Redirecting Serial port input to a file

12. Redirecting two input files to diff..

13. Redirect input to telnet