LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2010, 02:23 PM   #1
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Rep: Reputation: 0
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
 
Old 07-28-2010, 03:42 PM   #2
imagine_me2
Member
 
Registered: Nov 2009
Location: Kolkata, India
Distribution: Fedora 11
Posts: 136

Rep: Reputation: 22
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.
 
Old 07-28-2010, 04:03 PM   #3
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 07-28-2010, 04:19 PM   #4
imagine_me2
Member
 
Registered: Nov 2009
Location: Kolkata, India
Distribution: Fedora 11
Posts: 136

Rep: Reputation: 22
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.
 
Old 07-28-2010, 04:19 PM   #5
imagine_me2
Member
 
Registered: Nov 2009
Location: Kolkata, India
Distribution: Fedora 11
Posts: 136

Rep: Reputation: 22
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.
 
Old 07-28-2010, 04:30 PM   #6
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script stdout, stderr and stdin solo9300 Linux - General 6 12-29-2009 01:33 AM
read from stdin in a shell script bujecas Linux - General 3 07-03-2009 05:46 PM
Capture all stdin of shell to a file. jimmax777 Linux - Newbie 5 08-24-2006 07:22 AM
How to create a shell script that reads something from stdin? ricky_ds Programming 2 05-17-2005 06:06 AM
writing to child process stdin avikosan Programming 2 11-11-2003 08:06 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration