Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-30-2010, 12:51 PM   #1
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.

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

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351

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, 01:07 PM   #3
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:

 echo mydata | myprog
 myprog < mydata
myprog <<!
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:

'Hope that helps

Last edited by 14moose; 08-30-2010 at 01:08 PM.
Old 08-30-2010, 09:19 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,569

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

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

/path/ -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
# comment describing param usage/values

# comment describing param usage/values
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 09:28 PM.
Old 08-31-2010, 11:17 AM   #5
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
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.
screen -S "LQ Example" top
In another shell:
screen -S "LQ Example" -X stuff c
--- rod.


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

All times are GMT -5. The time now is 02:25 PM.

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