国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 Shell Script方式的PHP(轉) 這種方式頗有點像PERL的CGI方式。。:)

Shell Script方式的PHP(轉) 這種方式頗有點像PERL的CGI方式。。:)

Jun 21, 2016 am 09:12 AM
nbsp php quot script

cgi|perl

Shell Script方式的PHP

PHP 怎么這么紅??
最近 PHP(Personal Hypertext Preprocessor) 似乎已經(jīng)成了這一兩年來 Linux/Unix 上最廣為大家所使用的網(wǎng)頁處理語言﹐它的方便、強大功能與 OpenSource 的特性使得它正逐漸侵蝕到傳統(tǒng) CGI 甚至是 MicroSoft ASP(Active Server Page)的市場﹐幾乎各大網(wǎng)站征招人才莫不以會 PHP 作為基本條件。??
PHP 確實有這個資格可以這么紅﹐原因有下面數(shù)點 :??
PHP 是 OpenSource 軟件﹐完全免費﹐可以自由散布﹐因此吸引了極多的人來使用﹐也因為如此﹐吸引到了商業(yè)公司為其發(fā)展更好的引擎與最佳化軟件(請參考 http://www.zend.com/)。??
PHP 本身非常簡單易懂﹐淺顯的指令語法﹐外加一些基本的對象導向處理能力﹐讓新手足以在最短時間內學會。??
PHP 提供了相當多的功能﹐包含了數(shù)學處理、字符串處理、網(wǎng)絡相關功能、各種數(shù)據(jù)庫的支持、影像處理功能、有為數(shù)眾多的發(fā)展者正為 PHP 發(fā)展各式各樣的新功能﹐擴充性極佳。??
PHP 非常容易與 Apache 相結合﹐作為 Apache 的模塊來使用﹐設定安裝上相當簡單﹐也因為 Apache 目前已經(jīng)占據(jù)了 Web Server 全球 60% 的市場﹐PHP 自然而然成為 Apache 最佳搭配。??
不過﹐這次要講的主題不是 PHP 在網(wǎng)頁設計上的應用﹐而是 PHP 在 Shell Script 上的應用﹐一般所知的 Shell Script 大約就是 tcsh、bash、perl 或是 python 這幾類語言﹐我所要談的就是將 PHP 當成 Shell Script 來使用。??
PHP 執(zhí)行檔的安裝??
一般 PHP 作為網(wǎng)頁處理語言都是要編譯成 Apache 的模塊﹐這里當然不么做﹐也因此編譯起來很簡單﹐只要以 root 的身分進行如下動作 :??
解開 php-3.0.xx.tar.gz??
cd php??
configure??
make??
編譯完之后﹐在 php 目錄下有一個可執(zhí)行檔﹐檔名為 php﹐將它 copy 到 /usr/local/bin 下即可。注意﹐如果檔案太大﹐可以使用 strip 指令將 php 的方式將不必要的信息去除﹐這樣檔案就會小得多了。??
第一個程序??
開始撰寫我們的第一個 PHP Shell Script 程序﹐這個例子印出 "Hello world !" :??
#!/usr/local/bin/php -q??
??
echo "Hello, world !";??
?>??
注意到 PHP 原本是應用在網(wǎng)頁應用的﹐因此它內定會送出 HTML 的 HEADER﹐但是在此我們是要將 PHP 用作 Shell Script﹐"-q" 就是表示不要送出 HEADER 的意思﹐你可以試試看不加上 -q 的顯示結果。??
在這個例子中﹐/usr/local/bin/php 是表示要執(zhí)行 /usr/local/bin/ 下的 PHP﹐因為我們剛才將它裝在該處。echo 指令將 "Hello, world !" 印出﹐其中的 "" 字符是換行字符。??
注意到在將這個程序存成檔案后﹐須將其 chmod 成為可執(zhí)行屬性(chmod +x 文件名)﹐然后才能執(zhí)行喔。??
進階使用 I??
有時候我們需要在程序執(zhí)行時﹐送進一些參數(shù)﹐比如說 ls 這個指令﹐后面可以加上 -l 參數(shù)﹐PHP Shell Script 一樣也有支持這樣的用法﹐有兩個特殊的變量 : $argc 記錄著后面送入?yún)?shù)的個數(shù)﹐$argv[] 數(shù)組參數(shù)存著的則是參數(shù)的內容。比如說我現(xiàn)在要設計一個算兩個數(shù)字總和的程序 :??
#!/usr/local/bin/php -q??
??
$sum=0;??
$sum=$sum+$argv[1]+$argv[2];??
echo $sum;??
?>??
假設將此程序命名為 sum.php3﹐則執(zhí)行 sum.php3 1 2 按下 enter 則會印出 3。??
如果要算出不特定個數(shù)的參數(shù)和﹐那么就得要用到 $argc 這個特殊變量了 :??
#!/usr/local/bin/php -q??
??
$sum=0;??
for ($t=1;$t$sum=$sum+$argv[$t];??
echo $sum;??
?>??
假設將此程序命名為 bigsum.php3﹐則執(zhí)行 bigsum.php3 1 2 3 4 5 按下 enter 則會印出 15﹐執(zhí)行 bigsum.php3 1 2 3 4 5 6 按下 enter 則會印出 21。??
有時候我們需要在程序執(zhí)行中輸入資料﹐但是 PHP 原本就是用于網(wǎng)頁設計﹐而網(wǎng)頁上的資料輸入自然都是用 FORM 的方式來輸入﹐所以這將 PHP 作為 Shell Script 時問題就來了﹐好在 PHP 有提供了開文件功能﹐而在 Linux/Uinx 之下﹐輸入(input)這件事原本就可以用開檔的方式來完成﹐我們要開啟的是 /dev/stdin 這個設備檔(stdin 是表示 standard input 的意思)﹐程序如下 :??
#!/usr/local/bin/php -q??
??
$fp=fopen("/dev/stdin","r");??
$inputstr=fgets($fp,100);??
fclose($fp);??

echo " ---------------------- ";??
echo $inputstr;??
?>??
其中的 fgets($fp,100) 是指從 $fp 這個檔案(也就是 "/dev/stdin")中讀取出 100 個 byte 的資料﹐程序執(zhí)行到這行便會停下來等待我們的輸入﹐當我們輸入完按下 enter 之后﹐程序就會將剛才我們輸入的資料給印出來了。??
進階使用 II??
雖然已經(jīng)可以處理輸入﹐但是這樣的功能顯然還是太簡單﹐無法應付更大的應用﹐比如說我需要一個功能是將一串資料流(data stream)中的 HTML 給去除﹐這時便需要完整地處理輸出輸入轉向的能力﹐我們可以先設計程序如下 :??
#!/usr/local/bin/php -q??
??
$fp=fopen("/dev/stdin","r");??

while(!feof($fp)) {??
$c=fgetc($fp);??
$inputstr=$inputstr.$c;??
};??

fclose($fp);??

echo $inputstr;??
?>??
假設將此程序命名為 filt.php3﹐如果你直接執(zhí)行這個程序﹐它會一直等待你輸入﹐直到你按下 Ctrl+D 后才會將你的輸入資料給印出﹐我們可以這么執(zhí)行它 :??
more filt.php3 | filt.php3??
這樣的做法是將 filt.php3 這個程序用 more 給秀出并轉向給 filt.php3 這個程序﹐filt.php3 會不斷接受資料(事實上就是 filt.php3 程序代碼本身)﹐最后將其印出。??
我們可以在其中加上過濾 HTML 的功能 :??
#!/usr/local/bin/php -q??
??
$fp=fopen("/dev/stdin","r");??

while(!feof($fp)) {??
$c=fgetc($fp);??
$inputstr=$inputstr.$c;??
};??

fclose($fp);??

$inputstr=ereg_replace("]*)>","",$inputstr);??

echo $inputstr;??
?>??
假設將此程序命名為 filt2.php3﹐如此一來便完成了過濾功能﹐不信請拿個 HTML 檔來試試看 :??
more xxx.html | filt2.php3??
你便會看到刪除了 HTML TAG 的文件了。??
結論??
PHP 拿來當 Shell Script 事實上相當?shù)睾糜茅o原因是 PHP 本身很好學﹐而且它又支持了各種數(shù)據(jù)庫﹐當你已經(jīng)經(jīng)常拿 PHP 來設計你的網(wǎng)站之后﹐絕對不太喜歡再使用其它的 Shell Script 語言來處理其它必須非網(wǎng)頁的部份﹐這時候拿 PHP 來當做 Shell Script 的好處就會顯現(xiàn)出來了﹐你可以以一貫的方式來發(fā)展整個系統(tǒng)﹐而不必一下要用 PHP一下又用 Perl/Python 或是 C。



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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

How to Install PHP on Windows How to Install PHP on Windows Jul 15, 2025 am 02:46 AM

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

PHP Syntax: The Basics PHP Syntax: The Basics Jul 15, 2025 am 02:46 AM

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

How Do You Handle File Operations (Reading/Writing) in PHP? How Do You Handle File Operations (Reading/Writing) in PHP? Jul 16, 2025 am 03:48 AM

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

See all articles