php编写函数6,PHP执行linux命令6个函数代码实例

一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec。其实有很多的,结合手册内容,介绍以下6个函数。

1,exec函数

$test = "ls /tmp/test"; //ls是linux下的查目录,文件的命令

exec($test,$array); //执行命令

print_r($array);

?>

返回结果如下:

[root@krlcgcms01 shell]# php ./exec.php

Array

(

[0] => 1001.log

[1] => 10.log

[2] => 10.tar.gz

[3] => aaa.tar.gz

[4] => mytest

[5] => test1101

[6] => test1102

[7] => weblog_2010_09

)

2,system函数

$test = "ls /tmp/test";

$last = system($test);

print "last: $last\n";

?>

返回结果:

[root@krlcgcms01 shell]# php system.php

1001.log

10.log

10.tar.gz

aaa.tar.gz

mytest

test1101

test1102

weblog_2010_09

last:weblog_2010_09

3,passthru函数

$test = "ls /tmp/test";

passthru($test);

?>

4,popen函数

$test = "ls /tmp/test";

$fp = popen($test,"r"); //popen打一个进程通道

while (!feof($fp)) { //从通道里面取得东西

$out = fgets($fp, 4096);

echo $out; //打印出来

}

pclose($fp);

?>

5,proc_open函数

$test = "ls /tmp/test";

$array = array(

array("pipe","r"), //标准输入

array("pipe","w"), //标准输出内容

array("pipe","w") //标准输出错误

);

$fp = proc_open($test,$array,$pipes); //打开一个进程通道

echo stream_get_contents($pipes[1]); //为什么是$pipes[1],因为1是输出内容

proc_close($fp);

?>

6,shell_exec函数

$test = "ls /tmp/test";

$out = shell_exec($test);

echo $out;

?>

popen,passthru,proc_open,shell_exec的返回结果如下:

[root@krlcgcms01 shell]# php test.php

1001.log

10.log

10.tar.gz

aaa.tar.gz

mytest

test1101

test1102

weblog_2010_09

我能发现的就这几个函数,能执行linux下的命令,我想应当还有吧,欢迎大家补充。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。 ad51e517755f8fd6a7ec83ced4ecfaf3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Shell中的函数执行Linux命令。以下是一个示例函数的定义和使用: ```shell # 定义一个函数函数名为my_function my_function() { # 在函数执行Linux命令 echo "Hello, World!" ls -l } # 调用函数 my_function ``` 在这个例子中,我们定义了一个名为my_function的函数函数体内部执行了两个Linux命令:echo和ls。在函数定义后,通过调用函数名来执行函数。 如果你想在Linux系统启动时执行一个脚本,你可以将你的脚本添加到启动文件中,并赋予可执行权限。启动文件的位置取决于你使用的Linux发行版。在CentOS中,可以使用`/etc/rc.d/rc.local`文件。你可以在这个文件中添加你的脚本,并确保它具有可执行权限。下面是一个示例: 1. 打开终端并使用root权限登录。 2. 执行以下命令将你的脚本添加到`/etc/rc.d/rc.local`文件中并赋予可执行权限: ```shell echo "path/to/your/script.sh" >> /etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local ``` 这样,你的脚本将在系统启动时自动执行。 你可以在中找到更多关于在CentOS中编写需要开机启动的脚本的详细说明。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [linux常用命令函数](https://blog.csdn.net/wym2011aaj/article/details/97146770)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值