分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

shell脚本--php执行shell命令

发布时间:2023-09-06 01:45责任编辑:熊小新关键词:shell

  这里只演示一些普通的shell命令,一些需要root用户权限执行的命令,请参考:php以root权限执行shell命令

  php执行shell命令,可以使用下面几个函数:

string system ( string $command [, int &$return_var ] )string exec ( string $command [, array &$output [, int &$return_var ]] )void passthru ( string $command [, int &$return_var ] )

  注意的是:这三个函数在默认的情况下,都是被禁止了的,如果要使用这几个函数,就要先修改php的配置文件php.ini,查找关键字disable_functions,将这一项中的这几个函数名删除掉,然后注意重启apache。

  首先看一下system()和passthru()两个功能类似,可以互换:

<?php ???$shell = "ls -la"; ???echo "<pre>"; ???system($shell, $status); ???echo "</pre>"; ???//注意shell命令的执行结果和执行返回的状态值的对应关系 ???$shell = "<font color=‘red‘>$shell</font>"; ???if( $status ){ ???????echo "shell命令{$shell}执行失败"; ???} else { ???????echo "shell命令{$shell}成功执行"; ???}?>

  执行结果如下:

  

  注意,system()会将shell命令执行之后,立马显示结果,这一点会比较不方便,因为我们有时候不需要结果立马输出,甚至不需要输出,于是可以用到exec()

  

  exec()的使用示例: 

<?php ???$shell = "ls -la"; ???exec($shell, $result, $status); ???$shell = "<font color=‘red‘>$shell</font>"; ???echo "<pre>"; ???if( $status ){ ???????echo "shell命令{$shell}执行失败"; ???} else { ???????echo "shell命令{$shell}成功执行, 结果如下<hr>"; ???????print_r( $result ); ???} ???echo "</pre>";?>

  运行结果如下:

  

shell脚本--php执行shell命令

原文地址:https://www.cnblogs.com/-beyond/p/8570654.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved