Hello.

How to write commands in exec() and arguments?

 exec("env -i ".$config->get('path_php')." $cli test &>/dev/null &"); 

or

 exec("fgh/gh -size 350x120 xc:black -font pointsize 70 -fill white -annotate +20+80 'текст' -trim +repage -bordercolor black -border 10 si57825b3af15b5.jpg"); 

Description of this feature: link to manual
There's not a word about it.

  • So the arguments in the exec do not register for an external program? - Mr. Black
  • @ Doofy. What do you have in mind? - max
  • Inside exec, the path to the program being run and the arguments for it are the same - Mr. Black
  • @Doofy. Which algorithm: first the path, then the arguments? How to find out what arguments to prescribe, and how? What programs can I run? - Max
  • Well, how is this "a word"? It is explicitly written: command Команда (имя программы с аргументами - прим.пер.), которая будет исполнена. " How to find out which arguments to prescribe ": man progname , progname --help - progname --help , google://progname , etc. What programs can I run? ” - which fantasies tell you. - PinkTux

1 answer 1

The PHP documentation mentions this all the way, since this topic actually goes beyond the language and interpreter, referring to other programs and the operating system where the script is executed.

In fact, you can run all console programs that are available in the system to the user, from which the program runs with exec() . While on Windows and other window systems you can run exec() and graphical programs. In unix-like systems, such programs are concentrated in bin-directories. In modern UNIX there are a lot of them, hundreds and thousands (some go by default, some should be set).

For example, the simplest utility to view the current directory ls , which can take the parameters -l (list) and -a (show everything, including hidden files). If it happens in Windows, you can use the dir ls -la instead of ls -la

 <?php exec('ls -la', $output); echo '<pre>'; print_r($output); 

Instead of exec() often more convenient to use shell_exec()

 <?php echo nl2br(shell_exec('ls -la')); 

Or use reverse quotes altogether.

 <?php echo nl2br(`ls -la`); 

The algorithm for the formation of such a path to the program or the name of the program, if it is available on the command line without a path (spelled out in the PATH environment variable). This is followed by zero, one or more program parameters. The format of the parameter depends on the program itself - what it expects as parameters. In some cases, you specify a parameter preceding it with two hyphens.

 man --help 

sometimes one dash

 ls -h 

Sometimes several parameters can merge into one

 ls -l -a ls -la 

Sometimes it is completely absent.

 eye i 

Sometimes after the parameters there may be values ​​that are assigned to the parameters.

 mysql -h localhost -u root -p 

As a rule, two hyphens indicate the full name of the parameter, and one - the abbreviated

 mysql --host localhost --user root --password 

What format of the parameters is adopted in each of the teams needs to be studied separately each time, for 40 years what programs have not been written and there is no hard standard.

In any operating system there are a lot of commands available on the command line immediately after installation. You can study them by studying the command line of your operating system (even in UNIX-like systems there are differences in the parameters; in Windows, the commands are quite different from the UNIX world).

In general, this is really a big and interesting topic, which is devoted quite a few books. As a starting point, I would advise:

  • Richard Bloom, Christina Bresnahan. Linux command line and shell scripts.
  • Eric C. Raymond. Unix programming art
  • Thanks for the detailed answer. - Max