0

I need to write a while loop to check for a file existence.

My requirement is: check for the file only for 5 minutes. If file come in that path within 5 minutes exit the loop and continue rest of the script otherwise exit from the script after 5 minutes with an error 'file not found'. I wrote the code like this :

SOURCEFILE=/path/*file.csv    
StartTime=$(date +'%s')    
TimeSpan=300
EndTime=$((StartTime + TimeSpan))

while [[ ! -f ${SOURCEFILE} && $(date +'%s') < ${EndTime} ]]    
do

  echo "inside loop"    
  sleep 25

done    
echo "outside loop"

But with this while loop, even if the file is present in the mentioned path, it is going inside loop and will exit only after 300 seconds. I am beginner in shell scripting and I am not able to understand the issue. I am using ksh.

I could also tell you that it works find with while [ ! -f {SOURCEFILE} ] only. But whenever I add any && condition to while loop , then the -f is not working properly.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rima Joy
  • 3
  • 3

2 Answers2

1

The SOURCEFILE=/path/*file.csv is wrong in your case. It can't be evaluated right with the -f flag.

An easy solution would be to use find or ls and count the result:

find /path/ -name "*file.csv" -type f
# then count the result...

Now I think there is a logic issue with the operators precedence. To force evaluation of the ! for the -f only, use parenthesis. Here is what works for me, and you must adapt it a little to match the * before file.csv:

while [[  ( ! -f file.csv ) && $(date +'%s') < ${EndTime} ]]
do
  echo "inside loop"    
  sleep 25
...

There are some more explanation on this answer. The "and" operator precedes the "not", that's why you had the issue.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • Hi Chomel , Thank You for the reply .But it didn't work .even if the fiel is not present in the location , RETCODE is getting value as 0 .I am not able to understand why ? Because of this it is not entering in to the loop. – Rima Joy Jul 21 '17 at 11:22
  • in my previous code when i just used while [ ! -f {SOURCEFILE} ] loop worked fine . but whenever i add any && condition to while loop , then the -f is not working properly – Rima Joy Jul 21 '17 at 11:25
  • Yeah sorry I didn't test the other case `:) – J. Chomel Jul 21 '17 at 11:35
1

Your primary issue is getting the asterisk (*) to expand at the 'right time'.

It doesn't help that the [ ] and [[ ]] constructs behave differently, especially when it comes to if/when to expand that asterisk. [You can peruse the google search for 'ksh single bracket vs double bracket' for more details.]

Try running the following to see the differences between single/double brackets and unquoted/single-quoted/double-quoted variable:

SOURCEFILE=/path/*file.csv
set -x
[ ! -f ${SOURCEFILE} ] && echo 'missing'
[ ! -f '${SOURCEFILE}' ] && echo 'missing'
[ ! -f "${SOURCEFILE}" ] && echo 'missing'
[[ ! -f ${SOURCEFILE} ]] && echo 'missing'
[[ ! -f '${SOURCEFILE}' ]] && echo 'missing'
[[ ! -f "${SOURCEFILE}" ]] && echo 'missing'

NOTE: Notice which tests expand the asterisk and which are looking for a (literal) asterisk in the name.

NOTE: Try adding a space to your file name (eg, *file XX.csv) and run the above tests ... tricky, tricky, tricky ...

For this particular case ... asterisk/wildcard in file name, no spaces, ksh ... you'll likely be ok with something like:

while [[ ! -f ${SOURCEFILE} ]] && [[ $(date +'%s') < ${EndTime} ]]
markp-fuso
  • 28,790
  • 4
  • 16
  • 36