jeudi 20 février 2020

Is there a simple test in Bash to test if a logical path is a directory

The question is best explained through a small example:

Create an infrastructure of directories as follows:

rm -rf /tmp/work  
mkdir -p /tmp/work/a/b/c  
cd /tmp/work  
ln -s a/b/c s  
mkdir t  
tree  

This results in the following infrastructure:

Directory infrastructure

There is a very simple way to check for the existence of a directory:

cd /tmp/work
if test -d s/../t; then echo EXISTS; else echo DOES NOT EXIST; fi

Directory test

However, it seems that the '-d' test only checks for the physical path (a/b/t); The following clearly shows that the logical path/directory does exist:

cd s/../t
pwd

Logical path

I am hoping for a simple test to get this situation checked... Checking for similar questions in StackOverflow, I haven't found an answer that differentiates logical paths from physical paths...

I did a brute force (silly) attempt to get some more insight:

for o in -b -c -d -e -f -g -G -k -h -L -O -p -r -S -s -t -u -w -x; do
  if test $o s/../t; then echo EXISTS; else echo DOES NOT EXIST; fi
done

As expected, all of the tests indicate failure...

Aucun commentaire:

Enregistrer un commentaire