Is it possible to use case when checking if a folder exist?
I need to check if these folder exist [5 folders to be exact]. All of them could be present but I still need to check. If a folder is present, something will be processed. Different folder do different stuff.
location=/home/myhome/dir
case $stuff in
$location/folder1)
#do something here
;;
$location/folder2)
#do something here
;;
$location/folder3)
#do something here
;;
$location/folder4)
#do something here
;;
$location/folder5)
#do something here
;;
esac
I want to check the existence of each folder then do something if folder is present.
Is this possible? or should I just use 5 ifs? any suggestions?
location=/home/myhome/dir
for stuff in *
do
if [ -d $stuff ]
then
case $stuff in
$location/folder1)
#do something here
;;
$location/folder2)
#do something here
;;
$location/folder3)
#do something here
;;
$location/folder4)
#do something here
;;
$location/folder5)
#do something here
;;
*)
#Do something for any other value that is a directory
;;
esac
else
# Do something for an item that is not a directory
fi
done
Have I missed the point?
Of course, this will not work with files/directories starting with a full-stop, e.g. .profile
I hope that this helps
Robin
Liverpool/Blackburn
UK
I'm still not quite sure what you are trying to do - perhaps you could define a function to test something is in a given folder like this:
location=/home/myhome/dir
function stuff_in
{
for f in "$1"/*
do
[ -f "$f" ]
return
done
}
if stuff_in "$location/folder1"
then
echo "$location/folder1 is populated"
fi
if stuff_in "$location/folder2"
then
echo "$location/folder2 is populated"
fi
if stuff_in "$location/folder3"
then
echo "$location/folder3 is populated"
fi
Again ignoring files starting with . (like .profile)