LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-14-2006, 11:01 AM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
Simulate a "Ctrl+D" in a script?


Hi again all


I'm trying to write a bash script that will perform a Ctrl+D key combination. Any way to do this?


Thanks!
 
Old 06-14-2006, 11:17 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
In what context ?
For instance, if you want to close the input of standard input, as in "cat > /tmp/input.data", you may use the special << redirection specifier:
Code:
cat > /tmp/input.data <<EOT
Put your input ext here.
You even can use vars to be expanded on the fly, like:
My home dir is $HOME
EOT
In the above, EOT will close the input, as much CTRL^D would.
 
Old 06-14-2006, 12:04 PM   #3
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Hi marozsas


Thanks for the help. In my situation I'm trying to do a grep on an executable file. Particularly, I'm trying to do a wordcount on a particular error named "TA not available".

Lets call the executable term.

Here's what happens...

Quote:
[kinetik@testserver]# term localhost:6916 | grep "TA not available" | wc -l

TA not available
(At this stage, the program just hangs. With this particular executable, to exit you have to use Ctrl+D.)
(...After pressing Ctrl+D)
0
[kinetik@testserver]#
Would your example work in my situation?


EDIT: Sorry I was mistaken, when the executable hangs, not even it's built-in exit command (Ctrl+D) works. I actually have to Ctrl+C it to get back to prompt.

Last edited by kinetik; 06-14-2006 at 12:08 PM.
 
Old 06-14-2006, 12:14 PM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
I think its happens because some binary data can hangs your terminal if the terminal driver interprets some of this binary data as commands.
Try to grep only text data. Use the "strings" command to filter non-text data.

Code:
term localhost:6916 | strings | grep "TA not available" | wc -l
 
Old 06-14-2006, 12:36 PM   #5
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Hi marozsas


I tried your suggestion, no luck I'm afraid. The executable just gets stuck again, then when I do a Ctrl+C it just goes back to prompt, no result this time round.
 
Old 06-14-2006, 12:49 PM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
May be the term program never closes the pipe. It is just waiting to put more data on the pipe....
If I am right, then grep's m option can help. It closes the input after m matching lines.
Code:
term localhost:6916 | strings | grep -m 1 "TA not available" | wc -l
In the above code, the grep will close the standard input after the first match.

I am not sure if this is acceptable to your logic. Otherwise, we must think in another approach. May be putting this piece of code in background and killing it after a while.
 
Old 06-14-2006, 01:04 PM   #7
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Ah man, that nearly looked like it was going to work! Thanks for your help so far marozsas.

Isn't there another way to send the results of the term executable to grep?

I even tried this:

Quote:
grep -m 1 "TA not available" < `term localhost:6916`
I think the problems are a) The term program doesn't output the "TA not available" message to a place where grep actually sees it and b) Isn't there a way to run the term executable to terminate after say 5 seconds?
 
Old 06-14-2006, 01:57 PM   #8
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Ok, lets go try this:
Code:
# create a fifo
mkfifo mypipe

# write to the fifo, in background
term localhost:6916 > mypipe &

# in background, wait 10s, then kill term
( sleep 10; kill `ps -ef | grep "term localhost" | grep -v grep | awk '{print $2}'` ) &

# read the pipe, you have 10s to do that
grep "TA not available" mypipe | wc -l
I don't like this solution. We are pushing hard on this one.
The best solution is term finish itself after a while, or we close the pipe after a number of times, like in "grep -m 1".

Anyway, without more knowledge of term and it's output, I think it is my best shot.

good luck !
 
Old 06-14-2006, 02:10 PM   #9
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Yeah, I'm a tad hesitent to use that one, as the term program itself is on a pretty important machine. I'm gonna try to copy the term program over to a test machine and give it a go.

Thanks a bunch for your help marozsas, I really appreciate it.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
"Alt GR" produces Keycode for "CTRL" as well Bigpet Linux - Software 5 04-09-2006 04:31 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:37 PM.

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
Open Source Consulting | Domain Registration