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.

streber
  • 41
  • 1
  • 4
  • Consider pasting your code in http://www.shellcheck.net/ first, since it contains some syntax errors (hint: `test =$(...)` is wrong, [know why](http://stackoverflow.com/a/2268117/1983854)). – fedorqui Aug 18 '16 at 08:47
  • 1
    You never Close the string `"%d-%m-%y %T` – Jens Aug 18 '16 at 08:50
  • 2
    Run the script as `bash -x script-name`. My `date` command fails if there is ` space after the `+` – cdarke Aug 18 '16 at 08:59
  • my file and the script are in the same folder, and i edited my code now, but still does work only in the terminal – streber Aug 18 '16 at 09:01
  • 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? – Biffen Aug 18 '16 at 09:08
  • @Biffen the command runs manually and when i use `bash -x script-name ` it works fine, but i dont want to get an extra terminal window for this. adding `set -ex` did not helped. – streber Aug 18 '16 at 09:15
  • 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.) – Biffen Aug 18 '16 at 09:16
  • @Biffen it works when i don't use a space between `+` and `"%d-%m-%y %T" ` :S . if i use the command in my terminal or start the script with `bash -x script-name` like @cdarke told me, everything works, but if i just double-click my script (linux mint) it doesn't. – streber Aug 18 '16 at 09:23
  • @streber ‘*Doesn't work*’ isn't much of a problem description. Anyway, that sounds like a new question, and should be addressed in a new question. – Biffen Aug 18 '16 at 09:28
  • well the script seems to work then, then there have to be problems with the rights or something...? :S – streber Aug 18 '16 at 09:30

2 Answers2

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
cdarke
  • 42,728
  • 8
  • 80
  • 84
1

date: No space between + and "

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

Thirupathi Thangavel
  • 2,418
  • 3
  • 29
  • 49