脚本文件为: phpCli linux文件路径: /usr/local/bin/phpCli (须要可执行权限 chmod +x phpCli) linux执行: phpCli window执行: php phpCli
#!/usr/local/bin/php -Cq
var_dump($argv); /*array(2) { [0]=> string(6) "phpCli" [1]=> string(4) "test" }*/
运行:phpCli test ;
cli 模式下,参数会保存在$argv中.php
print 'please input something !'."\n"; $message = trim(fgets(STDIN)); var_dump($message); //test
标准输入在 PHP流的 STDIN 中,或者 unix风格的 '终端输入' 设备 /dev/tty 中得到.
运行:phpCli 会获得提示语 please input something ! .
输入信息 test ,var_dump($message)会输出 test .linux
//这里使用了自定义 switch ('test') { case '-m': $this->shell = 'php -m'; break; case '-v': echo $this->command.' 1.0.1-dev (cli)'; $this->putFormat(true); break; case '-h': case '-help': $this->help(); break; default: $this->help(); break; }
PEAR 提供了 Console_Getopt包能够同时支付简短和长格式(GNU风格)的选项.
默认是与PHP绑定安装的,除非你关闭了PEAR .
也能够自行定义 .docker
/*pcntl_fork — 在当前进程当前位置产生分支(子进程)。 译注:fork是建立了一个子进程,父进程和子进程 都从fork的位置开始向下继续执行, 不一样的是父进程执行过程当中,获得的fork返回值为子进程 号,而子进程获得的是0。 */ $pid = pcntl_fork(); //父进程和子进程都会执行下面代码 if ($pid == -1) { //错误处理:建立子进程失败时返回-1. die('could not fork'); } else if ($pid) { //父进程会获得子进程号,因此这里是父进程执行的逻辑 echo 'this is parent test' ; pcntl_wait($status); //等待子进程中断,防止子进程成为僵尸进程。 } else { //子进程获得的$pid为0, 因此这里是子进程执行的逻辑。 echo 'this is son test'; }
#!/usr/local/bin/php -Cq <?php class PHPCli { /** 命令名称 * @var string */ private $command = 'phpCli'; /** 输入数据 * @var */ private $initData ; /** 首个数据 * @var */ private $initFirst ; /** 格式化输出 * @var bool */ private $format = true; /** @var array 命令支持 */ private $runCommand = ['ds','dsa'] ; /** @var array */ private $shellMap = [ 'ds' => 'docker ps', 'dsa' => 'docker ps -a' ] ; /** 执行shell * @var */ private $shell ; /** * PHPCli constructor. * @param $argv */ public function __construct($argv) { # 基本参数 入口文件 $this->initFirst = $argv[0] ; array_shift($argv); $this->initData = $argv ; } /** 格式输出 * @param bool $end */ private function putFormat($end = false) { print "\n"; if($end) { exit(); } } /* * 使用说明 */ private function help() { $this->putFormat(); print 'Usage: '.$this->command.' Command'."\n"; $this->putFormat(); print 'Options:'."\n"; print ' -v Show phpCli version'."\n"; print ' -m Show php model'."\n"; print ' -h Display this help'."\n"; $this->putFormat(); print 'Commands:'."\n"; print ' ds Run docker command `docker ps`'."\n"; print ' dsa Run docker command `docker ps -a`'."\n"; $this->putFormat(); exit(); } /** shell运行 * @return mixed */ private function shell() { if(!$this->shell) { exit(); } if($this->format) { //$status 格式输出 system($this->shell, $status); }else{ // $status 以数组形式返回 exec($this->shell, $status); } //passthru(); return $status ; } /** * 功能入口 */ public function run() { $label = $this->initData[0] ?? '' ; if(empty($label)) { $this->help(); } if($label[0] == '-') { switch ($label) { //可扩展其它短命令 case '-m': $this->shell = 'php -m'; break; case '-v': echo $this->command.' 1.0.1-dev (cli)'; $this->putFormat(true); break; case '-h': case '-help': $this->help(); break; default: $this->help(); break; } }else{ if(in_array($label,$this->runCommand)) { //可扩展更多shell $this->shell = $this->shellMap[$label]; }else{ echo "Run '".$this->command." -help' for more information on a command."; $this->putFormat(true); } } $this->shell(); } } $phpCli = new PHPCli($argv); $phpCli->run(); exit();
运行: phpCli shell
运行: phpCli -m数组
运行:phpCli dsathis