Home  >  Article  >  Backend Development  >  php如何执行多条shell命令?

php如何执行多条shell命令?

PHPz
PHPzOriginal
2016-06-20 13:05:223422browse

PHP执行多条shell命令的方法:1、将多条shell命令放到同一shell文件中,然后使用system()执行该shell文件即可;2、将多条shell命令直接写在一条语句里,然后使用system()执行该语句即可。

php如何执行多条shell命令?

php如何执行多条shell命令?

php下执行shell命令可以用system、exec之类的,但是每个单独的system命令都是分别调用shell的,每次的环境都要重新初始化,因此多条命令连续的操作不能通过多条system实现功能。

于是就有两种方法来解决这种问题:

1、用php生成shell文件并执行它,完事后删除

file_put_countents('tmp.sh',"cd /usr/local echo 'string' ls $callback");
system('tmp.sh');

多条命令,直接换行就可以了

2、多条命令直接写在一条语句里

system("cd {$path1};if [ ! -d {$path2} ];then \n { mkdir -p {$path2} \n chmod -R 777 {$path2} \n } \n fi;find -name '{$etaskid}-{$sid}*' -print | while read name;do \n mv \$name/000000_0 \$name/\$name \n mv \$name/\$name ../download/{$folder}/{$sid}/ \n rm -r \$name \n done ");

很明显的缺点就是语句过长,不好写。

更多相关知识,请访问 PHP中文网!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn