java - Execute shell script with different interpreter -
i trying execute python script using jsch, want run python script .sh file using different interpreter (shebang).
here content of shell script file (fileexec.sh):
#!/usr/bin/env python print 'hello python'
it looks shebang cannot changed since getting:
bash : print command not found
here java code:
session = newsessionfor(user, host, port); channel channel = session.openchannel("exec"); file shfile = new file("fileexec.py"); filewriter writer = new filewriter(shfile); writer.write(shebang + "\n"); writer.write(command); writer.close(); printwriter printwriter = new printwriter(shfile); printwriter.println(shebang); printwriter.println(command); printwriter.close(); ((channelexec) channel).setcommand(fileutils.readfiletobytearray(shfile));
the shebang used os when executing file.
you not executing file, copy-pasting contents of python file onto shell prompt.
if don't want store , execute file on server, can run python command shell python -c yourcommand
. here's how looks in terminal, feel free try:
user@host ~$ python -c 'print "hello world"' hello world
to in program, add method escape arbitrary string in shells:
static string escapeforshell(string s) { return "'" + s.replaceall("'", "'\\\\''") + "'"; }
then run
((channelexec) channel).setcommand("python -c " + escapeforshell(command));
where string command = "print 'hello python'";
Comments
Post a Comment