Bash shell read error: 0: Resource temporarily unavailable

后端 未结 6 2057
感情败类
感情败类 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条回答
  •  迷失自我
    2021-02-08 00:34

    The answers here which suggest using redirection are good. Fortunately, Bash's read should soon no longer need such fixes. The author of Readline, Chet Ramey, has already written a patch: http://gnu-bash.2382.n7.nabble.com/read-may-fail-due-to-nonblocking-stdin-td18519.html

    However, this problem is more general than just the read command in Bash. Many programs presume stdin is blocking (e.g., mimeopen) and some programs leave stdin non-blocking after they exit (e.g., cec-client). Bash has no builtin way to turn off non-blocking input, so, in those situations, you can use Python from the command line:

    $ python3 -c $'import os\nos.set_blocking(0, True)'
    

    You can also have Python print the previous state so that it may be changed only temporarily:

    $ o=$(python3 -c $'import os\nprint(os.get_blocking(0))\nos.set_blocking(0, True)')
    $ somecommandthatreadsstdin
    $ python3 -c $'import os\nos.set_blocking(0, '$o')'
    

提交回复
热议问题