0

I want to know if there is any way to make my script stop until the user hits the Enter key, but without using the command read, just by the options of stty.

I was wondering a code for this and got some search; so I found a code that needs "space" to continue and changed the variable hold from empty space to \n, but no success.

That's how it looks like:

function _enter-to-continue() {
    local hold='\n'        # this solution i tried, but without success

    printf "Press 'ENTER' to continue or 'ESC' to cancel... "
    local original_tty_state=$(stty -g)
    trap "stty $original_tty_state; exit 0" 2
    stty intr \033

    stty raw isig noflsh echo icrnl

    until [ -z "${hold#$in}" ]; do
        in=$(dd bs=1 count=1 </dev/tty 2>/dev/null)
    done

    stty "$original_tty_state"
}
  • 11
    Umm... What's the reasoning around not using read? Also, what shell are you writing for, bash? A literal newline in bash could be written $'\n'. – Kusalananda Jun 19 at 10:01
  • 1) I was searching for a method that does not allows the writing of any text, avoiding the 'read -s -p "Press ENTER..." var' workaround, and being validated just with the exclusive usage of a specified key, in this case ENTER. 2) Yes: coding for bash. – Regis Barbosa Jun 20 at 9:49
1

I must admin that I did not read the question thoroughly enough. One of the reasons is that your intention is not related to terminal settings at all, with the exception of icrnl which is the default setting anyway.

[ -z "${hold#$in}" ] is a rather strange way to express [ "$in" = ' ' ] or [ "$hold" = "$in" ]. The problem or your code is that $(cmd) strips off a trailing newline. You are waiting for just a newline... So the wanted character never arrives at the testing code. You need

in=$(dd bs=1 count=1 </dev/tty 2>/dev/null; echo x)
in="${in%x}"

The other problem is local hold='\n'. That doesn't work, this is not C. You need

hold='
'
# or
hold=$'\n'

Waiting for Enter without read

head -n 1 >/dev/null

This could replace the whole until loop But would be limited to newline.

| improve this answer | |
  • Please, give me more details: where I would put that line? – Regis Barbosa Jun 20 at 9:50
  • @RegisBarbosa I changed my answer. – Hauke Laging Jun 20 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.