0

I want to write wait until a file exists in bash. How do I do this?

I tried this but it doesnt work:

while [ ! -f outputs\/$trace\_hist_len_$hist_len\_page_size_$page_size\.out]; do
echo "waiting for outputs\/$trace\_hist_len_$hist_len\_page_size_$page_size\.out";
done

It seems to have a syntax error.

6
  • Okay, your formatting probably doesn't look right anymore after indenting the code, sorry about that. Can you check whether your variables look right? And in the future, please indent code with 4 spaces or Ctrl-K so you don't have to escape anything!
    – slhck
    Oct 21, 2012 at 20:49
  • Yeah, It's correct. Thanks for the formatting. Oct 21, 2012 at 20:50
  • 1
    Please post the rest of your code as well. What are the values of $trace, $hist_len and $page_size? What is the error you're getting? Why do you need to escape the /? Oct 21, 2012 at 20:58
  • wait is probably not the command you are looking for. wait will pause a script until all child processes are done, at which point it will continue. Oct 22, 2012 at 13:52
  • I think you have your / and \ mixed up. Path names in bash (even in cygwin) use the / character as the delimiter. Oct 22, 2012 at 15:02

2 Answers 2

8
while : ; do
    [[ -f "/path/to/file" ]] && break
    echo "Pausing until file exists."
    sleep 1
done

Without using something like inotify, this is about the limit of what you're going to be able to do.

The while loop above just uses : as its conditional which pretty much just means "do it until we kill it."

The important line is the [[ test. The test conditional here does not use the if statement. This is because [[ is an actual command and NOT part of the if statement. This executes [[ -f "/path/to/file" ]] and the && checks the exit status of the [[ command and executes the following statement if that exit status is 0 (success). The following statement here is break which will close out the current loop that your script is in.

Note: In bash it is highly recommended that you use [[ instead of [. It has all of the capabilities of [ but is far more powerful and simple to use.

7
  • 1
    There is nothing wrong with while [ ! -f "path" ]; echo Whatever;done In fact, I think your example is silly because you can do: while [[ ! -f path ]]; do echo silly;done Further, the question was about a syntax error, and not about avoiding it with "highly recommended" and less portable bash constructs. Oct 22, 2012 at 14:13
  • No, the question was how can he accomplish a specific task and that his one attempt had failed. He asked how he can accomplish the task, I answered the question. If it is not the best method to obtain the results, then the votes will reflect that. You are correct, using the syntax you indicated is perfectly valid. However when using [[ and simply quoting his filename, it will likely be interpreted more appropriately by the [[ which is one of the reasons it is recommended over [. Oct 22, 2012 at 14:45
  • Quote: I want to write wait until a file exists in bash. How do I do this? Oct 22, 2012 at 14:45
  • You mean your vote will reflect that. His loop is valid with the exception of one missing space character. Oct 22, 2012 at 15:11
  • 1
    Whatever, I'll give you a vote. Silly argument, idk why I got so worked up over this. Let's move on dude. Oct 22, 2012 at 15:18
5

The space between the file argument and the closing bracket is important.

if [ -f "path" ]; then echo good;fi

if [-f "path"] ; then  echo bad;fi
if [ -f "path"]; then  echo bad;fi
if [-f "path" ]; then  echo bad;fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.