1. Basic Knowledge
1.1 What is Shell programming?
In Unix, shell is not a simple command interpreter (typical DOS in Windows), but a fully functional programming environment. Shell is a part of the operating system, used to deal with users, and can be used to coordinate various commands [1 ]. It is very convenient to use Shell programming to flexibly solve a large number of repeated tasks. However, Shell syntax is very weird (personal opinion) and cannot be easily remembered, if the familiar language can be used to write the shell, it will be fine-for example, php-, you can quickly develop the Shell program (for example, the background program of my Preminder), so you will have this article, this document uses Linux as an example to describe how to use php-cli. The versions on other platforms are similar.
1.2 What is php-cli?
As mentioned earlier, we can use php to develop Shell programs. Some may ask: "isn't php used for Web pages? -_-". Yes, php can be used for dynamic web pages, and php was originally developed for dynamic web pages. However, in theory, php can be used for any program or desktop program, the php-cli is the support environment for php to run on the command line, that is, the Environment Support for writing Shell.
Php-cli is short for php Command Line Interface. It is called the Interface that php runs on the Command Line. It is different from the php environment (php-cgi, isapi, etc.) [2 ].
That is to say, php can not only write front-end webpages, but also be used to write background programs.
2. Execute the php-cli script
2.1 php-cli Syntax
Of course it is exactly the same as php, because it is php! However, some default parameters are different from those of php-cgi. For example, the default runtime of php-cli is infinite, while that of php on the webpage is set to 30 s by default.
2.2 run the php-cli script
2.2.1. Execute php directly on the terminal
Kangzj @ localhost # php-r 'print _ r (get_defined_constants (); '2. 2.2. Run the php-cli script file
Kangzj @ localhost # php my_script.php
On kangzj @ localhost # php-f my_script.php, the php file is a common php file. Another way is to run the interpreter in the file. You can run the script directly on the terminal with "./test. php". test. php is like the following:
Copy codeThe Code is as follows:
#! /Usr/bin/php-q
<? Php
Echo "Hello world of php cli! ";
?>
Supplement: php Shell programs do not necessarily use php as the extension. They can use any extension, or even do not use the extension, just to be clear, I used the php extension.
2.2.3. Run the php-cli script with Cron
Cron is a scheduled execution tool in linux. You can run jobs and periodic jobs without manual intervention, such as backing up data and regularly querying PR by Preminder: open/etc/crontab and add:
0 13 ***/usr/bin/php-f/home/phpscripts/phpcli. php
4. Conclusion
If you have php, you will also have a Shell programming language!
If you don't know php, learning php is equivalent to learning dynamic web pages and Shell languages at once! You can even use php to write applications with graphical interfaces. One of the dynamic domain name clients of Dnspod is developed using php.
Php is famous for its easy to learn. If you still don't, what are you waiting?
In addition, publicize my "Preminder"-PR update Email reminder service ~~
5. References
1. Linux Shell Introduction: http://www.jb51.net/article/37801.htm
2. PHP Command Line Interface: Mystic Unleashed: http://www.php-cli.com/
3. ch 4.2, php manual: http://www.php.net
PS: You still need to know some Shell commands, otherwise some functions are not very good.