Visit Our UNIX and Linux User Community


Redirecting stdin from fd 3-9?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting stdin from fd 3-9?
# 1  
Old 09-02-2009
Redirecting stdin from fd 3-9?

Hi
I'm trying to do something on the bash command line that I will later put into a bash shell script.

I'm trying to take a program that reads stdin (using getline) and be able to keep it running in the background and fire "commands" to it. So what I thought I should do was to try taking stdin from (arbitrarily) file descriptor 4:

./myprogram <&4 &

Then later:
echo "inputstring" >&4
then later again
cat somefile >&4

This results is bash telling me "Bad file descriptor" when I start ./myprogram

The whole point of this is to exercise a library that I've written. Its wrapped in a C frontend that takes input from stdin (like a psuedo command line program). It's got to stay alive though so the data in my library under test can stay persistent.


Any idea if this is possible?

Last edited by niceguyeddie; 09-02-2009 at 05:00 PM..
# 2  
Old 09-02-2009
Use a FIFO.
# 3  
Old 09-03-2009
Quote:
Originally Posted by niceguyeddie
Hi
./myprogram <&4 &

Then later:
echo "inputstring" >&4
then later again
cat somefile >&4

This results is bash telling me "Bad file descriptor" when I start ./myprogram

The whole point of this is to exercise a library that I've written. Its wrapped in a C frontend that takes input from stdin (like a psuedo command line program). It's got to stay alive though so the data in my library under test can stay persistent.


Any idea if this is possible?
Um, you can't use a file descriptor that does not exist (hopefully you have prepared your C code better :-)

As mentioned earlier, you should create a FIFO (a special file). Have a look at mknod's manual page (parameter p is what you are looking for).

pen
# 4  
Old 09-03-2009
Actually, it can be done, but it's a bit ugly.

The problem is that when you run the job in the background, it's the background shell that creates fd4 and the parent (your command line shell) will know nothing about it. Because there's no one with fd4 opened for writing, the background job will immediately read EOF and terminate.

Something like this will give you a better shot:
Code:
exec >&4
./myprogram <&4 &
echo "message to program"

The first line redirects stdout to fd4 and leaves it turned on so that future data sent to stdout will go to fd4. (Writing an echo statement afterwards that goes to the real stdout is left as an exercise for the student. Smilie)

Now when you execute myprogram in the background, there will be a fd4 available so you won't get the error message, and fd4 will become stdin for that job.

Now when you print something to stdout it will go to the background job.

Using a FIFO (named pipe) will be similar. The important thing about named pipes is to open the reader first -- you'll get an error if you attempt to open the writer first (EPIPE on many system, "Attempt to write on a pipe with no readers").

If you have to stick with bash, then use one of the above techniques. But you're much better off (if possible) using a HERE document or the Korn shell's coprocess ability.

Previous Thread | Next Thread
Test Your Knowledge in Computers #425
Difficulty: Medium
C# 1.0 was first released in December 2002.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[stdin / stdout] Strategies for redirecting outputs

Well.. let's say i need to write a pretty simple script. In my script i have 2 variables which can have value of 0 or 1. $VERBOSE $LOG I need to implement these cases: ($VERBOSE = 0 && $LOG = 0) => ONLY ERROR output (STDERR to console && STDOUT to /dev/null) ($VERBOSE = 1... (5 Replies)
Discussion started by: Marmz
5 Replies

2. Shell Programming and Scripting

Redirecting stdin/stdout to/from command from/to string

Hi, I am working on a project where I have to generate and execute nasm code on-the-fly. I generate the code in a file program.asm and then execute it.This output is to stdout which i redirect to an output file which i read back to compare results: system("nasm -f elf program.asm >... (5 Replies)
Discussion started by: doc_cypher
5 Replies

3. UNIX for Dummies Questions & Answers

fork and stdin

When a process fork(), the child share the same file descriptors as his father. Thus, they share the same stdin. Quick and dirty exemple below (sorry for the ugly gets() call) : #include <stdio.h> #include <unistd.h> int main() { char buf; if (fork()) { /*parent */ ... (1 Reply)
Discussion started by: milouz
1 Replies

4. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

5. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

6. Shell Programming and Scripting

Redirecting to stdin

Hi, I'm having trouble with my script. I have to select different choices without any interaction from a menu that looks like : a - xxxxxxxxxxxxxx b - xxxxxxxxxxxxxx c - xxxxxxxxxxxxxx d - xxxxxxxxxxxxxx I tried things like : echo "a" >&0 read < echo "a" but none worked. Any... (4 Replies)
Discussion started by: flame_eagle
4 Replies

7. Shell Programming and Scripting

redirect STDIN

can you redirect STDIN with command arguments? I have tried this approach: # ./script -option <argument1> <argument2> 0<$2 # $2: ambiguous redirect Is this possible? (4 Replies)
Discussion started by: prkfriryce
4 Replies

8. Shell Programming and Scripting

redirection stdin

hello all, I need to create a password change utility for a database. I need to gather at the command line the username, password and database sid. I have the program currently doing this. What I would like to do is not have the new password appear on the screen when I do my read command.... (2 Replies)
Discussion started by: whited05
2 Replies

9. HP-UX

stdin device on HP

How can I access the standard-in device in HP-UX? I am trying to automate sftp on an HP-UX system. On solaris I can just do: sftp -b /dev/fd/0 remotehost <<EOF cd pub ascii get filename.txt bye EOF (2 Replies)
Discussion started by: dangral
2 Replies

10. Programming

stdin

hi, how does a program know whether some data are available from stdin? I would like to make a program which could read its data from stdin and _if_there_is_nothing_at_stdin_ from a file which name is given as an argument. If there is nothing in stdin and no filename is given as argument,... (2 Replies)
Discussion started by: marquis
2 Replies

Featured Tech Videos