LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-30-2010, 11:51 AM   #1
babaqga
LQ Newbie
 
Registered: Aug 2010
Posts: 18

Rep: Reputation: 1
Feeding input to interactive programs in bash


Hi guys,

My question is the following. If I have an interactive program, e.g top,
which, after launch, can be fed with key strokes and changes its behavior, for example, after launching top and then pressing "1", you can see the CPU utilization per core. I was wondering if there is a programmatical way to do so, so I don't have to press "1" every time. I would think it is an easy task, but so far my attempts have failed.

So, if anyone can help, I would appreciate it.

Regards
 
Old 08-30-2010, 12:06 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,292

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Hi

I think it can be done with tools like expect but it's a lot easier with top.

start top
Press 1 - you know get CPU per core.
Press W - you get a message saying .toprc was saved
Press q - quit

Then start top again. It reads that config file at startup.
 
Old 08-30-2010, 12:07 PM   #3
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Hi -

Depending on the program or script, it's common to use either/both of "redirection" or "here files".

For example:

EXAMPLE REDIRECTION:
Code:
 echo mydata | myprog
Code:
 myprog < mydata
EXAMPLE "HERE FILE":
Code:
myprog <<!
first_data_item
second_data_item
last_data_item
!
But "top" happens to support a "configuration file". Editing your $HOME/.toprc file is almost certainly the best solution. See "man top" on your system for more details:

http://linux.about.com/od/commands/l/blcmdl1_top.htm

'Hope that helps

Last edited by 14moose; 08-30-2010 at 12:08 PM.
 
Old 08-30-2010, 08:19 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
For a significant program, it's common to have a separate cfg file eg if we have a Perl prog

x.pl

then there would be an x.cfg file which the prog would read; typically invoked as

/path/x.pl -c /path/x.cfg

NB: paths can different for prog & cfg files.
This cfg file would be plain text with comments & blank lines allowed eg
Code:
# comment describing param usage/values
param1_name=param1_value

# comment describing param usage/values
param2_name=param2_value
This enables non-programmers to use a simple text editor to change the cfg file (& thus the way the prog runs) without having to know anything about programming. Also avoids accidental altering of code by mistyping.
Doubly useful when the program is a compiled binary...

Strictly speaking this isn't for 'std' interactive progs; see above post instead. Some progs do have an option to be interactive OR use a cfg file for automation purposes; your choice.

Last edited by chrism01; 08-30-2010 at 08:28 PM.
 
Old 08-31-2010, 10:17 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Gnu screen can be used to host a session, into which another shell can stuff keystrokes.
Code:
screen -S "LQ Example" top
In another shell:
Code:
screen -S "LQ Example" -X stuff c
--- rod.
 
  


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: Passing Output to another programs Input? zachet Programming 6 11-19-2009 10:12 AM
Python MySQL: interactive query input threaderslash Programming 4 09-22-2009 06:36 PM
feeding input question charlitos Programming 6 03-21-2009 10:58 AM
C interactive line input SciYro Programming 6 04-06-2007 01:57 PM
How to input several cmds to interactive apps through one shell cmd? kornerr Linux - General 4 01-01-2006 01:05 PM


All times are GMT -5. The time now is 06:43 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