LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH scripting: confused about redirection & file descriptors (https://www.linuxquestions.org/questions/programming-9/bash-scripting-confused-about-redirection-and-file-descriptors-190632/)

funkymunky 06-07-2004 06:27 AM

BASH scripting: confused about redirection & file descriptors
 
hello!

i horribly confused with this redirection business..iv tried hard, but the nail refuses to go into the wall..
:(
i am working with two virtual terminals, /dev/pts/0 and /dev/pts/1
i do this:

echo "hello" >/dev/pts/0

from pts/1, and a "hello" appears on pts/0 as expected..what i want is that whatever appears on pts/0 should go into a file, from where it can be read..
i tried this

exec 1<"/dev/pts/0" 1>&"tempfile"

is this wrong? im a :newbie: to shell scripting, so plz accomodate :D

also, how do file descriptors work? i mean, when a file descriptor is associated with a file, what exactly does it mean?

in the place of whats typed above, is this correct:

4<>"tempfile"
exec 4<"/dev/pts/0"
HELP !!
:( :confused:


(actually, what im trying to do is that i hav a small script to send a command to /pts/0 from /pts/2, and then on the basis of the output received. check whether the command was succesful..all through the script.)

ilikejam 06-07-2004 07:47 AM

Hi.

There's a utility called 'tee' which allows you to send output to several files, and std out. See 'man tee' for details. I don't think it will make a lot of difference, as I can't get echoed commands to 'press return' and therefore execute the command.

There is a way around this, though. I don't even know if this is what you're looking for, but here goes.

Do the following:
Open two xterms (konsole, or whatever). In one of them, do:
$mkfifo fifo1
$touch fifoout
$chmod +x fifoout

Now, type in the following into the terminal you want to recieve commands:
$while true; do cat fifo1 > fifoout; ./fifoout | tee output; done;
and hit return. You shouldn't get any output from that console at this point - it should appear to have 'crashed'. This is good. Because it's a fifo pipe, and not a file that's being cat'ed, it doesn't use any processor time - it just waits for a command.

In the other terminal, echo your commands to the fifo1 file, e.g.:
$echo "who" > fifo1
The result of 'who' should appear in the other console and in the file 'output' (the output file gets wiped before each command, so it will only contain the output of the last command, which may, or may not be useful). Should you need a listing of all the outputs of all the commands, then put 'while true; do cat fifo1 > fifoout; ./fifoout; done' in a script and run it with './name_of_script | tee output', instead of running '$while true; do cat fifo1 > fifoout; ./fifoout | tee output; done;' directly in the recieving console as shown above.

As I said, it may not be what you're looking for, and there's probably a really simple solution that I've missed, but it does work.

Dave


All times are GMT -5. The time now is 03:03 AM.