开发者

How to input asterisk into command line for it to understand?

I use php to get a string, ex : '3 * 2' , and cal开发者_运维问答l java (by exec() )to compute this but even when I replace the '*' with '\*' or not, it still didn't work. When I pass these arguments to command line, it works for '\*', not for '*'. How could I input asterisk so that the command line can understand ? I don't want to change the way my program work with '' into st else in the java file. I am using Ubuntu.


you can use escapeshellarg() in the php to escape your arguments


That's a very confused question. I had to guess what things like "pass these arguments with command line", which make no sense as they stand, might mean. This answer is based upon guessing what your question would have been in English.

It seems that you can invoke your Java program from the command line, as long as you escape metacharacters that the shell otherwise globs for you. That's normal and expected, and something that everyone using a shell to run programs that have characters such as * in their command-line syntaxes should be long familiar with.

Your actual problem appears to be that you cannot invoke this very same Java program from within a PHP script, because you are passing it '3 * 2' (with quotation marks) as the single command-line argument provided to exec(). Again, the problem here is very likely another well-known one. When one runs a program through the shell with the command-line 3 \* 2 (no quotation marks, notice), that is actually split, by the shell, into three command line arguments, 3, *, and 2 which are what the shell actually provides to the underlying execve() system call. So that's what your Java program will be expecting. You need to ensure that that's what ends up happening when you invoke the program from PHP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜