LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-20-2006, 11:36 AM   #1
ptcl
LQ Newbie
 
Registered: Nov 2006
Posts: 2

Rep: Reputation: 0
Bash script question (sending output to specific lines)


Basically I'd like to write a script that does something fairly simple. How to go about doing it, however, is another matter.

I'm running a command that will output many lines, asking for a Y/N/All response. There is no way to pass directly to this program the string I am looking for.

Let's say it will output A-Z. Let's also say I'm looking for the output D. I'd want it to, for every line starting with "The letter is", to say "N" until it comes to "D". Even after it has accepted "D" as my answer, it would still have to say "N" for the rest of the lines until "The letter is" line no longer appears, then it can exit the function.

I'm horrible at explaining these things, but hopefully some bash scripter out there feels my pain and will lend a hand. Thanks VERY MUCH in advance!

(Example)

The letter is A (Yes/No/ALL): N
The letter is B (Yes/No/ALL): N
The letter is C (Yes/No/ALL): N
The letter is D (Yes/No/ALL): Y
The letter is E (Yes/No/ALL): N
...
 
Old 11-20-2006, 12:53 PM   #2
AlbinoJap
Member
 
Registered: Mar 2004
Posts: 36

Rep: Reputation: 15
This can be done in BASH, however, it would probably be easier to use something like Perl. Is there a particular reason why it needs to be written in BASH?
 
Old 11-20-2006, 12:55 PM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
This will be better handled with an expect script rather than a bash script. Bash is poor at automating interactive processes, whereas expect excels at this. For example:

Code:
#!/usr/bin/expect

spawn -noecho someprogram
while 1 {
   expect {
      "The letter is D (Yes/No/ALL):" {send "Y\r"}
      "The letter is \[A-CE-Z] (Yes/No/ALL):" {send "N\r"}
      timeout {send "N\r"}
      eof exit
   }
}

Last edited by macemoneta; 11-20-2006 at 01:02 PM.
 
Old 11-20-2006, 01:10 PM   #4
ptcl
LQ Newbie
 
Registered: Nov 2006
Posts: 2

Original Poster
Rep: Reputation: 0
macemoneta, thanks! This should work exactly as expected (pardon the pun). I'm a bit new to scripting under linux, and definitely intend to read the extend man later, but is there a way to, instead of specifying A-CE-Z, just to send "N" to anything that's not "D"?

Thanks in advance once again.

Quote:
Originally Posted by macemoneta
This will be better handled with an expect script rather than a bash script. Bash is poor at automating interactive processes, whereas expect excels at this. For example:

Code:
#!/usr/bin/expect

spawn -noecho someprogram
while 1 {
   expect {
      "The letter is D (Yes/No/ALL):" {send "Y\r"}
      "The letter is \[A-CE-Z] (Yes/No/ALL):" {send "N\r"}
      timeout {send "N\r"}
      eof exit
   }
}
 
Old 11-20-2006, 01:20 PM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Sure, you can use any regular expression, so "\[^D]" matches any single character that is not D. There's a great book available for expect, if you're interested. It can do amazing things, and I find TCL (the language it is embedded in, above) to be much more readable than perl.
 
Old 11-20-2006, 01:41 PM   #6
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Rep: Reputation: 30
I wonder if this can be done using bash. Does any body know if bash can be used for interacting with program's output before the program ends?

One a side note, can you count the number of inputs you have to give as N before you arrive at the one with Y? (yeah yeah, this is a lame question :P)

Last edited by anupamsr; 11-20-2006 at 01:42 PM.
 
Old 11-20-2006, 01:53 PM   #7
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Bash seems to be limited to redirection for interaction, so if the number of inputs of a certain value are deterministic (as in the above example, A,B,C,D...), you could send the apropriate responses. If they are random, bash would not be able to accomodate that.

To count the number of occurences in the TCL/expect example:

Code:
#!/usr/bin/expect
set yes 0
set no 0
spawn -noecho someprogram
while 1 {
   expect {
      "The letter is D (Yes/No/ALL):" {
         incr yes
         send "Y\r"
      }
      "The letter is \[A-CE-Z] (Yes/No/ALL):" {
         incr no
         send "N\r"
      }
      timeout {
         incr no
         send "N\r"
      }
      eof {
         puts "Total yes: $yes  Total no: $no"
         exit
      }
   }
}
 
Old 11-20-2006, 02:42 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I think expect is the best option if you want to be simulating keystrokes dependant on a program's output. You could possibily do it by reading the program's output into a while loop, and having the program's input read from a fifo, which something in the while loop writes to... but that's way ugly.
 
Old 11-21-2006, 03:14 AM   #9
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Rep: Reputation: 30
Quote:
Originally Posted by matthewg42
I think expect is the best option if you want to be simulating keystrokes dependant on a program's output. You could possibily do it by reading the program's output into a while loop, and having the program's input read from a fifo, which something in the while loop writes to... but that's way ugly.
I guess there is a possibility of fifo not even working because buffer will be read at random times, though I don't know. Some one could clarify this issue.
 
Old 11-21-2006, 04:53 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by anupamsr
I guess there is a possibility of fifo not even working because buffer will be read at random times, though I don't know. Some one could clarify this issue.
Yeah, you're probably right.
 
  


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
cat: output specific number of lines mikeshn Linux - Software 8 01-24-2017 12:08 PM
Vi question. Cutting a specific lines blackzone Linux - General 1 07-19-2006 07:10 PM
Shell Script (adding a prefix to output lines) rheza Programming 3 01-04-2005 02:18 PM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 01:42 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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