tags:

views:

73

answers:

2

I usually work with BASH, but I'm trying to setup a cronjob from a machine and user account that is configured with zsh. When the cronjob runs, the date variable does not contain the date, just the string for the command to return the date.

DATE=$(date +%Y%m%d)
55 15 *  *  1-5  scp user@host:/path/to/some/file/$DATE.log /tmp

I've tried using backticks rather than $() around the command, but that did not work either. Is there a special way to do command substitution in zsh?

+2  A: 

I don't think the cron way of setting up environment variables supports the $() stuff. It doesn't even support variable substitutions.

I think you'll either have to put the date calculation into the cron command:

55 15 *  *  1-5  scp user@host:/path/to/some/file/$(date +%Y%m%d).log /tmp

or create another script which sets the variable then calls scp with it (and of course you call that script from within your crontab).

paxdiablo
+3  A: 

If this is a line from the user's crontab, then the line to set the variable isn't set by the shell, it's set by cron itself - and as you've discovered, it doesn't do a lot of interpretation on the values!

The values do get set, but set literal values in the process's environment - and environment variables don't get interpreted further after being expanded - so the $(date) stays as $(date), as you've seen.

The easiest thing to do would be:

55 15 * * 1-5 scp user@host:/path/to/file/`date +%Y%m%d`.log /tmp

The backticks `` are another way to interpolate the result of a command, that works in more shells than $(command) (though it is less flexible, since you can't nest it).

psmears
You can nest `` but it gets messy fast if you have to nest more than one level.
jamessan
You can in some shells, but not in others; I'm not sure of the details so I try to avoid ever nesting it :-)
psmears