linux脚本提示yes和no,如何提示Linux shell脚本中的Yes/No/Cancel输入?

a8a98da3b84f49cc00826243d30f43ac.png

沧海一幻觉

一个普通问题至少有五个答案。取决于POSIX兼容:可以针对通用的不良系统进行工作。壳环境巴什具体:使用所谓的巴什如果你想简单的`in line‘问题/回答(一般解决方案)格式良好的接口,如诅咒或者使用libgtk或libqt进行图形化。使用强大的读行历史记录功能1.POSIX通用解决方案你可以用read命令,然后是if ... then ... else:echo -n "Is this a good question (y/n)? "read answer# if echo "$answer" | grep -iq "^y" ;thenif [ "$answer" != "${answer#[Yy]}" ] ;then

    echo Yeselse

    echo Nofi(多亏了亚当·卡茨的评论:将上面的测试替换为一个更可移植的测试,并且避免使用一个分叉:)POSIX,但唯一的关键特性但如果你不想让用户回归,你可以写:(编辑:正如@JonathanLeffler正确的建议,储蓄stty的配置可能比强迫他们神智.)echo -n "Is this a good question (y/n)? "old_stty_cfg=$(stty -g)stty raw -echo ; answer=$(head -c 1) ;

 stty $old_stty_cfg # Careful playing with sttyif echo "$answer" | grep -iq "^y" ;then

    echo Yeselse

    echo Nofi注:这是在什, 巴什, KSH, 破折号和箱箱!相同,但显式地等待y或n:#/bin/shecho -n "Is this a good question (y/n)? "old_stty_cfg=$(stty -g)stty raw -echo

answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )stty $old_stty_cfgif echo "$answer" | grep -iq "^y" ;then

    echo Yeselse

    echo Nofi使用专用工具有许多工具是用libncurses, libgtk, libqt或者其他图形库。例如,使用whiptail:if whiptail --yesno "Is this a good question" 20 60 ;then

    echo Yeselse

    echo Nofi根据您的系统,您可能需要替换whiptail使用另一个类似的工具:dialog --yesno "Is this a good question" 20 60 && echo Yesgdialog --yesno "Is this a good question" 20 60 && echo Yeskdialog

 --yesno "Is this a good question" 20 60 && echo Yes哪里20对话框的高度(行数)和60对话框的宽度。这些工具都有近同语法。DIALOG=whiptailif [ -x /usr/bin/gdialog ] ;then DIALOG=gdialog ; fiif [ -x /usr/bin/xdialog ] ;then DIALOG=xdialog ; fi...$DIALOG --yesno ...2.专门针对巴什的解决办法基本排成一行方法read -p "Is this a good question (y/n)? " answercase ${answer:0:1} in

    y|Y )

        echo Yes

    ;;

    * )

        echo No

    ;;esac我更喜欢用case所以我甚至可以测试yes | ja | si | oui如果需要的话.。排成一行带着单键特征在bash下,我们可以为read指挥:read -n 1 -p "Is this a good question (y/n)? " answer在巴什下,read命令接受超时参数,这可能很有用。read -t 3 -n 1 -p "Is this a good question (y/n)? " answer[ -z "$answer" ] && answer="Yes"  # if 'yes' have to be default choice一些技巧专用工具更复杂的对话框,不仅仅是简单的yes - no目的:dialog --menu "Is this a good question" 20 60 12 y Yes n No m Maybe进度栏:dialog --gauge "Filling the tank" 20 60 0 

    for i in {1..100};do

        printf "XXX\n%d\n%(%a %b %T)T progress: %d\nXXX\n" $i -1 $i

        sleep .033

    done)小演示:#!/bin/shwhile true ;do

    [ -x "$(which ${DIALOG%% *})" ] || DIALOG=dialog

    DIALOG=$($DIALOG --menu "Which tool for next run?" 20 60 12 2>&1 \

            whiptail       "dialog boxes from shell scripts" >/dev/tty \

            dialog         "dialog boxes from shell with ncurses" \

            gdialog        "dialog boxes from shell with Gtk" \

            kdialog        "dialog boxes from shell with Kde" ) || exit

    clear;echo "Choosed: $DIALOG."

    for i in `seq 1 100`;do

        date +"`printf "XXX\n%d\n%%a %%b %%T progress: %d\nXXX\n" $i $i`"

        sleep .0125

      done | $DIALOG --gauge "Filling the tank" 20 60 0

    $DIALOG --infobox "This is a simple info box\n\nNo action required" 20 60

    sleep 3

    if $DIALOG --yesno  "Do you like this demo?" 20 60 ;then

        AnsYesNo=Yes; else AnsYesNo=No; fi

    AnsInput=$($DIALOG --inputbox "A text:" 20 60 "Text here..." 2>&1 >/dev/tty)

    AnsPass=$($DIALOG --passwordbox "A secret:" 20 60 "First..." 2>&1 >/dev/tty)

    $DIALOG --textbox /etc/motd 20 60

    AnsCkLst=$($DIALOG --checklist "Check some..." 20 60 12 \        Correct "This demo is useful"        off \       

     Fun        "This demo is nice"        off \        Strong        "This demo is complex"       

      on 2>&1 >/dev/tty)

    AnsRadio=$($DIALOG --radiolist "I will:" 20 60 12 \        " -1" "Downgrade this answer"        off \       

     "  0" "Not do anything"                on \        " +1" "Upgrade this anser"        off 2>&1 >/dev/tty)

    out="Your answers:\nLike: $AnsYesNo\nInput: $AnsInput\nSecret: $AnsPass"

    $DIALOG --msgbox "$out\nAttribs: $AnsCkLst\nNote: $AnsRadio" 20 60

  done更多样本?看一看使用鞭尾选择USB设备和USB可移动存储选择器:USBKeyChooser5.使用readline的历史记录例子:#!/bin/bashset -i

HISTFILE=~/.myscript.history

history -c

history -r

myread() {

    read -e -p '> ' $1

    history -s ${!1}}trap 'history -a;exit' 0 1 2 3 6while myread line;do

    case ${line%% *} in

        exit )  break ;;

        *    )  echo "Doing something with '$line'" ;;

      esac

  done这将创建一个文件.myscript.history在你的$HOME目录,无法使用readline的历史记录命令,如向上, 向下, 克特+r还有其他人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值