开发者

Zsh command substitution

开发者 https://www.devze.com 2022-12-31 10:21 出处:网络
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

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?


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).


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消