Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 20:49
    
Yeah, It's correct. Thanks for the formatting. –  snihalani Oct 21 '12 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 /? –  Ansgar Wiechers Oct 21 '12 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. –  UtahJarhead Oct 22 '12 at 13:52
    
I think you have your / and \ mixed up. Path names in bash (even in cygwin) use the / character as the delimiter. –  UtahJarhead Oct 22 '12 at 15:02

2 Answers 2

up vote 4 down vote accepted
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.

share|improve this answer
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 '12 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 [. –  UtahJarhead Oct 22 '12 at 14:45
    
Quote: I want to write wait until a file exists in bash. How do I do this? –  UtahJarhead Oct 22 '12 at 14:45
    
You mean your vote will reflect that. His loop is valid with the exception of one missing space character. –  Ярослав Рахматуллин Oct 22 '12 at 15:11
    
You can't vote on your own answers. Nice try. –  UtahJarhead Oct 22 '12 at 15:14

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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