LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash script read command issue with carriage return (https://www.linuxquestions.org/questions/linux-general-1/bash-script-read-command-issue-with-carriage-return-4175466215/)

itamarm 06-16-2013 08:24 AM

bash script read command issue with carriage return
 
Hello,

I am trying to catch carriage return input but with no success.
works fine when $replay is Y,y,N or n but it doesn't catch carriage return.
I've tried with ^K or ^L 'x0a' '\n' or '\r' and with $ like $'\n' or $'\r' but it doesn't work.

here is the code:
Code:

#! /bin/bash

for count in {30..1};
do   
        echo -en "\rWould you like to install McAfee Agent [$count] [y-Default / n]" && read -t 1 -n 1 replay;
        if [[ $replay =~ ^[YyNn]$ || $replay == '\r' ]];
        then
        echo Replay is $replay
        break
        fi
done

sleep 3

echo Replay is $replay

It doesn't brake when carriage return is been pressed.
If someone has any idea I'll be glad.

Regards,
Itamar

rknichols 06-16-2013 08:54 AM

If you haven't done anything to change the terminal input mode, carriage return will be translated to newline. The read command will strip off the newline, so what you want to test for is your variable being null:
Code:

if [[ $replay =~ ^[YyNn]$ || $replay == "" ]]

itamarm 06-17-2013 12:53 AM

rknichols,
Thanks for the comment but I want to have 3 situations:
1. entering Yes or No.
2. Default - when the counter ends which will return null.
3. Pressing Enter to accept the default and continue.

The problem is with this null value.

Regarding the code you've suggested, if your not entering nothing the loop ends after 1 sec cause it thinks Enter has been pressed.

Any other suggestion how can I achieve this method ?

Regards,
Itamar

rknichols 06-17-2013 07:37 AM

Test the return code from read. For a timeout, read will exit with a non-zero return code.


All times are GMT -5. The time now is 04:24 PM.