2

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?

John Conde
  • 217,595
  • 99
  • 455
  • 496
mr_van
  • 105
  • 1
  • 2
  • 10
  • Just an idea: maybe you can create an alias everytime, and the alias will check the directory: `alias cdgames='if grep /home/user/games /etc/mtab; then cd /home/user/games ; else echo "Directory not changed: not mounted"; fi – uzsolt Feb 06 '12 at 19:34

1 Answers1

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.

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
  • Brilliant - I replaced the code with: `if grep '/home/user/games' /etc/mtab 1>/dev/null; then alias cdgames='cd /home/user/games' elif [ -d '/home/user/games ] ; then alias cdgames = 'cd /home/user/games' fi` – mr_van Feb 06 '12 at 14:02
  • DRY: `if grep '/home/user/games' /etc/mtab 1>/dev/null || [ -d '/home/user/games' ] ; then alias cdgames = 'cd /home/user/games' fi` – glenn jackman Feb 06 '12 at 14:24
  • I edit my answer to add the -q parameter. It avoids print the result, and its cleaner that pipe to /dev/null – Francisco Puga Feb 07 '12 at 08:42