shell脚本不会写入文件
问题描述:
我只想编写一个小小的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,但它仍然不写。
答
你的测试是可疑的。更好的办法是:
#!/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
答
date
:+
和"
之间没有空间
date +"%d-%m-%y %T" >> autostartLog.txt
应该工作
首先考虑粘贴在http://www.shellcheck.net/你的代码,因为它包含一些语法错误(提示:'test = $(...)'是错的,[知道为什么](http://*.com/a/2268117/1983854))。 – fedorqui
你永远不会关闭字符串'“%d-%m-%y%T' – Jens
以'bash -x script-name'运行脚本,如果'+' – cdarke