无法使用JSch库执行包含变量的Unix命令 [英] Not able to execute Unix commands containing variables using JSch library

查看:68
本文介绍了无法使用JSch库执行包含变量的Unix命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Java程序,该程序执行后将打开一个上传向导,用户可以通过该向导选择文件.完成此操作后,所选文件将被传输/上传到Unix盒中的目录,该目录由用户指定并进行进一步处理.这是步骤/动作:

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. 通过上传向导浏览文件
  2. 此后,程序将提示您创建一个新目录,用户可以输入该目录的名称
  3. 程序将创建一个新目录(源)并将所选文件放置在服务器的已创建目录中
  4. 在源文件中显示传输文件的内容(使用cat命令)
  5. 提示用户要创建的新目录(目标),将文件从源复制到目标,然后使用Linux命令在目标中显示文件的内容.

我可以将文件上传到源(第3步).但是,第4步和第5步无效.这是代码:

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;

我无法使程序使用命令中的变量(用于用户输入).然而,像这样的硬编码字符串. cat /a/b/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?

推荐答案

我认为问题在于变量值(创建了变量d但几乎未使用)和最后一个/字符有些混乱.而且我不知道p值.

I think the problem is in some mess with variables values (variable d is created but almost not used) and final / character. And I don't know the p value.

在执行它们之前,我将开始为cmd2cmd3添加 println ,因为我认为某个地方会丢失/,或者应该在两个地方出现/只能取决于用户的类型,并对目录进行某种规范化,以便它们始终具有最终的/,否则就永远没有,但是我想确保无论用户输入或参数具有什么,所有目录名称是一致的.

I would start adding a println for cmd2 and cmd3 before executing them because I think there will be a missing / in some place, or perhaps two / where should be only one depending on what the user types, and doing some normalization of directories so they always have a final /, or they never have, but I would like to be sure that whatever the user inputs or the parameters have, all directory names are consistent.

目录名称,用户输入和最终/总是很麻烦...

Directory names, user input and final / are always a pain...

这篇关于无法使用JSch库执行包含变量的Unix命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆