2014-02-14 81 views
0

是否有可能超時c shell的用戶輸入?我的代碼到目前爲止是:超時用戶輸入csh shell

#!/bin/csh -f 


    set COUNT = 5 
    printf "INFO: Start ok (0/1)? " 
    set INPUT = 0 

    while ($COUNT > 0 && $INPUT == 0) 
     printf "\b%d" "$COUNT" 
     set INPUT = <$ 
     sleep 1 
     @ COUNT -- 
    end 

    if ($INPUT == 1) then 
    ./execute.sh 
    end 

如果沒有輸入,我想執行一個shell腳本;如果不是,我想跳過這部分。 不幸的是,skript不會跳過輸入部分,而是等待輸入。任何解決這個問題的方法? 非常感謝!

回答

0

嘗試此爲非阻塞的用戶輸入在tcsh外殼:

set TMPFILE = `mktemp` 
set COUNT = 5 
printf "INFO: Start ok (0/1)? " 
stty -F /dev/tty -icanon 
while ($COUNT > 0 && -z $TMPFILE) 
    printf "\b%d" "$COUNT" 
    sleep 1 
    (dd bs=1 count=1 iflag=nonblock > $TMPFILE) >& /dev/null 
    set INPUT = `cat $TMPFILE` 
    @ COUNT-- 
end 
stty -F /dev/tty icanon 
echo "" 
if ("$INPUT" == "1") then 
    ./execute.sh 
endif