Bash read with -p option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash read with -p option
# 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.





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  
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
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  
Old 08-30-2018
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  
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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

2. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

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. (4 Replies)
Discussion started by: sinnlosername
4 Replies

3. Shell Programming and Scripting

Read: line 6: illegal option -e

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... (4 Replies)
Discussion started by: orangeSunshine
4 Replies

4. Shell Programming and Scripting

sh file: READ (menu) but now run with option

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... (1 Reply)
Discussion started by: holyearth
1 Replies

5. Shell Programming and Scripting

Bash Script for parse input like option and value

I would create a bash script than parse like this: test.sh -p (protocol) -i (address) -d (directory) I need retrive the value after -p for example... understand??? I hope... thanks (6 Replies)
Discussion started by: ionral
6 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

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... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. Shell Programming and Scripting

Request for file read option in Unix shell scripting

Hi Friends, I would like to read all the record from one txt file to other file txt For example I have two txt file a.txt and b.txt. I need to read a.txt record by record and I need add the system date @ end of each record before moving it to b.txt. Could you please share the coding for... (4 Replies)
Discussion started by: vinoth124
4 Replies

8. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

9. Shell Programming and Scripting

Find in Bash with -a option

Hi, The proble is below: Assume i have files starting from "process" then date/time then ".log". ex . process.20100504092942.log process.20100503152213.log process.20100430144217.log process.20100429153644.log process.20100428121200.log process.20100427130746.log... (2 Replies)
Discussion started by: meetvipin
2 Replies

10. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

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? (9 Replies)
Discussion started by: wiseguy
9 Replies
Login or Register to Ask a Question