streber
streber

Reputation: 41

shell script doesn't write into file

I just want to write a little shell script, which restarts my dosbox everytime it crashes.

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

and it restarts fine, but I can't write into my autostartLogs.txt.

I tried

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

in the terminal and it worked perfectly, but if I use it in my script it doesn't do anything.

edit: used checker, but it still doesn't write.

Upvotes: 0

Views: 944

Answers (2)

cdarke
cdarke

Reputation: 44354

Your test is suspect. A better way would be:

#!/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

Upvotes: 3

Thirupathi Thangavel
Thirupathi Thangavel

Reputation: 2465

date: No space between + and "

date +"%d-%m-%y %T" >> autostartLog.txt should work

Upvotes: 1

Related Questions