简体   繁体   English

--shell停止执行,包括命令提示符,批处理文件,PHP exec()等

[英]--shell stops execution, for Command Prompt, Batch files, PHP exec(), and more

What I want 我想要的是

I have a command I was to execute. 我有一个要执行的命令。 When I do this manually with command prompt, one command at a time, I have no issues. 当我通过命令提示符手动执行此操作时,一次只有一个命令,我没有任何问题。 I am trying to automate this. 我正在尝试使其自动化。

What I have tried 我尝试过的

Here is what I type manually into command prompt. 这是我在命令提示符下手动键入的内容。 I have also tried using a .bat file. 我也尝试过使用.bat文件。

cd c:/Program Files/Inkscape
inkscape --shell
c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900

Here is an attempt using php exec() . 这是尝试使用php exec()

exec('cd c:/Program Files/Inkscape && inkscape --shell && c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900');

What is happening 怎么了

Debugging using echo , it is clear that --shell is causing the execution to stop. 使用echo调试,很明显--shell导致执行停止。 When running the batch file, the batch closes at that command. 运行批处理文件时,批处理将在该命令下关闭。 When running php exec() , no more commands work after that command is called. 当运行php exec() ,在调用该命令后不再有其他命令可用。 When I execute that command manually over command prompt, I get this response. 当我在命令提示符下手动执行该命令时,会收到此响应。

Inkscape 0.91 r13725 interactive shell mode. Type 'quit' to quit.

After this, I can run the next command I need to run. 在此之后,我可以运行我需要运行的下一个命令。 I can't, however, run this command. 但是,我无法运行此命令。

inkscape --shell && c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900

I get the same response, and the actions after --shell are not taken. 我得到相同的响应,并且--shell之后的动作未采取。

When I removed --shell from both the batch file, it gives me an inkscape error 当我从两个批处理文件中删除--shell ,它给了我一个inkscape错误

(inkscape.exe:11912): Gtk-WARNING **: Could not find the icon 'object-visible'.
The 'hicolor' theme was not found either, perhaps you need to install it.
You can get a copy from:
http://icon-theme.freedesktop.org/releases
(inkscape.exe:11912): Gdk-CRITICAL **: inner_clipboard_window_procedure: assertion 'success' failed

The exec function without --shell results in the file to never finish loading. 没有--shellexec函数导致文件永远无法完成加载。

What I need 我需要的

It can be a batch file, php exec() function, or any other method to accomplish this command, as long as it can be automated. 它可以是批处理文件,php exec()函数或任何其他可以完成此命令的方法,只要它可以自动化即可。 Please explain your answer so I can understand executing commands a little bit better. 请解释您的答案,以便我更好地理解执行命令。

Update: What Workers 更新:什么工人

Thanks to both answers for help in figuring this out. 感谢这两个答案为您解决这个问题提供了帮助。

In inkscape, I had to create a folder in Inkscape/share/icons named hicolor , and in that folder, place an empty file called index.theme . 在inkscape中,我必须在Inkscape/share/icons创建一个名为hicolor文件夹,然后在该文件夹中放置一个名为index.theme的空文件。 Then, I had to correct my syntax to be this. 然后,我不得不更正我的语法。

cd c:/Program Files/Inkscape && inkscape --file=t1.svg --export-eps=r1.eps --export-dpi=900)

Looking at the docs, --shell is for interactive use only as if you are typing into a terminal. 查看文档,--shell仅用于交互式使用,就好像您在终端中键入内容一样。 Try removing this flag. 尝试删除此标志。 You need your CLI program to execute on the command line in a non-interactive mode in order to trigger it from PHP. 您需要CLI程序以非交互模式在命令行上执行,以便从PHP触发它。

Here are the options: https://inkscape.org/en/doc/inkscape-man.html 以下是选项: https : //inkscape.org/en/doc/inkscape-man.html

I can't speak to any Windows-specific behaviour here, but ... Inkscape's --shell option may take commands from standard input. 在这里,我无法谈论任何Windows特定的行为,但是... Inkscape的--shell选项可能会从标准输入中获取命令。 If that works the same in your environment as it would in a Unix system, then there may be an easy solution. 如果在您的环境中与在Unix系统中相同,那么可能会有一个简单的解决方案。

First off, note the notation you've used in your exec() : 首先,请注意您在exec()使用的符号:

commandone && commandtwo

This is not the same as typing one line (commandone) and then typing another line (commandtwo). 这是一样的键入一条线(commandone),然后键入另一线路(commandtwo)。 Instead, what it does is to run commandone, and if it completes (finishes) successfully, run commandtwo. 相反,它要做的是运行commandone,如果成功完成 (完成),则运行commandtwo。 This is obviously not what you want. 这显然不是您想要的。

Instead, in a Unix environment, you might try something like this: 相反,在Unix环境中,您可以尝试如下操作:

echo "somestring" | commandone

This makes "somestring" the input to "commandone", as if you had typed it into the terminal. 这使“ somestring”成为“ commandone”的输入,就像您已经在终端中键入了一样。 In your case, this might look something like this: 就您而言,这可能看起来像这样:

echo "c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900" | inkscape --shell

The effect is that you take a string , and you echo it .. but you pipe the stdout through a command ( inkscape --shell ) which accepts stdin . 效果是您采用了一个字符串 ,然后回显了它..但是您通过接受stdin的命令( inkscape --shell )将stdout传递给管道。

Alternately if you are using bash as your shell, another notation might be: 或者,如果您将bash用作shell,则另一种表示法可能是:

inkscape --shell <<<"c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900"

This is possibly easier to read, in that it puts the important command at the beginning of the line. 这可能更容易阅读,因为它将重要的命令放在行的开头。 The <<< tells bash to "take the following string, and feed it to the preceding command", or thereabouts. <<<告诉bash“接收以下字符串,并将其提供给前面的命令”或其附近。

To put this into PHP exec(), I would recommend using the first notation, because I have no idea whether your Windows environment uses bash or some other shell to execute command lines. 要将其放入PHP exec(),我建议使用第一种表示法,因为我不知道您的Windows环境是使用bash还是使用其他Shell执行命令行。

Try the "echo" line above in your Windows shell, and see what happens. 在Windows Shell中尝试上面的“ echo”行,看看会发生什么。 It may just work. 它可能会起作用。 I guarantee nothing. 我不保证。 :-) :-)

Your final PHP code might look something like this: 您最终的PHP代码可能如下所示:

$inkscape="c:/Program Files/Inkscape/Inkscape --shell";
$cmd="c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900";

exec(sprintf("echo '%s' | %s", $cmd, $inkscape), $output, $retval);
if ($retval!==false) {
  print "Success!\n";
}

Needless to say, this is untested, YMMV, may contain nuts. 不用说,未经测试的YMMV可能包含坚果。 But perhaps it helps. 但也许有帮助。 ;-) ;-)


UPDATE: 更新:

After having looked at the Inkscape man page , it looks like you could handle this through a pure command line, without the need for a pipe and --shell . 看完Inkscape手册页后 ,您似乎可以通过纯命令行来处理此问题,而无需使用pipe和--shell

What about just this? 那呢?

exec("c:/Program Files/Inkscape/Inkscape --file=c:/wamp/www/i/t1.svg --export-png=c:/wamp/www/i/r1.png --export-dpi=900", $output, $retval);

if ($retval>0)
    printf("ERROR (%d): %s\n", $retval, implode("\n", $output));

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM