PHP code example of technodelight / shell-exec

1. Go to this page and download the library: Download technodelight/shell-exec library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

technodelight / shell-exec example snippets




use Technodelight\ShellExec\Command;
use Technodelight\ShellExec\Exec;

$shell = new Exec('which');
$output = $shell->exec(
    Command::create()
        ->withArgument('php')
        ->withStdErrToStdOut()
        ->withStdOutTo('/dev/null') // command will be assembled as 'which php 2>'
);

var_dump($output); // will be ["/usr/bin/php"]



use Technodelight\ShellExec\Command;
use Technodelight\ShellExec\Exec;
use Technodelight\ShellExec\ShellCommandException;

try {
    $shell = new Exec;
    $shell->exec(
        Command::create('which')
            ->withArgument('nope') // command will be assembled as 'which nope'
    );
} catch(ShellCommandException $e) {
    // $e->getCode() will be the shell return code for the executed command.
    var_dump($e->getResult());
}