Bash shell中读取错误:0:资源暂时不可用 [英] Bash shell read error: 0: Resource temporarily unavailable

查看:1433
本文介绍了Bash shell中读取错误:0:资源暂时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写一个bash脚本。有时候,你正在运行开辟了另一个程序,如故宫,作曲..等,但在你需要使用,以提示用户在同一时间的命令。

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 in order to prompt the user.

不可避免的你打这种类型的错误:

Inevitable you hit this kind of error:


读:读取错误:0:资源暂时不可用

之后做一些研究似乎通过管道那些操纵你的bash脚本的STDIN到/ dev / null的。

After doing some research there seems to be a solution by piping the STDIN of those programs which manipulate the STDIN of your bash script to /dev/null.

是这样的:


NPM安装<的/ dev / null的

其他的研究已经表明,它牵扯到的事实,标准输入被设置为某种阻塞/ noblocking地位和它的程序后,并未重置完成。

Other research has shown it has something to do with the fact that the STDIN is being set to some sort of blocking/noblocking status and it isn't being reset after the program finishes.

问题是有某种形式的傻瓜证明,阅读用户优雅的方式提示输入而不会受到那些操纵STDIN并没有打猎哪些程序需要有自己的标准输入重定向到/ dev / null的程序。你甚至可能需要使用这些程序的STDIN!

The question is there some sort of fool proof, elegant way of reading user prompted input without being affected by those programs that manipulate the STDIN and not having to hunt which programs need to have their STDIN redirected to /dev/null. You may even need to use the STDIN of those programs!

推荐答案

通常重要的是要知道什么样的输入调用程序期望并从那里,所以它不是重定向从/ dev / null的标准输入那些问题不应该得到任何

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.

然而,这是可以做到这对外壳本身和所有调用程序。只需将标准输入到另一个文件描述符和打开/ dev / null的在它的​​位置。像这样的:

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</dev/null

在文件描述符3以上的重复标准输入文件描述符(0),然后打开/ dev / null来取代它。

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

在此的任何调用的命令试图读取标准输入会从/ dev / null时阅读。应该读原标准输入程序应具有由文件描述符3.重定向这样的:

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

&LT; 重定向操作假定目标文件描述符0,如果忽略,因此上述两个命令可以写成这样:

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 </dev/null
read -r var <&3

这篇关于Bash shell中读取错误:0:资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆