Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

java - Not able to execute Unix commands containing variables using JSch library

I have written a Java program which when executed would open an upload wizard through which a user can select a file. After doing this, the selected file would be transferred/uploaded to a Unix box to a directory which the user specifies and processed further. Here are the steps/actions:

  1. Browse the file through the upload wizard
  2. After this the program would prompt for a new directory to be created, the name of which the user can give as an input
  3. The program creates a new directory (source) and places the selected file in the created directory of the server
  4. Show the contents of the transferred file in the source (using cat command)
  5. Prompt the user for a new directory to be created (target), copy the file from the source to target and subsequently show the contents of the file in the target, all with Linux commands.

I'm able to upload the file to the source (step 3). However, the 4th and the 5th steps don't work. Here's the code:

String cmd1 = "cat" + " " + path + s1 + "/" + file;
System.out.println(cmd1);
((ChannelExec)channel).setCommand(cmd1);
Scanner in2 = new Scanner(System.in);
System.out.println("Enter d target directory");
String s = in2.nextLine();
String path2 = "/e/f/";
String d = path2 + s;
String cmd2 = "mkdir" + " " + path2 + s;
((ChannelExec)channel).setCommand(cmd2);
String src = p + "/" + file;
String cmd3 = "cp" + " " + path + s1 + "/" + file + " " + path2 + s;
((ChannelExec)channel).setCommand(cmd3);
String destpath = d + "/" + file;
String cmd4 = "cat" + " " + path2 + s + "/" + file;

I'm not able to make the program work with variables (for user inputs) in the command. However, hardcoded strings like, for eg. cat /a/b/file seems to be able to work.

Could any one please help me in this regard?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From your snippet i guess channel is a ChannelExec, which is a channel intended for just issuing commands and does not work like a shell (that use-case is handled by the shell channel, which allocates a pseudo-tty).

In your case I'd simplify the code and not using variables at all. Otherwise, if you absolutely need them, you could:

  • if the variable is defined your Java program call ChannelExec.setEnv(name, value)
  • if the variable is defined on the target host, ensure it's in the right place, for example a file sourced when a non-interactive SSH session is spawned. A good place could be .bashrc but you should check your system documentation (and maybe this other answer)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...