0

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.

10
  • Consider pasting your code in shellcheck.net first, since it contains some syntax errors (hint: test =$(...) is wrong, know why). Commented Aug 18, 2016 at 8:47
  • 1
    You never Close the string "%d-%m-%y %T Commented Aug 18, 2016 at 8:50
  • 2
    Run the script as bash -x script-name. My date command fails if there is ` space after the + Commented Aug 18, 2016 at 8:59
  • 1
    @streber As previously mentioned, date probably doesn't like the space after the +. Does date + "%d-%m-%y %T" work when run manually? Could you try adding set -ex to the top of the script? Commented Aug 18, 2016 at 9:08
  • 1
    @streber date + "%d-%m-%y %T" with a space prints a date? Mine doesn't. Now I'm confused about what works and what doesn't (and what's this about terminal windows?!). Could you clarify, perhaps edit the question? (set -ex wasn't supposed to solve as much as help debug.) Commented Aug 18, 2016 at 9:16

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

date: No space between + and "

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.