PHP tutorial on the use of shell script in PHP

  • 2020-05-12 02:24:00
  • OfStack

As a command language, it interactively interprets and executes user input commands or automatically interprets and executes a preset sequence of commands. As a programming language, it defines variables and parameters, and provides many of the control structures, including loops and branches, that are available in higher-order languages.
After the development of PHP for a long time, many users are familiar with PHP. Here I would like to make a personal understanding and discuss with you. For the most part, I use the exec() command and data array for everything. Or use shell_exec() for simpler commands, especially if you don't care about the results. If I just need to return an 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 might ask is "what are their strengths?" . If you don't have a clue, or if you have a project that's perfect for using the shell command, but don't know how to use it, here's some insight. If you are writing an application that provides various backup or file transfer capabilities, you can choose to run PHP shell scripts supported by rsync using shell_exec() or one of the other commands provided here. You can write the PHP shell script to include the necessary rsync commands, and then execute it using passthru() based on the user's command or cron job.

For example, a user who has the appropriate permissions in your application (such as administrator permissions) 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 PDF to send, and then click Submit. During this process, the form should have an PHP script that runs the rsync script through passthru() using the return option variable, so you know if there is a problem, as shown below.

Listing 1. An example PHP script running the rsync script through passthru()
 
<?php 
passthru('xfer_rsync.sh',$returnvalue); 
if ($returnvalue != 0){ 
//we have a problem! 
//add error code here 
}else{ 
//we are okay 
//redirect to some other page 
} 
?> 


If your application needs to list processes or files, or data about those processes or files, you can easily do so using command 1 summarized in this article. For example, a simple grep command can help you find files that match a particular search condition. Use it with exec() command 1 to save 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 talked about user-generated events -- when a user presses a button or clicks a link, PHP runs the script. You can also use standalone PHP scripts and cron or other scheduler 1 to achieve some interesting results. For example, if you have a backup script, you can either run it via cron or package it into an PHP script and run it later.

Why do you do this? It seems superfluous, doesn't it? No -- you need to consider that you can run the backup script through exec() or passthru(), and then do something based on the return code. If an error occurs, you can log it to an 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 an exhaustive (verbose) schema that is useful for subsequent problem diagnosis of 10 points).

security

We briefly discuss security here: if you accept user input and pass it to shell, it is best to filter user input. Remove commands and content that you consider harmful, such as sudo(running as a superuser) or rm(deleted). In fact, you might not want users to send open requests, but instead let them select from a list.

For example, if you are running a transport that accepts a list of files as a parameter, you should list all the files through the series 1 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.

This article introduces the usage of PHP shell script from two aspects, hoping to help you.

Related articles: