我如何使用读取统计超时? [英] How can I use read timeouts with stat?

查看:208
本文介绍了我如何使用读取统计超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

 #!/斌/庆典阅读-t1< ≤(STAT -t/我的/挂载点)
如果[$? -eq 1];然后
  回声NFS挂载陈旧。移除...
  -f的umount -l /我的/挂载点
科幻

我如何静音统计的输出,而在同一时间是仍然能够检测到在随后的测试其错误程度?

添加>的/ dev / null的2 - ;&放大器; 1 子shell里面,或者在读行的末尾不起作用。但是,必须有一个办法...

感谢有这方面的见解!


解决方案

使用命令Subsitution,不进程替换

而不是从过程subsitution读,可以考虑使用命令替换来代替。例如:

 挂载点= $(STAT -t/我的/挂载点2  - ;&安培; 1)

这将被存放在变量标准输出静音输出,但提领的 $挂载点的离开,结果检索。这种方法也使访问的退出状态通过的 $?

更清晰的替代

另外,你可能只是重写这个更简单的:

 挂载点=/我的/挂载点
如果统计-t$挂载点2 - ;&安培; -
然后
    回声NFS挂载陈旧。卸下......
    -f的umount -l$挂载点
科幻

对我来说,这似乎更多的意向揭示和不易出错,但你一定里程可能会有所不同。

使用读超时

(AB)

在评论中,OP问读超时是否可能会被滥用,从统计的处理挂起的输入。答案是肯定的,如果您关闭标准错误,并检查是否有一个空的 $ REPLY 的字符串。例如:

 挂载点=/我的/挂载点
阅读-t1< ≤(统计-t$挂载点2 - ;&放大器;-)
如果[-n$ REPLY]];然后
    回声NFS挂载陈旧。卸下......
    -f的umount -l$挂载点
科幻

这工作有以下几个原因:


  1. 当使用的阅读的内置在击:


      

    如果没有名称被提供,读出的行被存储在答复变量


  2.   
  3. 在标准错误关闭,的 $ REPLY 的将是空的,除非统计的返回到标准输出,东西也不会,如果它遇到一​​个错误。换句话说,你检查的内容 $答复的字符串,而不是从出口状态的阅读

  4.   


I have the following code:

#!/bin/bash

read -t1 < <(stat -t "/my/mountpoint")
if [ $? -eq 1 ]; then
  echo NFS mount stale. Removing... 
  umount -f -l /my/mountpoint
fi

How do I mute the output of stat while at the same time being still able to detect its error level in the subsequent test?

Adding >/dev/null 2>&1 inside the subshell, or in the end of the read line does not work. But there must be a way...

Thanks for any insights on this!

解决方案

Use Command-Subsitution, Not Process Substitution

Instead of reading in from process subsitution, consider using command substitution instead. For example:

mountpoint=$(stat -t "/my/mountpoint" 2>&1)

This will silence the output by storing standard output in a variable, but leave the results retrievable by dereferencing $mountpoint. This approach also leaves the exit status accessible through $?.

A Clearer Alternative

Alternatively, you might just rewrite this more simply as:

mountpoint="/my/mountpoint"
if stat -t "$mountpoint" 2>&-
then
    echo "NFS mount stale. Removing..."
    umount -f -l "$mountpoint"
fi

To me, this seems more intention-revealing and less error-prone, but your mileage may certainly vary.

(Ab)using Read Timeouts

In the comments, the OP asked whether read timeouts could be abused to handle hung input from stat. The answer is yes, if you close standard error and check for an empty $REPLY string. For example:

mountpoint="/my/mountpoint"
read -t1 < <(stat -t "$mountpoint" 2>&-)
if [[ -n "$REPLY" ]]; then
    echo "NFS mount stale. Removing..."
    umount -f -l "$mountpoint"
fi

This works for several reasons:

  1. When using the read builtin in Bash:

    If no NAMEs are supplied, the line read is stored in the REPLY variable.

  2. With standard error closed, $REPLY will be empty unless stat returns something on standard output, which it won't if it encounters an error. In other words, you're checking the contents of the $REPLY string instead of the exit status from read.

这篇关于我如何使用读取统计超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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