0

I am facing an issue as not able to pause inside a function and wait user confirmation to continue or exit the script, the background is I am writing a verification function which verify against an input file and I want it pause when encounter a certain value from input, ideally it able to temporary suspend the function and provide user a menu including Continue or Exit, only after user select the function will resume the execution or the script terminate.

Create an example to test, some idea get from Press space to continue and How do I prompt for Yes/No/Cancel input in a Linux shell script?, as my test, it not work by just ignore the read command line which suppose to pause function and wait user input.

test.sh

#!/bin/bash
function pause(){
 read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...\n'
}

function wait_user_confirm() {
  echo "When encounter number 2 ask user to confirm continue"
  while IFS= read -r line; do
    if [[ "$line" -eq 2 ]]; then
      pause # Not pause the function till user input and ignored by script
    else
      echo "Skip"
    fi
  done < "input.txt"
}

wait_user_confirm

input.txt

1
2
3

Test result:

When encounter number 2 ask user to confirm continue
skip
skip

I test that read command individually (not inside a function, directly as a command line only), it works properly, the issue not because of read command, its because it not able to pause inside a function of shell script. How can I properly pause the function of shell script and wait for user input by providing a select menu ? Appreciate all the help, thanks

oguz ismail
  • 1
  • 16
  • 47
  • 69
Lampard
  • 394
  • 7
  • 23
  • 2
    Duplicate of [Read input inside a loop](https://stackoverflow.com/questions/6883363/read-input-in-bash-inside-a-while-loop), or [Read stdin for user input when a file is already redirected to stdin](https://stackoverflow.com/questions/8886683/read-stdin-for-user-input-when-a-file-is-already-redirected-to-stdin) – oguz ismail Aug 23 '20 at 19:33
  • Hi, oguz ismail, let me check the link, thank you for providing idea, will confirm weather it works – Lampard Aug 23 '20 at 19:36
  • Hi, oguz ismail, the given 2 links really helpful, resolve my doubt by redirecting the while loop read from regular stdin to other fd, thanks again – Lampard Aug 23 '20 at 20:03

1 Answers1

3

The problem here is that both of your read calls work on the same input filehandle (input.txt, which you redirected), while you intended the pause function to read from the original standard input handle instead.

To solve this, you could for example use a different fd (e.g. 4) for the input.txt file, like so:

# (...)

function wait_user_confirm() {
  while IFS= read -ru4 line; do

    # (...)

  done 4< "input.txt"
}

(note the -u4 passed to read and the redirection to fd 4 at the end)

Chris Smeele
  • 966
  • 4
  • 8
  • Hi, Chris Smeele, thank you for providing the idea, will check and confirm back if this work – Lampard Aug 23 '20 at 19:37
  • Nice, thank you Chris Smeele, the `-u4` redirection explicitly resolve the issue, not only in this small demo example, also merged and test in original function, works properly ! – Lampard Aug 23 '20 at 20:01