bash checking directory existence hanging when NFS

2019-07-20 15:32发布

问题:

I have a .bash_profile script that sets up some aliases for me based on directory existence:

if [ -d  /home/user/games ] ; then
  alias cdgames='cd /home/user/games'
fi

One of these directory is on an NFS mount - if the filer becomes unresponsive su - user will hang on this line in .bash_profile.

Is there any way to check existence of a directory in bash without causing a hang if the directory is mounted to an unresponsive filer?

回答1:

As the folder should appear as a mount device in /etc/mtab you can try something like this

if grep -q '/home/user/games' /etc/mtab ; then 
    alias cdgames='cd /home/user/games'
fi

This approach is a bit rude but it works for most of the situations.