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, but php can be used for any program in theory,
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:
The code is as follows:
#! /Usr/bin/php-q
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:
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.bitsCN.com/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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.