0

I have a program which sometimes writes empty outputs

$ cat ex.py 
import numpy as np
outf = open('out.dat', 'w')
if np.random.random() < 0.2: print >> outf, 'something'

I want to run ex.py until there is content in out.dat. Here is my best attempt so far. This is not working for me. What am I doing wrong?

$ cat ex.sh 
while [ -s out.dat ]
do
   python ex.py
done
~/Documents/bigyawarticle $ . ex.sh 
~/Documents/bigyawarticle $ tail out.dat 

Edit Thanks I got the spacing issue.

kilojoules
  • 9,768
  • 18
  • 77
  • 149
  • 1
    Whitespace matters. `[` is a command (literally, the same command also usable with the name `test`), not a piece of syntax. Just like you have to type `ls -l` (with the space between the command name and its first argument) instead of `ls-l`, you can't run `[-s`. – Charles Duffy Apr 18 '17 at 00:06
  • BTW, this is something http://shellcheck.net/ will catch for you automatically. – Charles Duffy Apr 18 '17 at 00:06
  • Ahh. Now that you've edited it -- note that the while loop only runs if the file is **non**empty. So if the file is already empty, your new code will never run at all. Maybe you wanted `while ! test -s out.dat`? – Charles Duffy Apr 18 '17 at 00:09
  • That did the trick – kilojoules Apr 18 '17 at 00:10

0 Answers0