平时工作中很少用到proc_open这个函数,最近用这个函数做了一个数据库管理小程序。
实现如下:
//file name db.php$mysql_list = array( 'test' => 'mysql -hlocalhost -utest -p123456 -P3306 test', 'test2' => 'mysql -h172.16.190.199 -utest2 -p654321 -P3306 test2',);$desc = array( 0 => STDIN, 1 => STDOUT, 2 => STDERR,);$flag = @$argv[1];$env = $_ENV;$cwd = getcwd();if(!isset($mysql_list[$flag])){ exit("can not found the $flag");}$re = proc_open($cmd, $desc, $pipes, $cwd, $env);if(!$re){ exit('failed to execute cmd');}
这样有很多数据库需要管理的时候就比较容易了,直接配置下连接参数,然后:
php db.php test
简单实用吧~
补一个bash实现的。
#!/bin/bashflag=$1;case $flag in 'test') (mysql -hlocalhost -utest -p123456 -P3306 test); ;; 'test2') (mysql -h172.16.190.199 -utest2 -p654321 -P3306 test2); ;; *) echo 'can not found the flag cmd'; ;;esac;
评论| 作者: tomheng 85159e08d5f1f50d244825cb03e92c65 爱情从希望开始,也由绝望结束。死心了,