/bin/sh: Syntax error: Unterminated quoted string

BashUbuntu

Bash Problem Overview


I am trying to get a cronjob to pipe output into a dated file in a specified (folder) location.

My crontab entry looks something like this:

* * * * * /some/path/test.sh >> $(date "+/home/oompah/logs/%Y%m%d.test.log")

What I don't understand is that when I type this command at the console, I get the correct string:

echo $(date "+/home/oompah/logs/%Y%m%d.test.log")
/home/oompah/logs/20110329.test.log

What's causing this error and how may I fix it?

bash version info is:

GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

Bash Solutions


Solution 1 - Bash

You should excape the percent signs in your crontab:

* * * * * /some/path/test.sh >> $(date "+/home/oompah/logs/\%Y\%m\%d.test.log")

Percent (%) signs have a special meaning in crontabs. They are interpreted as newline characters.

Solution 2 - Bash

Put the date command inside the script. cron isn't necessarily running the shell you think it is.

Solution 3 - Bash

make sure you have shebang #!/bin/bash as the first line in your script. Also, as bmargulies stated, put the date command inside the script if possible.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoompahloompahView Question on Stackoverflow
Solution 1 - BashbmkView Answer on Stackoverflow
Solution 2 - BashbmarguliesView Answer on Stackoverflow
Solution 3 - BashkurumiView Answer on Stackoverflow