• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

shell redirection

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I need to create something with getRuntime() that does the following:

srvrmgr /u <username> /g <gateway> /e <enterprise> /s <server> <<EOF
<password>
list tasks
exit
EOF


How do I do the <<EOF to EOF stuff? I was trying to use something like the following to do this, but it is not working:



It whines and dumps the commands usage information. I am guessing the <<EOF ... EOF part is what is not working.

Thanks,

David
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Here documents" are implemented by the parser of the shell; the here document is not read as if from the command line, but rather the shell switches from grabbing command lines to simply reading from standard input in unbuffered mode. So sending the data as a command line, as you're doing here, won't work. Two suggestions I can make to you: the first one would be to write the "here document"'s text to a real temporary file (call it 'inputfile'), then use standard redirection, which still requires a little song and dance -- you must say

sh -c "/bin/whatever arg arg arg < inputfile"

The other option is to feed the data into the process's standard input from the Java program, something like

Process p = Runtime.exec(...);
OutputStream os = p.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os), true);
pw.println("password");
pw.println("whatever");
pw.println("exit");
pw.close();
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful, thanks for the information. Unfortunately, I am not a java pro yet. My exec code does the following:



I see it closing OutputStream at the top fo run() and the it writes stuff to StringBuffers via the append() and appendEx() functions. The exec calls start() and join(). I take it start() calls the run() function. Do I put your pw writer stuff before it closes the output stream and then let it close the output stream?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, the printwriter idea worked fine for srvrmgr command. The only stuff I wrote was pw.println(password) and pw.println("exit"); I am not sure why, but if I do the same thing for another program (repimexp), it does not work. However, if I do from the command line the <<EOF ... EOF steps to pass in the password and exit it does work for repimexp. Any ideas on what I can look at on why it is not working for the repimexp?
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is waiting for the password on stderr instead of stdout (possible?), how do I do something like pw.println() and have it go to stderr instead of stdout?
 
Nothing? Or something? Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic