LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH take argument if user don't type anything (https://www.linuxquestions.org/questions/programming-9/bash-take-argument-if-user-dont-type-anything-4175496320/)

elalexluna83 02-26-2014 02:11 PM

BASH take argument if user don't type anything
 
Hi everyone, i have the following script but as you can see, you need to type "y" or "n" in order to do something, is there any way for the script to send a "y" or a "n" after 10 seconds if the user don't type anything?

Code:

echo " Continue ..."
while true ; do
        read ANSWER
        case $ANSWER in
        Y|y )
#                echo " you said yes"
                date
                ;;
        N|n )
#                echo " you said no"
                exit 255
                ;;
        * )
                exit 1
                ;;
esac
done


suicidaleggroll 02-26-2014 02:20 PM

From "man read"

Quote:

-t timeout
Cause read to time out and return failure if a complete
line of input is not read within timeout seconds. time‐
out may be a decimal number with a fractional portion
following the decimal point. This option is only effec‐
tive if read is reading input from a terminal, pipe, or
other special file; it has no effect when reading from
regular files. If timeout is 0, read returns success if
input is available on the specified file descriptor,
failure otherwise. The exit status is greater than 128
if the timeout is exceeded.
Use $? to check read's exit status and then take the necessary action.

elalexluna83 02-26-2014 02:42 PM

Great, you just gave me an idea. I will try it and post here.

Thanks again.

elalexluna83 02-26-2014 03:40 PM

This is the final script i had in mind.

Code:

#!/bin/bash

clear
COUNTER=0
echo " Continue ... [y|n]"
while true ; do
        read -t 60 ANSWER
        if [ $? -eq 0 ]; then
                case $ANSWER in
                Y|y )
                        xdotool mousemove 1200 10
                        sleep 3
                        xdotool click 3
                        xdotool mousemove 100 100
                        ;;
                N|n )
                        exit 255
                        ;;
                * )
                        exit 1
                        ;;
                esac
        else
                        xdotool mousemove 1200 10
                        sleep 3
                        xdotool click 3
                        xdotool mousemove 100 100
        fi
let COUNTER=COUNTER+1
echo $COUNTER
done


grail 02-26-2014 08:16 PM

Your script currently only prompts the user once to continue, yet if 'Y' is chosen a blank prompt will appear again and it may not be obvious to the user that they need to enter something.

Also I am a big believer in not replicating code if possible, so you could optionally do something like:
Code:

#!/bin/bash

clear
COUNTER=0

while true ; do
        read -n 1 -p " Continue ... [y|n]" -t 60 ANSWER
        ANSWER=${ANSWER:-Y}
       
        case ${ANSWER^} in
        Y )
                xdotool mousemove 1200 10
                sleep 3
                xdotool click 3
                xdotool mousemove 100 100
                ;;
        N )
                exit 255
                ;;
        * )
                exit 1
                ;;
        esac
        (( COUNTER++ ))
        echo $COUNTER
done



All times are GMT -5. The time now is 03:38 AM.