php cli Introduction of does not use Shell as Shell does

  • 2020-06-07 04:04:23
  • OfStack

1. Basics

1.1 What is Shell programming?
In Unix, shell is not a simple command interpreter (typically DOS as in Windows), but a fully functional programming environment. Shell is a part of the operating system that deals with users and can be used to coordinate commands [1]. Programming with Shell allows you to solve a large number of repetitive tasks in 10 minutes. But Shell grammar 10 weird (personal opinion), is not easy to remember, if now familiar with the language can be used to write shell it would be nice - such as php - can quickly develop Shell program (for example, my Preminder daemon), then this article, this article shows php Linux - cli usage, similar to other platform version.

1.2 What is php-ES21en?
As mentioned earlier, we can use php to develop Shell programs. Some of you might ask, "Isn't php for web pages? -_ - ". Yes, php can be used for dynamic web pages, and php was originally developed for dynamic Web pages, but in theory php can be used for anything, even desktop applications, and ES29en-ES30en is the environment that php runs on the command line, which is what we call the environment that can be used to write Shell.

php-cli is short for php Command Line Interface, as the name implies, the interface that php runs on the command line, as opposed to the php environment (ES44en-ES45en, isapi, etc.) that runs on Web servers [2].

In other words, php can not only write foreground pages, it can also be used to write background programs.

2. Execute the php-ES53en script

2.1 Syntax of ES56en-ES57en
Of course, it is the same as php1, because it is php! However, some default parameters are different from ES61en-ES62en, such as the running time: ES63en-ES64en runs to infinity, while php is set to 30s.

2.2 Execute the ES69en-ES70en script

2.2.1. Execute php directly on the terminal
kangzj @ # localhost php - r 'print_r (get_defined_constants ()); '2.2.2. Run the ES84en-ES85en script file
kangzj@localhost# php my_script.php

kangzj @localhost # php-f my_script. php and the php file is 1 php file is no different. Another option is to use the instruction interpreter in a file, so that the script can be executed directly at the terminal with "./ test.php ". test.php looks like this:

#!/usr/bin/php -q 
echo "Hello world of PHP CLI!"; 

Supplement: Shell's Shell program does not specify php as an extension. You can use any extension, or even no extension, just to be clear, I use the php extension.

2.2.3. Execute es115EN-ES116en script with Cron
cron is a timing execution tool under linux, which can run the job without human intervention, periodic job, such as backup data, Preminder regularly query PR, etc. Add method: open /etc/crontab, add:

0 13 * * * /usr/bin/php -f /home/phpscripts/phpcli.php

4. Conclusion
If you know php, you know an Shell programming language!

If you don't know php, you can learn php and learn dynamic web and Shell! And you can even write graphical applications using php, one of Dnspod's dynamic DNS clients was developed using php.

php is famous for its easy learning. If you don't know it yet, what are you hesitating about?

In addition, I would like to promote my "Preminder" - PR update Email reminder service ~~

5. References
1. Linux Shell introduction: https: / / www ofstack. com article / 37801. htm
2. PHP Command Line Interface: Mystic Unleashed: http: / / www php - cli. com /
4.2, 3. ch php manual: http: / / www php. net
PS: Shell command must know 1, otherwise some functions are not very easy to implement.

Related articles: