1
$command1 = "interfacename -S ipaddress -N nms -P company ";
$command2 = "list search clientclass hardwareaddress Mac address ";
if ( exec( $command1 . "&&" . $command2 ) ) {
    echo "successfuly executed";
} else {
    echo "Not successfuly executed";
}

If command 1 (cmd query) successfully executed, I want command 2 (which also contains some cmd queries) to be executed next. In the above script, only command 1 is executed. It doesn’t show any result for command 2.

I have wasted two days on this without finding any solution.

Rehman Ali
  • 23
  • 1
  • 8

4 Answers4

0

You can use shell_exec() PHP function to run Shell Command directly in your script.

Syntax : string shell_exec (string $cmd)

Example :

$output = shell_exec('ls -lart');
var_dump($output);  #Showing the outputs

You can use multiple conditions in a single command line.

Example :

$data   = "rm a.txt && echo \"Deleted\"";
$output = exec($data);
var_dump($output);

if($output=="Deleted"){
   #Successful
}

In above example "Deleted" string will assign to $output when the file deleted successfully. Otherwise the error/warning/empty string will assign to $output variable. You should make condition with $output string.

Here is the documentation of shell_exec()

Note : There will be a new line character of the function shell_exec() output.

Sumon Sarker
  • 2,707
  • 1
  • 23
  • 36
0

You can use either a ; or a && to separate the comands. The ; runs both commands unconditionally. If the first one fails, the second one still runs. Using && makes the second command depend on the first. If the first command fails, the second will NOT run. Reference

krishnaraj
  • 41
  • 6
0

If I understand your question correctly, you want to execute $command1 and then execute $command2 only if $command1 succeeds.

The way you tried, by joining the commands with && is the correct way in a shell script (and it works even with the PHP function exec()). But, because your script is written in PHP, let's do it in the PHP way (in fact, it's the same way but we let PHP do the logical AND operation).

Use the PHP function exec() to run each command and pass three arguments to it. The second argument ($output, passed by reference) is an array variable. exec() appends to it the output of the command. The third argument ($return_var, also passed by reference) is a variable that is set by exec() with the exit code of the executed command.

The convention on Linux/Unix programs is to return 0 exit code for success and a (one byte) positive value (1..255) for errors. Also, the && operator on the Linux shell knows that 0 is success and a non-zero value is an error.

Now, the PHP code:

$command1 = "ipcli -S 192.168.4.2 -N nms -P nmsworldcall ";
$command2 = "list search clientclassentry hardwareaddress 00:0E:09:00:00:01";

// Run the first command
$out1  = array();
$code1 = 0;
exec($command1, $out1, $code1);

// Run the second command only if the first command succeeded
$out2  = array();
$code2 = 0;
if ($code1 == 0) {
    exec($command2, $out2, $code2);
}

// Output the outcome
if ($code1 == 0) {
    if ($code2 == 0) {
       echo("Both commands succeeded.\n");
    } else {
       echo("The first command succeeded, the second command failed.\n");
    }
} else {
    echo("The first command failed, the second command was skipped.\n");
}

After the code ends, $code1 and $code2 contain the exit codes of the two commands; if $code1 is not zero then the first command failed and $code2 is zero but the second command was not executed.

$out1 and $out2 are arrays that contain the output of the two commands, split on lines.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • In above code command 2 isn't executed.It directly goes to else part and echo(The first command succeeded, the second command failed.\n) – Rehman Ali Sep 05 '17 at 10:30
  • It reports what happened. The second command returned an exit code different than `0` or it didn't run at all. What is the value of `$code2`? – axiac Sep 05 '17 at 11:10
0

I'm not sure to know about simultaneous execution but I'm sure about one cmd dependent on another cmd execution action. Here I'm running single execution cmd first to clear all set path, second I've declared my file path, third I install angular cmd npm install.

    $path = "D:/xampp/htdocs/tests/omni-files-upload/aa-test/src";
        $command_one = "cd /";
        $command_two = "cd ".$path;
        $command_three = "npm install";
        
        @exec($command_one."&& ".$command_two."&& ".$command_three);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129