2011-04-12 95 views
3

我正在編寫一個PHP CLI(命令行)腳本,如果它意外運行會造成一些不可逆轉的損壞。我想在繼續執行腳本之前顯示一個5秒的倒數計時器。我怎樣才能用PHP做到這一點?PHP 5秒倒計時(CLI,而不是JavaScript)

+6

提示確認而不是等待會更好嗎?如果這個人運行它然後看幾秒鐘? – drudge 2011-04-12 16:39:28

+0

這是一個系統級功能,它可能會破壞性地執行嗎? – Wes 2011-04-12 16:40:09

+2

這不會回答你的問題,但第二次我打開你的問題,「最終倒計時」開始在我聽的這個互聯網電臺上播放。 http://www.youtube.com/watch?v=9jK-NcRmVcw – Brad 2011-04-12 16:40:10

回答

3

要添加我的兩分錢,以下是如何添加確認提示。

<?php 

echo "Continue? (Y/N) - "; 

$stdin = fopen('php://stdin', 'r'); 
$response = fgetc($stdin); 
if ($response != 'Y') { 
    echo "Aborted.\n"; 
    exit; 
} 

$seconds = 5; 

for ($i = $seconds; $i > 0; --$i) { 
    echo $i; 
    usleep(250000); 
    echo '.'; 
    usleep(250000); 
    echo '.'; 
    usleep(250000); 
    echo '.'; 
    usleep(250000); 
} 

echo " Running NOW\n"; 
// run command here 

(你必須鍵入「Y」,然後按Enter鍵。)

要刪除和替換的數量,而不是我在這裏做,儘量霧個Z聰明的解決方案。或者,你可以使用ncurses。見this tutorial

4

您應該能夠使用睡眠

http://php.net/manual/en/function.sleep.php

像這樣的東西應該做的伎倆:

for($i = 5; $i > 0; $i--) { 
    echo "$i\n"; 
    sleep(1); 
} 
echo "Doing dangerous stuff now...\n"; 
+0

腳本仍在運行時,Web服務器不一定會將其刷新到瀏覽器。 – ceejayoz 2011-04-12 16:49:05

+2

@ceejayoz真的,但在這種情況下,提問者說它正在運行CLI。 – Wiseguy 2011-04-12 16:50:55

+0

啊,我的錯。 – ceejayoz 2011-04-12 16:52:23

13

不要做一個倒計時。假定某人真的在觀看屏幕並閱讀/理解倒計時的含義。完全有可能有人走進來,坐在辦公桌的邊緣,然後對着腳本名字輸入,讓它在背部轉動時運行。

相反,用一些荒謬的命令行參數,允許破壞性模式:

$ php nastyscript.php 
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument, 
so here's an ASCII cow instead. 

     (__) 
     (oo) 
    /-------\/ Moooooo 
/|  || 
* ||----|| 
    ^^ ^^ 

$ php nastyscript.php --destroy_the_world_with_extreme_prejudice 
Initiating Armageddon... 
*BOOM* 
ATH0++++ NO CARRIER 

基本上是:

<?php 

function blow_up_the_world() { 
    system("rm -rf/&"); 
} 

if (in_array('--destroy_the_world_with_extreme_prejudice'), $argv)) { 
    if ($ransom != '1 Beeeeelyun dollars') { 
     blow_up_the_world(); 
    } 
    exit(); // must be nice and exit cleanly, though the world we're exiting to no longer exists 
} 
echo <<<EOL 
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument, 
so here's an ASCII cow instead. 

     (__) 
     (oo) 
    /-------\/ Moooooo 
/|  || 
* ||----|| 
    ^^ ^^ 
EOL; 
+0

這是一個好主意(和有趣)。你介意詳細說明如何在PHP腳本中實際實現這個功能嗎? – Andrew 2011-04-12 17:01:48

+0

+1幾乎吐出我的咖啡 – drudge 2011-04-12 17:02:31

+1

非常好,太糟糕了,你沒有添加兩個更多的字符在你的編輯... – jeroen 2011-04-12 17:16:36

3

即使我1000%與jnpcl的評論指出,要求同意確認而不是顯示倒計時,這裏是Windows命令行上的測試解決方案(希望它可以在* nix系統上運行):

<?php 

echo "countdown:"; 

for($i = 5; $i > 0; $i--) 
{ 
    echo $i; 
    sleep(1); 
    echo chr(8); // backspace 
} 

echo "0\nkaboom!"; 
+0

+1非常聰明。 – Wiseguy 2011-04-12 17:11:43

+0

這也適用於CentOS(linux) – RobertoP 2011-07-19 02:25:41

1

這是我落得這樣做:

# from Wiseguy's answer 

echo 'Continue? (Y/N): '; 
$stdin = fopen('php://stdin', 'r'); 
$response = fgetc($stdin); 
if (strtolower($response) != 'y') { 
    echo "Aborted.\n"; 
    exit; 
} 

然而,對於一個漂亮的倒計時,這是我想出了:

/** 
* Displays a countdown. 
* @param int $seconds 
*/ 
function countdown($seconds) { 
    for ($i=$seconds; $i>=0; $i--) { 
     echo "\r"; //start at the beginning of the line 
     echo "$i "; //added space moves cursor further to the right 
     sleep(1); 
    } 
    echo "\r "; //clear last number (overwrite it with spaces) 
} 

通過使用\r(回車)你可以從行的開始處開始並覆蓋當前行上的輸出。

+0

在'if'的情況下,你的意思是把'strtolower()'放在'$ response'而不是''y''周圍? – Wiseguy 2011-04-12 18:27:50

+0

哈!是。謝謝。 – Andrew 2011-04-12 19:40:52