shell脚本不写入文件

时间:2016-08-18 08:45:51

标签: bash shell

我只想写一个小的shell脚本,每次崩溃都会重启我的dosbox。

#!/bin/bash
while [ "1" == "1" ]
do
  test=$(pgrep dosbox)
  if [ "$test" == '' ]
    then
      date + "%d-%m-%y     %T" >> autostartLog.txt
      dosbox emulator
  fi
done

它重新启动很好,但我无法写入我的autostartLogs.txt。

我试过

echo $(date + "%d-%m-%y     %T) >> autostartLog.txt
在终端中

并且它工作得很好,但是如果我在我的脚本中使用它它就什么都不做。

编辑:使用checker,但仍然没有写。

2 个答案:

答案 0 :(得分:3)

您的测试是可疑的。更好的方法是:

#!/bin/bash

while :      # Tidier infinite loop
do
  if ! pgrep dosbox          # Note we are testing the failure of the pgrep
  then
      date "+%d-%m-%y     %T" >> autostartLog.txt
      dosbox emulator
  fi
done

答案 1 :(得分:1)

date+"

之间没有空格

date +"%d-%m-%y %T" >> autostartLog.txt应该有效