2016-08-18 124 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應該工作