简体   繁体   中英

Zsh command substitution

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM