1. Go to this page and download the library: Download phpsu/shellcommandbuilder 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/ */
phpsu / shellcommandbuilder example snippets
use PHPSu\ShellCommandBuilder\ShellBuilder;
$builder = new ShellBuilder();
$builder
->add('a')
->and('b')
->pipe('c')
->or('d')
->pipeWithForward('f')
->and(
$builder->createGroup()->add('g')->and('h')
)
->or(
$builder->createGroup(true)->add('i')->or('j')
);
use PHPSu\ShellCommandBuilder\ShellBuilder;
$builder = new ShellBuilder();
use PHPSu\ShellCommandBuilder\ShellBuilder;
// 1. First we create the command `echo`
$echo = ShellBuilder::command('echo');
// 2. "hello world" is an argument, that we can add like this:
$echo->addArgument('Hello World');
// 3. we create the `grep` command
$grep = ShellBuilder::command('grep');
// 4. and add the option '-e "world"'.
// the single hyphen that is before the option "e" marks it as a *short* option (addShortOption)
// Having two hyphens like --color makes it a regular option (addOption)
$grep->addShortOption('e', 'world');
// 5. Now we need combine those two commands together
// We do that, by creating a ShellBuilder
$builder = ShellBuilder::new();
// 6. And then adding the echo-command into it
$builder->add($echo);
// 7. Earlier we saw, that these two commands where held together by the pipe-Operator
// This can be accomplished by using the pipe-Method
$builder->pipe($grep);
// 8. To use this command in e.g. shell_exec, you can convert it into a string and use it
shell_exec((string)$builder); // -> echo 'hello world' | echo -e 'world'
use PHPSu\ShellCommandBuilder\ShellBuilder;
$builder = new ShellBuilder();
// adding the initial command
$builder->add('a');
// adding the next command for `;`
$builder->add('b');
// combining with and --> `&&`
$builder->and('c');
// piping the output --> `|`
$builder->pipe('d');
// combining with or --> `||`
$builder->or('e');
// piping the output including the error --> `|&`
$builder->pipeWithForward('f');
// redirect stderr to stdout --> `2>&1`
$builder->redirectErrorToOutput();
use PHPSu\ShellCommandBuilder\ShellBuilder;
$builder = new ShellBuilder();
// creating the first command.
// The 'true' removes the connection between ShellBuilder and ShellCommand and makes it anonymous.
// This is the same result as ShellBuilder::command()
$mysqlDump = $builder->createCommand('mysqldump', true)
// adding the options and short-options
->addOption('opt')
->addOption('skip-comments')
->addOption('single-transaction')
// the signature of options have four variables
// 'lock-tables' is the name of the option --> "--lock-tables"
// the string 'false' is the value --> "--lock-tables 'false'"
// the third variable disables escaping --> "--lock-tables false"
// the fourth variable turns the space between name and value into '=' --> "--lock-tables=false"
->addOption('lock-tables', 'false', false, true)
->addShortOption('h', 'database')
->addShortOption('u', 'root')
->addShortOption('p', 'root')
->addArgument('sequelmovie')
->addToBuilder();
$builder->createCommand('ssh')
->addShortOption('F', 'php://temp')
->addArgument('hostc')
// SubCommand is technically an argument, that always escapes the output
->addSubCommand(
$mysqlDump->pipe(
// 'createGroup' flags a ShellBuilder to wrap the commands in braces e.g (echo "hello world")
$mysqlDump->createGroup()
->createCommand('echo')
->addArgument('CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;')
->addToBuilder()
->and('cat')
)
)
->addToBuilder()
->pipe(
$builder->createCommand('mysql')
->addShortOption('h', '127.0.0.1')
// disabling escaping here: --> "-P 2206"
->addShortOption('P', '2206', false)
->addShortOption('u', 'root')
->addShortOption('p', 'root')
)
;
use PHPSu\ShellCommandBuilder\ShellBuilder;
$builder = ShellBuilder::new()
->createCommand('cat')
// the false at the end prints the argument unescaped
->addArgument(
ShellBuilder::new()
// turning all commands within this builder into a process substitution --> <(command ...)
// the same would work with `createCommandSubstition` resulting in something like this $(command ...)
->createProcessSubstition()
->createCommand('ls')
// currently combining short-options has to be done manually, although it could change in the future
// but doing it like this will always be possible, since it's impossible to evaluate the correctness
// without having the man-page of all the commands available
->addShortOption('1ARSsD')
->addToBuilder()
->pipe(
ShellBuilder::command('grep')
->addArgument('.*\.php')
),
false
)
->addToBuilder()
// redirects stdout from the previous command and pushes it into stdin of the next command
// if redirected into a file, the true at the end changes the type to appending instead of overwriting --> "a >> b"
->redirectOutput(
ShellBuilder::new()
->createCommand('date')
->addArgument('+%B', false)
// this is similar to the process/command-substitition from above but here it is applied on a command instead
// toggling means that instead of taking true or false as an argument it flips the internal state back and forth
->toggleCommandSubstitution()
->addToBuilder()
->addFileEnding('txt'),
true
)
;
use PHPSu\ShellCommandBuilder\ShellBuilder;
use PHPSu\ShellCommandBuilder\Conditional\FileExpression;
use PHPSu\ShellCommandBuilder\Conditional\ArithmeticExpression;
# 1:
ShellBuilder::new()
->add(FileExpression::create()->notEmpty('test.php'))
->and(ShellBuilder::command('echo')->addArgument('hello'))
;
# 2:
ShellBuilder::new()
// adding a variable "a" with the value "6"
// the third argument replaces $() through backticks --> a=$(cat) ~> a=`cat`
// the fourth argument sets escpaing to false.
// Escaping is disabled for commands as value.
->addVariable('a', '6', false, false)
->add(ArithmeticExpression::create()->greater('$a', '5'))
->and(ShellBuilder::command('echo')->addArgument('hello'))
;
# 3:
ShellBuilder::new()
->addVariable('a',
ShellBuilder::new()
->createCommand('cat')
->addNoSpaceArgument('file')
->addToBuilder()
->addFileEnding('txt'),
true // enable backticks
)
->add(ArithmeticExpression::create()->greater('$a', '5')->escapeValue(true))
->and(ShellBuilder::command('echo')->addArgument('hello'))
;
use PHPSu\ShellCommandBuilder\Definition\GroupType;
use PHPSu\ShellCommandBuilder\ShellBuilder;
// we first create a new ShellBuilder, that will be wrapped in the group-syntax that does not open a subshell
// -> { command-list ;}
$builder = new ShellBuilder(GroupType::SAMESHELL_GROUP);
// then we set that builder to be asynchronous.
// the second argument of this method gives the coprocess a name.
// default is no name
// -> coproc [NAME] command
$builder->runAsynchronously(true)
->createCommand('tee')
->addArgument(
// createGroup again wraps it into a group-syntax and the true indicates, that is is in the same-shell notation
// false would open a subshell like e.g ( command ).
// default is false
$builder->createGroup(true)
->createCommand('tee')
->addArgument('logfile', false)
->addToBuilder(),
false
)
->addToBuilder()
// redirectDescriptor is the more powerful way of writing redirects between File Descriptors
// argument 1: command that we redirect from/to
// argument 2: direction of the redirect (true: >&, false <&)
// argument 3: file descriptor before redirection
// argument 4: file descriptor after redirection
// the example below would render: >&3
->redirectDescriptor('', true, null, 3);
ShellBuilder::new()->add($builder)->redirectDescriptor('', true, 3, 1);
use PHPSu\ShellCommandBuilder\ShellBuilder;
ShellBuilder::new()->add('./import-script')->async('./import-script2')->async();
use PHPSu\ShellCommandBuilder\Definition\Pattern;
Pattern::split('three blind mice');
// ['three', 'blind', 'mice']
use PHPSu\ShellCommandBuilder\Definition\Pattern;
Pattern::split("a \"b c d e");
// ShellBuilderException::class
// The given input has mismatching Quotes