LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-16-2013, 08:24 AM   #1
itamarm
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Rep: Reputation: 0
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

Last edited by itamarm; 06-16-2013 at 08:46 AM.
 
Old 06-16-2013, 08:54 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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 == "" ]]
 
Old 06-17-2013, 12:53 AM   #3
itamarm
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 06-17-2013, 07:37 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

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


Reply

Tags
bash scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java bufferedreader -- how to read carriage return/new line sent over network from C texasone Programming 2 01-20-2012 08:15 AM
Pass carriage return to a command through script say_hi_ravi Programming 2 02-18-2011 07:07 AM
read carriage return code elainelaw Linux - Newbie 4 03-10-2010 12:04 AM
vsftpd carriage return issue taylorjp75 Linux - Software 1 07-14-2009 06:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration