Hello All,
I have two issues.
1).I want to check if directory exists and inside that if file exists with today's date minus one. I can check directory exists but how can i check only a pattern of filename in that directory.Name of file is files-20170105-09.gz.
2).Also i want to exit immediately if directory is not present. Is exit 0 correct. Please help.
if [[ -d "$dir" ]]; then echo "Dir exists $dir"; else echo "dir $dir doesnot exists"; exit 0; fi
and think that multi-line is better readable and maintainable.
Problem #2 "test for matching files" can be solved with a file_exists function.
file_exists(){
for _i do
[ -f "$_i" ] && return
done
return 1
}
if [[ -d "$dir" ]]
then
echo "Dir exists $dir"
if file_exists "$dir/$filename"*
then
echo "Filename $filename is correct"
else
echo "Filename is not correct"
fi
else
echo "dir $dir doesnot exists"
exit 0
fi
In such a structure I can often omit an exit or return .
Hello RudiC,
In the directory I will get filename say files-20170105-09.gz. I have to check if file exists also if exists then it should have today's date minus one. So I have to search this part 20170105 . Rest of the part of file , I will not be knowing. That is the reason to check it I was using wildcard *
---------- Post updated at 07:36 AM ---------- Previous update was at 07:34 AM ----------
Just guessing: [[ -f '$dir/$filename*' ]] does not supply your filename. man bash :
You could try to use [ ... ] . But, some caveats are to be considered:
Within single quotes, no variable expansion will be done - use double quotes instead. And, within quotes, the * loses its wildcard meaning - put it outside the quotes. And, make very sure that the expansion yields one single filename only to avoid a bash error.
You can do it with ls -d , but the external program means some overhead, and you must discard stdout and stderr, and there are some border cases if special files would match.
--
The function lets the shell expand the arguments; the for loop cycles through them, tests them, and returns immediately with status 0 ("true") if one is present.