2014-01-19 33 views
2

使用bash腳本的這一部分作爲一個例子如何在不必按ENTER的情況下輸入'y'?

{ 
    read -p "Do you want to update the tv feed? [y/n/q] " ynq 
    case $ynq in 
    [Yy]*) rm ~/cron/beeb.txt; /usr/bin/get-iplayer --type tv>>~/cron/beeb.txt;; 
    [Nn]*) echo;; 
    [Qq]*) exit;; 
    *) echo "Please answer yes or no. ";; 
    esac 
} 

我如何得到它,這樣就可以按Ÿ,而不必按輸入它被接受嗎?

+0

您可以使用'-n'指定要讀取的字符數。搜索btw時的第一個結果。 – keyser

回答

4

添加-n 1read命令的選項。從bash的手冊頁:

-n nchars 
    read returns after reading nchars characters rather than 
    waiting for a complete line of input. 

順便說一句,你也應該用雙引號"$ynq" - 有時用戶只是按回車鍵,這可能會導致怪異的行爲,如果變量不是雙引號。另外請注意,read -n是bash擴展名,因此請確保您使用的是bash(即#!/bin/bash或類似的腳本的第一行),而不是brand-x shell(#!/bin/sh或類似的)。

+0

是的,它是#!/ bin/bash,但那只是腳本的一部分,但非常感謝,現在它的工作:) – boudiccas

2

使用-n1read指定輸入長度的最大數量爲1:

read -n1 -p "Do you want to update the tv feed? [y/n/q] " ynq 
相關問題