From: Jack on
I have a script. When excecuting the script from commandline, a line
will be print out to prompt for password. For example:

>myscript
>please input your password:

Then a user can type in the password.

In my java programe, I will use Runtime.getRuntime() to run the script
as;

Runtime.getRuntime().exec("myscript");

How to pass in the password automatically if I store the password in a
String? Here there is no use/program interaction. There is no user to
type in the password.

Thanks.

Jack


From: Jean-Baptiste Nizet on
On 31 mar, 08:53, Jack <junw2...(a)gmail.com> wrote:
> I have a script. When excecuting the script from commandline, a line
> will be print out to prompt for password. For example:
>
> >myscript
> >please input your password:
>
> Then a user can type in the password.
>
> In my java programe, I will use Runtime.getRuntime() to run the script
> as;
>
> Runtime.getRuntime().exec("myscript");
>
> How to pass in the password automatically if I store the password in a
> String? Here there is no use/program interaction. There is no user to
> type in the password.
>

The easiest way is certainly to modify the script. It might take a
unique argument which would be the password, so you could start it
with

myscript mySecretPassword

If no argument is passed in, then it would ask it as it does now.

JB.
From: Paul Cager on
On Mar 31, 7:53 am, Jack <junw2...(a)gmail.com> wrote:
....
> Runtime.getRuntime().exec("myscript");
>
> How to pass in the password automatically if I store the password in a
> String? Here there is no use/program interaction. There is no user to
> type in the password.
>
> Thanks.
>
> Jack

If you can't alter the script (or don't want to send the password as
an argument, e.g. for security concerns), then you could write input
to the process's stdin:

Process p = Runtime.getRuntime().exec("....");
p.getOutputStream().write("MyPassword".getBytes());
p.getOutputStream().close();

(That should work for Linux, but I'm not too familiar with Windows).

However a script to read a password might not read from standard input
(it might open the terminal directly so that it can control character
echoing). In that case things get complicated...
From: Roedy Green on
On Tue, 30 Mar 2010 23:53:22 -0700 (PDT), Jack <junw2000(a)gmail.com>
wrote, quoted or indirectly quoted someone who said :

>In my java programe, I will use Runtime.getRuntime() to run the script
>as;

It would be simpler just to have Java ask for the password.

You did not say which language your script was written in or what
environment it was running in.

In any case, in Windows, you can't run any sort of script the way you
were attempting. See http://mindprod.com/jgloss/exec.html
You need to launch some sort of command processor that understands the
script, and pass it the script as a parameter.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you tell a computer the same fact in more than one place, unless you have an automated mechanism to ensure they stay in sync, the versions of the fact will eventually get out of sync.
From: Martin Gregorie on
On Wed, 31 Mar 2010 14:14:39 -0700, Roedy Green wrote:

> In any case, in Windows, you can't run any sort of script the way you
> were attempting. See http://mindprod.com/jgloss/exec.html You need to
> launch some sort of command processor that understands the script, and
> pass it the script as a parameter.
>
I can't think of any scripting language that doesn't apply to.

The only difference is that UNIX shells use the 'hash-bang' metaphor to
let the first line of a script specify what interpreter is to be used to
interpret the rest of it. BUT that first line is still interpreted by a
command line processor capable of loading and starting to run the script.

IOW the OP *must* specify the correct interpreter for the script, and
probably should not make assumptions about the calling program being able
to search for that interpreter. In other words if the OP's exec argument
is:

"myscript" it will almost certainly fail to run

"perl -- myscript" it might run if the program's run-time
can search for the Perl interpreter

"/usr/bin/perl -- myscript" it will run in most environments

and in a UNIX/Linux environment "/bin/sh -c myscript" would run a perl
script provided that its first line is #!/usr/bin/perl
and a gawk script if the first line is #!/usr/bin/gawk


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |