Php shell_exec () command usage

Source: Internet
Author: User
Tags call shell php script php tutorial strlen

Shell_exec ()

List 1. Calculate the number of words using the anti-marker
Copy the code as follows:

#! /Bin/sh
Number_of_words = 'WC-w *. txt'
Echo $ number_of_words

# Result wocould be something like:
#165 readme.txt 388 results.txt 588 summary.txt
# And so on ....

In your PHP script, you can run this simple command in shell_exec (), as shown in listing 2, and get the desired result. Suppose there are some text files in the same directory.

Listing 2. Run the same command in shell_exec ()
Copy the code as follows:

<? Php Tutorial
$ Results = shell_exec ('WC-w *. txt ');
Echo $ results;
?>

As shown in Figure 1, the results are the same as those obtained from the shell script. This is because shell_exec () allows you to run external programs through shell and then return results in the form of strings.
Figure 1. Result of running shell command through shell_exec ()

Note that only the suffix marker operator will get the same result, as shown below.
Listing 3. Only use the suffix marker operator
Copy the code as follows:

<? Php
$ Results = 'WC-w *. txt ';
Echo $ results;
?>

Listing 4 provides a simpler method.
Listing 4. Simpler methods
Copy the code as follows:

<? Php
Echo 'WC-w *. txt ';
?>

It is important to know that many things can be done through UNIX command lines and shell scripts. For example, you can use a vertical line to connect commands. You can even use operators to create shell scripts in them and only call shell scripts (use or not use parameters as needed ).

For example, if you only want to calculate the number of words in the first five text files in the directory, you can use a vertical line (|) to connect the wc and head commands. In addition, you can place the output result in the pre tag so that it can be visually displayed in a Web browser, as shown below.

Listing 5. More complex shell commands
Copy the code as follows:

<? Php
$ Results = shell_exec ('WC-w *. txt | head-5 ');
Echo "<code lang =" php ">". $ results. "</code> ";
?>

Figure 2 demonstrates the result of running the script in listing 5.
Figure 2. Result of running more complex shell commands from shell_exec ()

Later in this article, you will learn how to use PHP to pass parameters for these scripts. Now you can think of it as a way to run shell commands, but remember that you can only see standard output. If a command or script has an error, you will not see the standard error (stderr) unless you add it to stdout through a vertical line.

Official website reference

<? Php
Define ("_ USED_CHARS _", "abcdefghijklmnopqrstuvwxyz0123456789 ");
Define ("_ CASE_SENSITIVE _", true); // Use string above or use uppercase/lowercase variant

$ Bf = new chargen (2); // new chargen object, length 2
$ Bf-> generate ("whois"); // generate chars and call whois function

Function whois ($ str)
{
$ Domain = $ str. ". com ";

$ Retval = shell_exec ("whois $ domain ");

If (eregi ("no match", $ retval ))
Echo $ domain. "ist availablen ";
Else
Echo $ domain. "is unavailablen ";
}

Class chargen
{
Private $ chars = NULL;
Private $ maxlength = NULL;

Protected $ buffer = NULL;

Function generate ($ mycallback = false)
        {
Foreach ($ this-> buffer as $ char)
                {
Foreach ($ this-> chars as $ nextchar)
                        {
$ Retval = $ char. $ nextchar;
$ This-> buffer [$ retval] = $ retval;

If ($ mycallback & function_exists ($ mycallback ))
$ Mycallback ($ retval );
Else
Echo $ retval. "n ";
                        }
                }

If (strlen ($ retval) ==$ this-> maxlength)
Return;

$ This-> generate ($ mycallback );
        }

Function _ construct ($ maxlength = 8)
        {
$ Chars = array ();

$ This-> buffer = array ();
Array_push ($ this-> buffer ,"");

For ($ I = 0; $ I <strlen (_ USED_CHARS _); $ I ++)
                {
$ Index = substr (_ USED_CHARS __, $ I, 1 );

If (_ CASE_SENSITIVE __)
                        {
$ This-> chars [$ index] = $ index;
                        }
Else
                        {
$ This-> chars [strtolower ($ index)] = strtolower ($ index );
$ This-> chars [strtoupper ($ index)] = strtoupper ($ index );
                        }
                }

$ This-> maxlength = $ maxlength;
        }
}
?>

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.