Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用stat的读取超时?_Bash_Nfs_Stat_Subshell_Process Substitution - Fatal编程技术网

Bash 如何使用stat的读取超时?

Bash 如何使用stat的读取超时?,bash,nfs,stat,subshell,process-substitution,Bash,Nfs,Stat,Subshell,Process Substitution,我有以下代码: #!/bin/bash read -t1 < <(stat -t "/my/mountpoint") if [ $? -eq 1 ]; then echo NFS mount stale. Removing... umount -f -l /my/mountpoint fi #/bin/bash read-t1在子shell内部或在读取行的末尾不起作用。但一定有办法 谢谢你对这方面的任何见解 使用命令替代,而不是进程替代 而不是从过程替代中阅读,而是考虑

我有以下代码:

#!/bin/bash

read -t1 < <(stat -t "/my/mountpoint")
if [ $? -eq 1 ]; then
  echo NFS mount stale. Removing... 
  umount -f -l /my/mountpoint
fi
#/bin/bash
read-t1在子shell内部或在读取行的末尾不起作用。但一定有办法

谢谢你对这方面的任何见解

使用命令替代,而不是进程替代

而不是从过程替代中阅读,而是考虑使用命令替代。例如:

mountpoint=$(stat -t "/my/mountpoint" 2>&1)
mountpoint="/my/mountpoint"
read -t1 < <(stat -t "$mountpoint" 2>&-)
if [[ -n "$REPLY" ]]; then
    echo "NFS mount stale. Removing..."
    umount -f -l "$mountpoint"
fi
这将通过将标准输出存储在变量中使输出静音,但通过取消引用$mountpoint使结果可检索。这种方法还使退出状态可以通过$

更明确的选择 或者,您可以更简单地将其改写为:

mountpoint="/my/mountpoint"
if stat -t "$mountpoint" 2>&-
then
    echo "NFS mount stale. Removing..."
    umount -f -l "$mountpoint"
fi
对我来说,这似乎更能揭示意图,更不容易出错,但您的里程数肯定会有所不同

(Ab)使用读取超时 在评论中,OP询问是否可以滥用读取超时来处理来自stat的挂起输入。如果关闭标准错误并检查空$REPLY字符串,答案是肯定的。例如:

mountpoint=$(stat -t "/my/mountpoint" 2>&1)
mountpoint="/my/mountpoint"
read -t1 < <(stat -t "$mountpoint" 2>&-)
if [[ -n "$REPLY" ]]; then
    echo "NFS mount stale. Removing..."
    umount -f -l "$mountpoint"
fi
mountpoint=“/my/mountpoint”
读取-t1<&-)
如果[-n“$REPLY”];然后
echo“NFS装载过时。正在删除…”
umount-f-l“$mountpoint”
fi
这有几个原因:

  • 在Bash中使用read内置时:

    如果未提供名称,则读取的行将存储在REPLY变量中
  • 在标准错误关闭的情况下,$REPLY将为空,除非stat在标准输出上返回某些内容,如果遇到错误,它不会返回这些内容。换句话说,您检查的是$REPLY字符串的内容,而不是read的退出状态
  • 使用命令替代,而不是进程替代

    而不是从过程替代中阅读,而是考虑使用命令替代。例如:

    mountpoint=$(stat -t "/my/mountpoint" 2>&1)
    
    mountpoint="/my/mountpoint"
    read -t1 < <(stat -t "$mountpoint" 2>&-)
    if [[ -n "$REPLY" ]]; then
        echo "NFS mount stale. Removing..."
        umount -f -l "$mountpoint"
    fi
    
    这将通过将标准输出存储在变量中使输出静音,但通过取消引用$mountpoint使结果可检索。这种方法还使退出状态可以通过$

    更明确的选择 或者,您可以更简单地将其改写为:

    mountpoint="/my/mountpoint"
    if stat -t "$mountpoint" 2>&-
    then
        echo "NFS mount stale. Removing..."
        umount -f -l "$mountpoint"
    fi
    
    对我来说,这似乎更能揭示意图,更不容易出错,但您的里程数肯定会有所不同

    (Ab)使用读取超时 在评论中,OP询问是否可以滥用读取超时来处理来自stat的挂起输入。如果关闭标准错误并检查空$REPLY字符串,答案是肯定的。例如:

    mountpoint=$(stat -t "/my/mountpoint" 2>&1)
    
    mountpoint="/my/mountpoint"
    read -t1 < <(stat -t "$mountpoint" 2>&-)
    if [[ -n "$REPLY" ]]; then
        echo "NFS mount stale. Removing..."
        umount -f -l "$mountpoint"
    fi
    
    mountpoint=“/my/mountpoint”
    读取-t1<&-)
    如果[-n“$REPLY”];然后
    echo“NFS装载过时。正在删除…”
    umount-f-l“$mountpoint”
    fi
    
    这有几个原因:

  • 在Bash中使用read内置时:

    如果未提供名称,则读取的行将存储在REPLY变量中
  • 在标准错误关闭的情况下,$REPLY将为空,除非stat在标准输出上返回某些内容,如果遇到错误,它不会返回这些内容。换句话说,您检查的是$REPLY字符串的内容,而不是read的退出状态

  • 我想我明白了!响应中提到的重定向似乎在子shell中工作,而不会像2>&1那样清除返回代码。因此,这正如预期的那样起作用:

    read -t1 < <(rpcinfo -t 10.0.128.1 nfs 2>&-)
    if [ $? -eq 0 ]; then
      echo "NFS server/service available!"
    else
      echo "NFS server/service unavailable!"
    fi
    
    read-t1<&-)
    如果[$?-等式0];然后
    echo“NFS服务器/服务可用!”
    其他的
    echo“NFS服务器/服务不可用!”
    fi
    
    其中10.0.128.1是“坏”IP(没有服务器/服务响应)。脚本在一秒钟内超时,并生成“NFS服务器/服务不可用!”响应,但没有来自rpcinfo的输出。同样,当IP良好时,输出所需的响应


    我对你的回答投了赞成票

    我想我明白了!响应中提到的重定向似乎在子shell中工作,而不会像2>&1那样清除返回代码。因此,这正如预期的那样起作用:

    read -t1 < <(rpcinfo -t 10.0.128.1 nfs 2>&-)
    if [ $? -eq 0 ]; then
      echo "NFS server/service available!"
    else
      echo "NFS server/service unavailable!"
    fi
    
    read-t1<&-)
    如果[$?-等式0];然后
    echo“NFS服务器/服务可用!”
    其他的
    echo“NFS服务器/服务不可用!”
    fi
    
    其中10.0.128.1是“坏”IP(没有服务器/服务响应)。脚本在一秒钟内超时,并生成“NFS服务器/服务不可用!”响应,但没有来自rpcinfo的输出。同样,当IP良好时,输出所需的响应


    我对你的回答投了赞成票

    谢谢你的回复!有没有办法将此与
    阅读结合起来?它的原因是暂停一个否则会挂起的命令。rpcinfo也会发生这种情况:
    read-t1&-生效,退出状态仍然有效。如果[$?-eq 0],
    行;然后…
    如果[-n“$REPLY”]];然后…
    在使用
    read-t1
    来超时挂起的命令时,以及在子shell中使用2>&-来静音标准错误(如示例中所示)时,似乎同样可以正常工作。感谢您的回复!有没有办法将此与
    阅读结合起来?它的原因是暂停一个否则会挂起的命令。rpcinfo也会发生这种情况:
    read-t1&-生效,退出状态仍然有效。如果[$?-eq 0],
    行;然后…
    如果[-n“$REPLY”]];然后…
    在使用
    read-t1
    超时挂起命令时,以及在子shell中使用2>&-使标准错误静音时,似乎同样有效(如示例所示)。