Today's Posts

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Bash read with -p option

shell scripts

Login to Reply

Thread Tools Search this Thread
# 1  
Old 08-29-2018
Bash read with -p option

I have this simple code to stop script execution.

I did not invent it, and it works.
It used to have fackEnterKey as option, but it works without it.

My curiosity question is -

the "function " terminates when "enter" key is pressed and only "enter" key.

I did read the man and found no explanation why it works only with "enter" key.
I was looking for some kind of "default" setting and found none.

I also found the "description " of "-p" option hard to read - "coming from terminal" implies ( to me) no prompt display UNTIL something is physically (coming) entered on terminal. That is not the case, and makes perfect sense.

  52 #"OUTPUT" redirected  
  53 read -p "Press [Enter] key to continue..." </dev/tty
  55 #fackEnterKey
  56 }

-p prompt
             Display prompt on standard error, 
             without a trailing newline, before attempting to read
             any input. The prompt is displayed only if input is 
             coming from a terminal.

# 2  
Old 08-29-2018
It reads input from stderr - when you create a process you get three separate "connections" called stdin (0), stdout (1), stderr(2). They can be accessed by those numbers. They behave mostly like files, you can read and write any one of them.

Normally read gets input from the keyboard (or a pipe) via stdin. read -p [prompt] blocks (that means wait for input) on stderr. This means the terminal is waiting for the return keypress to come in stderr. No place else.
It won't read anything on stdin. And it expects a keypress to mean 'I am done so stop reading'
# 3  
Old 08-29-2018
OK, but why it accepts only "enter" key to terminate?
# 4  
Old 08-29-2018
Originally Posted by annacreek
OK, but why it accepts only "enter" key to terminate?
what else would identify a line?
from man bash:
       read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
              One  line  is  read  from  the standard input, or from the file descriptor fd supplied as an argument to the -u option, split into words as
              described above under Word Splitting, and the first word is assigned to the first name, the second word to the second name, and so on.

# 5  
Old 08-30-2018
Press any key:
read -rs -n1 -p "Press any key" < /dev/tty

Press q to quit from processing loop - non blocking
echo "Press q to quit"
while true
    read -rs -n1 -t 0.1 < /dev/tty
    [ "$REPLY" = "q" ] && break
    # ... your stuff here

# 6  
Old 08-31-2018
Adding these options -rs -n1 "falls" thru the "Pause" function
without waiting for any key.
Which will be handy to "remove" all debugging "pause" later. Thanks
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Menu Driven Bash Shell Script with Default Option kiran_j Shell Programming and Scripting 3 06-18-2016 08:29 AM
[Bash] Read History function & Read Arrowkeys sinnlosername Shell Programming and Scripting 4 12-09-2014 02:34 AM
Bash while read line chrisjorg Shell Programming and Scripting 3 07-25-2014 08:54 PM
Read: line 6: illegal option -e orangeSunshine Shell Programming and Scripting 4 11-15-2012 07:31 PM
read command with bash millsy5 UNIX for Dummies Questions & Answers 8 10-19-2012 08:42 AM
sh file: READ (menu) but now run with option holyearth Shell Programming and Scripting 1 10-01-2012 10:51 PM
Read Bash Script BrianBlaze UNIX for Dummies Questions & Answers 2 08-10-2012 12:35 PM
Bash Script for parse input like option and value ionral Shell Programming and Scripting 6 06-05-2012 07:54 AM
Help with Bash piped while-read and a read user input at the same time MyMorris Shell Programming and Scripting 8 02-26-2012 02:50 PM
Request for file read option in Unix shell scripting vinoth124 Shell Programming and Scripting 4 06-01-2011 03:44 PM
recently introduced to the newer option for find...does an older option exist? mr_manny Shell Programming and Scripting 2 07-20-2010 01:56 PM
Find in Bash with -a option meetvipin Shell Programming and Scripting 2 05-14-2010 01:59 AM
bash: read file line by line (lines have '\0') - not full line has read??? alex_5161 Shell Programming and Scripting 6 04-14-2010 05:58 AM
read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell wiseguy Shell Programming and Scripting 9 01-28-2009 01:20 PM
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 01:46 PM

All times are GMT -4. The time now is 07:49 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
Show Password