bash - How can I use read timeouts with stat? -


i have following code:

#!/bin/bash  read -t1 < <(stat -t "/my/mountpoint") if [ $? -eq 1 ];   echo nfs mount stale. removing...    umount -f -l /my/mountpoint fi 

how mute output of stat while @ same time being still able detect error level in subsequent test?

adding >/dev/null 2>&1 inside subshell, or in end of read line not work. there must way...

thanks insights on this!

use command-subsitution, not process substitution

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

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

this silence output storing standard output in variable, leave results retrievable dereferencing $mountpoint. approach leaves exit status accessible through $?.

a clearer alternative

alternatively, might rewrite more as:

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

to me, seems more intention-revealing , less error-prone, mileage may vary.

(ab)using read timeouts

in comments, op asked whether read timeouts abused handle hung input stat. answer yes, if close standard error , check empty $reply string. example:

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

this works several reasons:

  1. when using read builtin in bash:

    if no names supplied, line read stored in reply variable.

  2. with standard error closed, $reply empty unless stat returns on standard output, won't if encounters error. in other words, you're checking contents of $reply string instead of exit status read.


Comments