Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
edOffline
Post subject: Send multiple commands to command line program?  PostPosted: 18.10.2010, 22:06



Joined: 2010-10-18
Posts: 6

Status: Offline
Hi,

I am using a command line program that does not accept input arguments, but when used waits for user input.
I have the source code, so the proper way to do it would be to add my own argument handling which I might do at some point anyway, but is it possible to 'queue' a list of commands to send to the program?

My google searching has not turned up anything yet, but maybe I just wasn't using the right keywords?

thanks,
ed
 
 View user's profile Send private message  
Reply with quote Back to top
sx9Offline
Post subject: RE: Send multiple commands to command line program?  PostPosted: 18.10.2010, 22:31



Joined: 2010-09-12
Posts: 219
Location: Wiesbaden,Germany
Status: Offline
I'm not completely sure what you mean, but I think a pipe would help you, because it tries to read something from the standard input after having been launched.

program1 | program2
or better:
echo 'commandlist' | programm

The first commandline pipes the standard output (for example an error message, or a list of files) of the first programm into the standard input of the second programm.

The second commandline is principially the same, but the text in commandlist will be exactly directed to the stdin of the 2nd programm, because the text in commandlist is the output (stdout) of the programm echo.

An example:

ls /home/sx9 | cpio -ov (not sure if the option is right - PC isn't running) > /opt/backup.cpio

ls lists all files in /usr and displays the filenames, wich are sent to cpio, that everytime reads the stdin and packs it to the backup.cpio-archive in /opt. > means write in and overwrite existing file. >> means write in and adding the stuff to the existing file.
 
 View user's profile Send private message Send e-mail Yahoo Messenger  
Reply with quote Back to top
sx9Offline
Post subject: RE: Send multiple commands to command line program?  PostPosted: 18.10.2010, 22:44



Joined: 2010-09-12
Posts: 219
Location: Wiesbaden,Germany
Status: Offline
Recommended Links(all in German):

http://www.bin-bash.de/scripts.php
http://www.chemie.fu-berlin.de/chemnet/ ... ts_sh.html
http://de.wikibooks.org/wiki/Linux-Komp ... rammierung

_________________
My new self-made computer:
Intel Core i7-2600k
ASUS Maximus IV Gene-Z (Mainboard)
2x4GB DDR3 RAM
ATI Radeon HD 6770
OCZ Vertex 3 60GB (SSD)
Western Digital Caviar Green WD20EARX 2TB (HDD)
...
aptosid x86_64
 
 View user's profile Send private message Send e-mail Yahoo Messenger  
Reply with quote Back to top
edOffline
Post subject:   PostPosted: 19.10.2010, 08:51



Joined: 2010-10-18
Posts: 6

Status: Offline
Yes, a pipe was exactly what I needed!

I now have:

myscript.sh

#! /bin/sh
echo command1
echo command2
echo command3
echo command4


and now...

$ sh myscript.sh | myprogram

works perfectly, allowing me to edit just one line of the script if required, and then rerun the whole thing.


Thank you for your help!
 
 View user's profile Send private message  
Reply with quote Back to top
muchan
Post subject:   PostPosted: 19.10.2010, 11:15
Moderator


Joined: 2010-09-11
Posts: 468

If this echo xxxx works, then probably you need only a text file like mycmd with

command1
command2
command3
command4

and call
$ myprogram < mycmd
 
 View user's profile Send private message  
Reply with quote Back to top
sx9Offline
Post subject:   PostPosted: 19.10.2010, 14:39



Joined: 2010-09-12
Posts: 219
Location: Wiesbaden,Germany
Status: Offline
That works, too.
The text file will be read by the programm.

> means write
< means read

You have to use it like arrows directing from the source to the destination)
 
 View user's profile Send private message Send e-mail Yahoo Messenger  
Reply with quote Back to top
edOffline
Post subject:   PostPosted: 20.10.2010, 21:37



Joined: 2010-10-18
Posts: 6

Status: Offline
yes, that is a bit easier - I don't have to write echo in front of every command this way, although I can't put comments in my scripts now, but I know what they do.

I am now running the program with the list of commands and logging the outputs:
$ myprogram <mycmds> outputlog.txt


thanks
 
 View user's profile Send private message  
Reply with quote Back to top
sx9Offline
Post subject:   PostPosted: 21.10.2010, 09:01



Joined: 2010-09-12
Posts: 219
Location: Wiesbaden,Germany
Status: Offline
Sometimes it's easier to use pipes and sometimes not

_________________
My new self-made computer:
Intel Core i7-2600k
ASUS Maximus IV Gene-Z (Mainboard)
2x4GB DDR3 RAM
ATI Radeon HD 6770
OCZ Vertex 3 60GB (SSD)
Western Digital Caviar Green WD20EARX 2TB (HDD)
...
aptosid x86_64
 
 View user's profile Send private message Send e-mail Yahoo Messenger  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT - 12 Hours
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by Zafenio