LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Writing to the STDIN of a shell (https://www.linuxquestions.org/questions/linux-newbie-8/writing-to-the-stdin-of-a-shell-822690/)

nikon2k 07-28-2010 01:23 PM

Writing to the STDIN of a shell
 
Hi,

Not sure if this is the right section to be posting this in, if not please point me in the right direction.

Pretty new to Linux, just starting to explore and write a few programs here and there. I'd like to get to know how the system works a bit better.

I am trying to write input to a shell and get the shell to parse the input that I am writing to it as if a user was typing in commands.

Thus far I have tried echoing some text into the shell's FD for STDIN in /proc/<pid>/fd

Whilst this displays the text that I echo, the shell that I am writing to never tries to execute the command that I pass to STDIN.

What is the difference between a shell taking STDIN from the user and data written to STDIN by another process e.g. echo ?

It appears I am missing something fundamental.

Cheers

imagine_me2 07-28-2010 02:42 PM

if could post the portion of the code that executes the command and the portion that takes the input.
Programs do not press "Return" as users do. Sometimes that may be the problem. But i will have to see the code to make out anything.

nikon2k 07-28-2010 03:03 PM

Hi,

Thanks for the reply.

Well there are two ways that I am trying.

The first is just through a shell using echo:

echo ls > /proc/<pid>/fd/0

The second way I have tried is by opening the FD in some C code and writing "ls\n" to it e.g.

Code:

fd = open("/proc/<pid>/fd/0", O_WRONLY);
write(fd, "ls\n", 3);

Both methods appear to print "ls" and a newline in the target terminal.

Pressing enter in the shell then starts a new line ready for input instead of processing the "ls" command.

Cheers

imagine_me2 07-28-2010 03:19 PM

Your shell should basically look like:

main()
{
while(1)
{
read(fileno(STDIN),buffer);
/*print the buffer here*/
execv(buffer);
}
}

Now if it prints it must execute. I was asking what does your shell look like. The skeleton code, i mean.

imagine_me2 07-28-2010 03:19 PM

Your shell should basically look like:

main()
{
while(1)
{
read(fileno(STDIN),buffer);
/*print the buffer here*/
execv(buffer);
}
}

Now if it prints it must execute. I was asking what does your shell look like. The skeleton code, i mean.

nikon2k 07-28-2010 03:30 PM

Hi,

Sorry, probably didn't make it very clear in my first post.

I am trying to write to a running bash shell's STDIN and get it to execute the commands I pass to it through STDIN.

I have not coded my own shell.

Cheers


All times are GMT -5. The time now is 11:15 PM.