2016-08-18 99 views
0

我只想编写一个小小的shell脚本,每次它崩溃时都会重新启动我的dosbox。shell脚本不会写入文件

#!/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,但它仍然不写。

+0

首先考虑粘贴在http://www.shellcheck.net/你的代码,因为它包含一些语法错误(提示:'test = $(...)'是错的,[知道为什么](http://stackoverflow.com/a/2268117/1983854))。 – fedorqui

+1

你永远不会关闭字符串'“%d-%m-%y%T' – Jens

+2

以'bash -x script-name'运行脚本,如果'+' – cdarke

回答

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

date+"之间没有空间

date +"%d-%m-%y %T" >> autostartLog.txt应该工作