Home > Backend Development > PHP Tutorial > PHP tutorial: How to use shell scripts in PHP_PHP tutorial

PHP tutorial: How to use shell scripts in PHP_PHP tutorial

WBOY
Release: 2016-07-20 10:57:42
Original
914 people have browsed it

We all know that in computer science, SHELL is similar to command.com under DOS. It receives user commands and then calls the corresponding application. At the same time, it is a programming language. As a command language, it interactively interprets and executes commands entered by the user or automatically interprets and executes a series of preset commands; as a programming language, it defines various variables and parameters, and provides many high-level Control structures unique to the language, including loops and branches.

After a long period of development, many users are familiar with PHP. Here I would like to share my personal understanding and discuss it with you. Mostly I use exec() commands and data arrays for everything. Or use shell_exec() for simpler commands, especially if you don't care about the results. If I just need to return a PHP shell script, I use passthru(). Often I use different functions on different occasions, and sometimes they are interchangeable.

It all depends on my mood and what I want to achieve. Another question you might ask is "What are their strengths?". If you have no clue, or have a project that would be great for using shell commands but don't know how to use them, I'm here to provide some insight. If you are writing an application that provides various backup or file transfer functionality, you may choose to run an rsync-supported PHP shell script using shell_exec() or one of the other commands provided here. You can write a PHP shell script to include the necessary rsync commands and then use passthru() to execute it based on the user's command or a cron job.

For example, a user with appropriate permissions in your application (such as admin rights) wants to send 50 PDF files from one server to another. The user then needs to navigate to the correct location in the application, click Transfer, select the PDF that needs to be sent, and click Submit. Along the way, the form should have a PHP script that runs the rsync script via passthru() using the return options variable as shown below.

Listing 1. Sample PHP script to run rsync script via passthru()

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="func">passthru</span><span>(</span><span class="string">'xfer_rsync.sh'</span><span>,</span><span class="vars">$returnvalue</span><span>);   </span></li><li class="alt"><span class="keyword">if</span><span> (</span><span class="vars">$returnvalue</span><span> != 0){   </span></li><li><span class="comment">//we have a problem!  </span><span> </span></li><li class="alt"><span class="comment">//add error code here  </span><span> </span></li><li><span>}</span><span class="keyword">else</span><span>{   </span></li><li class="alt"><span class="comment">//we are okay  </span><span> </span></li><li><span class="comment">//redirect to some other page  </span><span> </span></li><li class="alt"><span>}   </span></li><li><span>?> </span></span></li></ol>
Copy after login

If your application needs to list processes or files, or about these Data from a process or file, you can easily achieve this 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 into 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 - whenever the user presses a button or clicks a link, PHP runs the corresponding script. You can also use standalone PHP scripts with cron or other schedulers to achieve some interesting effects. For example, if you have a backup script, you can run it via cron, or package it into a PHP script and run it.

Why do you do this? It seems redundant, doesn't it? That's not it - you need to think of it this way, you can run the backup script via exec() or passthru() and then do something based on the return code Behavior. If an error occurs, you can log it to the error log or database, or send a warning email. If the script succeeds, you can dump the raw output to a database (for example, rsync has a verbose mode, which is useful for diagnosing problems later).

Security

Let’s briefly discuss security here: If you accept user input and pass the information to the shell, it’s a good idea to filter the user input. Remove commands you consider harmful and disallowed, such as sudo (run as superuser) or rm (remove). In fact, you probably don't want the user to send an open request, but instead let them choose from a list.

For example, if you run a transfer program that accepts a list of files as an argument, you should list all the files via a series of checkboxes. Users can select and deselect files and activate the rsync shell script by clicking Submit. Users cannot enter files themselves or use regular expressions.

This article introduces the usage of PHP shell script from two aspects, I hope it can be helpful to you.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445742.htmlTechArticleWe all know that in computer science, SHELL is similar to command.com under DOS. It receives user commands and then calls the corresponding application. At the same time, it is a programming language. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template