shell脚本中的"一元运算符预期"

iaa*_*aav 1 unix bash shell scripting while-loop

我需要一个脚本来保持轮询"receive_dir"目录,直到"stopfile"写入目录.

这必须运行尽管空目录.

到目前为止,我有这个,但如果receive_dir为空,没有"一元运算符预期"的文件,则会失败.救命 !!

#!/usr/bin/ksh

until [ $i = stopfile ]
 do
   for i in `ls receive_dir`; do
   time=$(date +%m-%d-%Y-%H:%M:%S)
   echo $time
   echo $i;
  done
done
Run Code Online (Sandbox Code Playgroud)

And*_*son 6

这将按照您的要求执行(循环直到停止文件存在).我添加了"睡眠1"以降低资源使用率.使用"#!/ usr/bin/env ksh"作为shebang也是一种好习惯.

#!/usr/bin/env ksh

until [ -e receive_dir/stopfile ]
 do
   time=$(date +%m-%d-%Y-%H:%M:%S)
   echo $time
   sleep 1
done
Run Code Online (Sandbox Code Playgroud)