In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the use of shell scripts in PHP, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
As we all know, in computer science, SHELL is similar to command.com under DOS. It receives user commands and then invokes the appropriate application. At the same time, it is also a programming language. As a command language, it interactively interprets and executes commands entered by users or automatically interprets and executes a series of predetermined commands; as a programming language, it defines a variety of variables and parameters. it also provides many control structures that are unique in high-level languages, including loops and branches.
PHP after a long period of development, many users are very familiar with PHP, here I would like to publish a personal understanding, and discuss with you. In most cases, I use the exec () command and the data array to handle everything. Or use shell_exec () for simpler commands, especially if you don't care about the results. If I only need to return a PHP shell script, I use passthru (). Usually, I use different functions in different situations, and sometimes they are interchangeable.
It all depends on my mood and what I want to achieve. Another question you may ask is "what are their strengths?" If you don't have a clue, or if a project is perfect for using the shell command but don't know how to use it, I'll give you some insights here. If you are writing an application that provides various backup or file transfer capabilities, you can choose to run rsync-supported PHP shell scripts using shell_exec () or one of the other commands provided here. You can write a PHP shell script to contain the necessary rsync commands, and then use passthru () to execute it based on the user's command or cron job.
For example, a user who has appropriate privileges (such as administrator privileges) in your application wants to send 50 PDF files from one server to another. Then, the user needs to navigate to the correct location in the application, click Transfer, select the PDF to be sent, and then click Submit. In the process, the form should have a PHP script that runs the rsync script through passthru () with the return option variable, so you know if there is a problem, as shown below.
Listing 1. A sample PHP script that runs the rsync script through passthru ()
If your application needs to list processes or files, or data about those processes or files, you can easily do so using one of the commands summarized in this article. For example, a simple grep command can help you find files that match specific search criteria. Using it with the exec () command saves the results to an array, which allows you to build an HTML table or form, which in turn allows you to run other commands.
So far, I've discussed user-generated events-- PHP runs the script as soon as the user presses a button or clicks a link. You can also use stand-alone PHP scripts with cron or other schedulers to achieve some interesting effects. For example, if you have a backup script, you can run it through cron, or package it into a PHP script and run it.
Why would you do that? It seems superfluous, doesn't it? That's not true-- you need to think that you can run the backup script through exec () or passthru (), and then perform some behavior based on the return code. If an error occurs, you can log it to the error log or database, or send a warning email. If the script is successful, you can dump the original output to the database (for example, rsync has a verbose mode that is useful for later diagnosing problems).
Safety
Let's briefly discuss security here: if you accept user input and pass the information to shell, then * filter user input. Delete commands that you consider harmful and content that is not allowed, such as sudo (run as superuser) or rm (delete). In fact, you may not want users to send open requests, but rather let them choose from the list.
For example, if you run a transfer program that accepts a list of files as a parameter, you should list all files through a series of check boxes. Users can select and deselect files and activate the rsync shell script by clicking Submit. Users cannot enter files or use regular expressions themselves.
After reading the above, do you have any further understanding of how to use shell scripts in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.