Bash shell read error: 0: Resource temporarily unavailable

后端 未结 6 2052
感情败类
感情败类 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:39

    Usually it is important to know what input the invoked program expects and from where, so it is not a problem to redirect stdin from /dev/null for those that shouldn't be getting any.

    Still, it is possible to do it for the shell itself and all invoked programs. Simply move stdin to another file descriptor and open /dev/null in its place. Like this:

    exec 3<&0 0

    The above duplicates stdin file descriptor (0) under file descriptor 3 and then opens /dev/null to replace it.

    After this any invoked command attempting to read stdin will be reading from /dev/null. Programs that should read original stdin should have redirection from file descriptor 3. Like this:

    read -r var 0<&3
    

    The < redirection operator assumes destination file descriptor 0, if it is omitted, so the above two commands could be written as such:

    exec 3<&0 

提交回复
热议问题