Bash shell read error: 0: Resource temporarily unavailable

后端 未结 6 2060
感情败类
感情败类 2021-02-07 23:54

When writing a bash script. Sometimes you are running a command which opens up another program such as npm, composer.. etc. But at the same time you need to use read

6条回答
  •  梦毁少年i
    2021-02-08 00:39

    I had a similar issue, but the command I was running did need a real STDIN, /dev/null wasn't good enough. Instead, I was able to do:

    TTY=$(/usr/bin/tty)
    cmd-using-stdin < $TTY
    read -r var
    

    or combined with spbnick's answer:

    TTY=$(/usr/bin/tty)
    exec 3<&0 < $TTY
    cmd-using-stdin
    read -r var 0<&3`
    

    which leaves a clean STDIN in 3 for you to read and 0 becomes a fresh stream from the terminal for the command.

提交回复
热议问题