Read a variable in bash with a default value

后端 未结 9 668
不思量自难忘°
不思量自难忘° 2020-11-29 16:13

I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.

# Please enter your          


        
9条回答
  •  伪装坚强ぢ
    2020-11-29 17:12

    The -e and -t parameter does not work together. i tried some expressions and the result was the following code snippet :

    QMESSAGE="SHOULD I DO YES OR NO"
    YMESSAGE="I DO"
    NMESSAGE="I DO NOT"
    FMESSAGE="PLEASE ENTER Y or N"
    COUNTDOWN=2
    DEFAULTVALUE=n
    #----------------------------------------------------------------#
    function REQUEST ()
    {
    read -n1 -t$COUNTDOWN -p "$QMESSAGE ? Y/N " INPUT
        INPUT=${INPUT:-$DEFAULTVALUE}
        if  [ "$INPUT" = "y" -o "$INPUT" = "Y" ] ;then
            echo -e "\n$YMESSAGE\n"
            #COMMANDEXECUTION
        elif    [ "$INPUT" = "n" -o "$INPUT" = "N" ] ;then
            echo -e "\n$NMESSAGE\n"
            #COMMANDEXECUTION
        else
            echo -e "\n$FMESSAGE\n"
        REQUEST
        fi
    }
    REQUEST
    

提交回复
热议问题