加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何输入’y’而不必按ENTER键?

发布时间:2020-12-15 21:54:39 所属栏目:安全 来源:网络整理
导读:以bash脚本的这一部分为例 { 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.
以bash脚本的这一部分为例

{   
    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
}

如何获得它以便您可以按y而不必按Enter键以便接受它?

解决方法

将-n 1添加到read命令的选项中.从bash手册页:

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

顺便说一下,你还应该双引号“$ynq” – 有时用户只会按回车,如果变量不是双引号,可能会导致奇怪的行为.另请注意,读取-n是bash扩展名,因此请确保使用bash(即#!/ bin / bash或类似脚本的第一行),而不是brand-x shell(#!/ bin) / sh或类似的).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读