服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - php命令行写shell实例详解

php命令行写shell实例详解

2019-09-18 11:34月小升 PHP教程

这篇文章通过实例给大家介绍了php命令行写shell的方法,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧

php 可以像java perl python 那样运行,今天发现如果我早早知道这个,或许我不会去学习java 和 python

当年学java不过为了一个程序放在服务器上,不停的跑啊跑,原来 php 也可以。

php -h
Usage: php [options] [-f] <file> [--] [args...]
 php [options] -r <code> [--] [args...]
 php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
 php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
 php [options] -S <addr>:<port> [-t docroot] [router]
 php [options] -- [args...]
 php [options] -a
 -a    Run as interactive shell
 -c <path>|<file> Look for php.ini file in this directory
 -n    No configuration (ini) files will be used
 -d foo[=bar]  Define INI entry foo with value 'bar'
 -e    Generate extended information for debugger/profiler
 -f <file>  Parse and execute <file>.
 -h    This help
 -i    PHP information
 -l    Syntax check only (lint)
 -m    Show compiled in modules
 -r <code>  Run PHP <code> without using script tags <?..?>
 -B <begin_code> Run PHP <begin_code> before processing input lines
 -R <code>  Run PHP <code> for every input line
 -F <file>  Parse and execute <file> for every input line
 -E <end_code> Run PHP <end_code> after processing all input lines
 -H    Hide any passed arguments from external tools.
 -S <addr>:<port> Run with built-in web server.
 -t <docroot>  Specify document root <docroot> for built-in web server.
 -s    Output HTML syntax highlighted source.
 -v    Version number
 -w    Output source with stripped comments and whitespace.
 -z <file>  Load Zend extension <file>.
 args...   Arguments passed to script. Use -- args when first argument
     starts with - or script is read from stdin
 --ini   Show configuration file names
 --rf <name>  Show information about function <name>.
 --rc <name>  Show information about class <name>.
 --re <name>  Show information about extension <name>.
 --rz <name>  Show information about Zend extension <name>.
 --ri <name>  Show configuration for extension <name>.

1.用php命令行的方式执行php脚本,例如/usr/bin/php test.php

缩写 php test.php

test.php

<?php
for($i=0;$i<10;$i++){
 echo $i;
 echo '\n';
}
?>

2.脚本开头第一行写上#!/usr/bin/php,然后可以把脚本设为可执行 chmod a+x test.php,之后就可以用命令行的方式直接执行脚本了,例如./test.php

#!/usr/bin/php
<?php
for($i=0;$i<10;$i++){
 echo $i;
 echo " java-er.com \n";
}
?>

执行一小时,看看php会不会挂,我希望一个命令行可以跑到天荒地老

#!/usr/bin/php
<?php
for($i=0;$i<360;$i++){
 echo $i;
 sleep(10);
 echo " java-er.com \n";
}
?>

php命令行写shell实例详解

3. 外部传入参数

#!/usr/bin/php
<?php
 var_dump($argc); //返回参数总个数
 var_dump($argv);
 
exit;
?>
./test.php

int(1)
array(1) {
 [0]=>
 string(10) "./test.php"
}
./test.php a java php

int(4)
array(4) {
 [0]=>
 string(10) "./test.php"
 [1]=>
 string(1) "a"
 [2]=>
 string(4) "java"
 [3]=>
 string(3) "php"
}

总结

以上所述是小编给大家介绍的php命令行写shell实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

延伸 · 阅读

精彩推荐
  • PHP教程浅谈php使用curl模拟多线程发送请求

    浅谈php使用curl模拟多线程发送请求

    这篇文章主要介绍了php使用curl模拟多线程发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    洛卡卡了3062019-06-03
  • PHP教程PHP addAttribute()函数讲解

    PHP addAttribute()函数讲解

    今天小编就为大家分享一篇关于PHP addAttribute()函数讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    php参考手册2012019-06-23
  • PHP教程Laravel关联模型中过滤结果为空的结果集(has和with区别)

    Laravel关联模型中过滤结果为空的结果集(has和with区别)

    这篇文章主要介绍了Laravel关联模型中过滤结果为空的结果集(has和with区别),需要的朋友可以参考下...

    Rootrl3432019-09-01
  • PHP教程PHP内部实现打乱字符串顺序函数str_shuffle的方法

    PHP内部实现打乱字符串顺序函数str_shuffle的方法

    这篇文章主要给大家介绍了关于PHP内部实现打乱字符串顺序函数str_shuffle的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...

    写PHP的老王4282019-06-15
  • PHP教程PHP中如何使用Redis接管文件存储Session详解

    PHP中如何使用Redis接管文件存储Session详解

    这篇文章主要给大家介绍了关于在PHP中如何使用Redis接管文件存储Session的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    SangSir5042019-08-16
  • PHP教程php中访问修饰符的知识点总结

    php中访问修饰符的知识点总结

    在本篇文章里小编给大家分享了关于php中访问修饰符的知识点总结,兴趣的朋友们可以学习参考下。...

    laozhang3642019-06-28
  • PHP教程PHP基于mcript扩展实现对称加密功能示例

    PHP基于mcript扩展实现对称加密功能示例

    这篇文章主要介绍了PHP基于mcript扩展实现对称加密功能,结合实例形式简单分析了php使用mcript扩展进行加密与解密相关操作技巧,需要的朋友可以参考下...

    ztblog5112019-06-11
  • PHP教程PHP通过GD库实现验证码功能示例

    PHP通过GD库实现验证码功能示例

    这篇文章主要介绍了PHP通过GD库实现验证码功能,结合实例形式分析了php验证码相关图片绘制、输出操作实现技巧,需要的朋友可以参考下...

    不能吃的坚果4362019-06-10