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 06:00 PM..
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).
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:
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. )
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)