php如何执行多条shell命令?

PHPz
Release: 2020-09-05 09:59:39
Original
3423 people have browsed it

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');
Copy after login

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

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 ");
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!