Linux bash在输出文件不为空时运行python

Linux bash在输出文件不为空时运行python,linux,bash,shell,unix,sh,Linux,Bash,Shell,Unix,Sh,我有一个程序,有时写空输出 $ cat ex.py import numpy as np outf = open('out.dat', 'w') if np.random.random() < 0.2: print >> outf, 'something' 编辑 谢谢,我有间隔问题 空格很重要[是一个命令(从字面上看,同一个命令也可以与名称test一起使用),而不是一段语法。就像您必须键入ls-l(命令名与其第一个参数之间有空格)而不是ls-l,您不能运行[-s.BTW,这

我有一个程序,有时写空输出

$ cat ex.py 
import numpy as np
outf = open('out.dat', 'w')
if np.random.random() < 0.2: print >> outf, 'something'
编辑
谢谢,我有间隔问题

空格很重要
[
是一个命令(从字面上看,同一个命令也可以与名称
test
一起使用),而不是一段语法。就像您必须键入
ls-l
(命令名与其第一个参数之间有空格)而不是
ls-l
,您不能运行
[-s
.BTW,这是一些会自动为您捕获的内容。啊。现在您已经编辑了它--请注意,while循环仅在文件为非空时运行。因此,如果文件已为空,您的新代码将永远不会运行。也许您想要
while!test-s out.dat
?这就成功了
$ cat ex.sh 
while [ -s out.dat ]
do
   python ex.py
done
~/Documents/bigyawarticle $ . ex.sh 
~/Documents/bigyawarticle $ tail out.dat