简体   繁体   中英

--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.

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() .

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. When running the batch file, the batch closes at that command. When running php exec() , no more commands work after that command is called. 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.

When I removed --shell from both the batch file, it gives me an inkscape error

(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.

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. 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 . 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. 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.

Here are the options: 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. If that works the same in your environment as it would in a Unix system, then there may be an easy solution.

First off, note the notation you've used in your exec() :

commandone && commandtwo

This is not the same as typing one line (commandone) and then typing another line (commandtwo). Instead, what it does is to run commandone, and if it completes (finishes) successfully, run commandtwo. This is obviously not what you want.

Instead, in a Unix environment, you might try something like this:

echo "somestring" | commandone

This makes "somestring" the input to "commandone", as if you had typed it into the terminal. 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 .

Alternately if you are using bash as your shell, another notation might be:

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.

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.

Try the "echo" line above in your Windows shell, and see what happens. It may just work. I guarantee nothing. :-)

Your final PHP code might look something like this:

$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. 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 .

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));

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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