Hi,
I would like to check whether all the directories exists or not. I tried the below but it gives some error. below is the excerpt from my original script
24 #Check if the required directories are exists
25 dirExists() {
26
27 if [ ! -d $SWDIR && ! -d $SWDIR/eaijava && ! -d $ipetoolsdir && ! -d $hawkdir ]
28 then
29 echo "required directories does not exists.. exiting.."
30 exit
31
32 else
33 echo "directories are exists.."
34
35 fi
./backup-1.sh[27]: [: ']' missing
Also, is there anyway to find actually which directory is missing and give appropirate message(s)
Thanks in advance.
&& doesn't work inside [ ].
Your logic is slightly off as well. If dir1 doesn't exist AND dir2 doesn't exist AND dir3 doesn't exist, it'll complain -- if only one or two are missing, it wouldn't.
Better logic would be, if dir1 exists AND dir2 exists AND dir3 exists, then everything's okay, otherwise, error.
Couple ways to do it.
function check_dirs1
{
# && means 'if the first is okay, run the second'. So if any of these don't exist,
# the chain will stop and the function will return nonzero.
[ -d dir1 ] && [ -d dir2 ] && [ -d dir3 ]
}
function check_dirs2
{
# -a instead of && for [ ]
[ -d dir1 -a -d dir2 -a -d dir3 ]
}
function check_dirs3
{
# for bash/ksh, which has double-brackets [[ ]]
[[ -d dir1 && -d dir2 && -d dir3 ]]
}
# If your expression is very large, you can break it into parts
function check_dirs4
{
# || means, 'if the first thing doesn't succeed, run the second'.
# So if dir1 doesn't exist, the function will return immediately
# with a nonzero value.
[ -d dir1 ] || return 1
[ -d dir2 ] || return 1
[ -d dir3 ] || return 1
# once you're sure they all exist, return okay
return 0
}
if ! check_dirs1
then
echo "Required files not found in dirs1"
fi
if ! check_dirs2
then
echo "Required files not found in dirs2"
fi
if ! check_dirs3
then
echo "Required files not found in dirs3"
fi
if ! check_dirs4
then
echo "Required files not found in dirs4"
fi