Bash read with -p option


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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.





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




Code:
-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  
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'
# 4  
Quote:
Originally Posted by annacreek
OK, but why it accepts only "enter" key to terminate?
what else would identify a line?
from man bash:
Code:
       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  
Press any key:
Code:
read -rs -n1 -p "Press any key" < /dev/tty

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

# 6  
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 or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
[Bash] Read History function & Read Arrowkeys
sinnlosername
Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance.... Shell Programming and Scripting
4
Shell Programming and Scripting
Read: line 6: illegal option -e
orangeSunshine
For some reason read -e isn't working in my script. I need a directory as input from a user and I'd like for them to be able to use tab complete which is why I'm using -e. When the script is run, I get: read: line 6: illegal option -e In order to just figure out what is going on with the -e...... Shell Programming and Scripting
4
Shell Programming and Scripting
sh file: READ (menu) but now run with option
holyearth
I have a script which uses READ to detect choice of menu option...now I want to change the script without doing whole rewrite such that when user runs ./script.sh 5 it would execute menu option 5 rather than user running ./script.sh waiting for it to load and then pressing "5 enter" Is it...... Shell Programming and Scripting
1
Shell Programming and Scripting
Help with Bash piped while-read and a read user input at the same time
MyMorris
Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read...... Shell Programming and Scripting
8
Shell Programming and Scripting
read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell
wiseguy
Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command?... Shell Programming and Scripting
9
Shell Programming and Scripting