LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-10-2005, 12:00 AM   #1
happy78
Member
 
Registered: Aug 2003
Posts: 50

Rep: Reputation: 15
echo command to a running process


Hi all,

Im a newbee to shell bash
I wrote a shell bash that launches a program in Redhat, this program reads contents of a file...then wait for a command to do some job

$DIR/spider /tmp/test
echo "doThis"

Spider will be launched using the contents of test. Once, it up, it waits for command to tell it what to do w/ the contents. However, my echo doesn't echo "doThis" to Spider's command line.

How do i do this?
And when user input something on Spider's command line, how do I record them for later user?

Thank you for your time and assistance..
 
Old 08-10-2005, 12:05 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Well, you could try something like
Code:
echo "doThis" | $DIR/spider /tmp/test
 
Old 08-10-2005, 12:23 AM   #3
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Matir
Well, you could try something like
Code:
echo "doThis" | $DIR/spider /tmp/test
That works great.
However, if I want to keep sending command (1 after another) to Spider within 1 session of Spider. How do I do that?
And I can't just do: echo "doThis" | $DIR/spider /tmp/test because everytime i do it, it's another session of Spider.

Anyone knows how to record whatever user enters in the Spider's command line?
 
Old 08-10-2005, 12:41 AM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
You could record things using the "script" utility.

To echo multiple commands, you can do something like:
Code:
echo -e "command1\ncommand2" | spider
or
Code:
echo <<EOT | spider
command1
command2
EOT
Obviously, replace spider with the command above. Also, read the bash man pages on "Here Documents"
 
Old 08-10-2005, 03:20 PM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
what you want to do is run a server (or service) that listens for input then re-acts appropriately. check 'ipc' to see how to share mem among running processes.

this should get you started:

#!/bin/ksh
while(true)
do
cat command.txt
sleep 5
done
________________

save and run it as:
schneidz@lq> spider.ksh &

then
schneidz@lq> echo "new-command" > command.txt

good luck,
 
Old 08-10-2005, 04:48 PM   #6
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Matir
You could record things using the "script" utility.

To echo multiple commands, you can do something like:
Code:
echo -e "command1\ncommand2" | spider
or
Code:
echo <<EOT | spider
command1
command2
EOT
Obviously, replace spider with the command above. Also, read the bash man pages on "Here Documents"
Thank you all for your help and time. I have tried your method, but the echo went somewhere else, not to Spider.
My spider program is giving me a hard time. As I launch it, it reads the file for some inputs, then it automatically does the job. Once it is done, it doesn't quit until I issued a "quit" command.

How do I issue the "quit" command within a RUNNING bash script? So there are 2 threads, the bash-script and the spiders. I like send the quit command to spider.
And how can I capture inputs that were entered in Spider's command line?
 
Old 08-10-2005, 04:52 PM   #7
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Just add quit to the command lines above.
 
Old 08-11-2005, 12:05 PM   #8
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally posted by happy78

How do I issue the "quit" command within a RUNNING bash script? So there are 2 threads, the bash-script and the spiders. I like send the quit command to spider.
And how can I capture inputs that were entered in Spider's command line?
why not kill two penquins with one stone (or at least hurt them badly).

what i was alluding to in my first example is i would put a condition in the first part of my script that would handle a call like:
schneidz@lq> spider.ksh svr &
that would start the server.

then in a new console i would update 'command.txt' with the current command:
schneidz@lq> spider.ksh -c command #this in affect would check if $1 is equal to '-c', then execute 'echo $2 >> command.txt

the server loop would 'tail' the last line of command.txt. to actually quit, you have to hard-code the script to break the loop if 'tail -n 1 command.txt' = quit
(usually servers or ipc services contain a file like spider.pid - not sure how to cat a pid?)

if you wanted to be fancy you can timestamp the line when it is executed. and do some error handling like add a blank line so it doesn't continually execute the same command.

Last edited by schneidz; 08-11-2005 at 12:41 PM.
 
Old 08-16-2005, 03:27 PM   #9
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Guys,

Thank you very much for your help.
I found a solution to my problem, and would like to post it here if anyone is looking for it.
1) find out the proccess ID
2) cd /proc/pID/fd
3) ls -la
user should see something links: 0, 1, 2...etc

4) echo "blabla" > /proc/pID/fd/0
this will echo "blabla" to the running process


That would do it!
Unfortunately, I ran into another problem. After the command is echoed, I had to MANUALLY press enter for the SPIDER to execute my command. So, what is the equivalent command to ENTER_BUTTON?
In other words, I don't want to MANUALLY press the ENTER_BUTTON. I want SPIDER to eat the command right after echo-ing.

Thank you gys
 
Old 08-21-2005, 12:47 PM   #10
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
i'm a little confused. you would have to hit enter in order to execute the echo command. this might stare you in the right direction:

echo -e "\012"
echo -e "\015"

012 is the octal value for an ascii line feed and 015 is for a carriage return.

hope this helps...
 
Old 09-01-2005, 08:06 PM   #11
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
thank you very much,

but it doesn't help. I can echo "quit" to spider, but without pressing enter, spider wont quit..
 
Old 09-01-2005, 11:07 PM   #12
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
You're not using the -n on echo, are you?
 
Old 09-01-2005, 11:08 PM   #13
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
thank you for replying..

I'm using -e on the echo
 
Old 09-01-2005, 11:27 PM   #14
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Try adding an extra "\n" to the end of the echoed line.
 
Old 09-01-2005, 11:47 PM   #15
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
that doesn't work either.
I used -n in the echo, spider catches it without jumping to the new line. The weir thing is I can't even delete the echoed-line that spider received. Then I hit entered in hope that spider would accept the echo-lined and echo it back to the screen. But spider didn't, because it echoed back a blank new line.
 
  


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
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 01:08 AM
Bash Scripting - echo command question gbhil Programming 2 04-27-2005 08:27 PM
what is the echo command in perl? hamish Programming 5 12-14-2004 09:31 AM
How can I see what process are running and how to I kill them? (command line). brynjarh Linux - Newbie 4 07-07-2004 04:01 PM
echo command peculiarity eallen Linux - General 3 02-11-2003 06:30 PM


All times are GMT -5. The time now is 08: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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration