1

I'm on VMWare which, no matter what anyone says, responds to most unix commands.

Some things are unique though, for example the fact that almost no files are persistent through reboot. Due to this I'm getting a problem with a scripted backup which is using a variable to name log files:

If I manually insert

backup-$(date +%Y-%m-%d).log

into cron, log files will of course be named after the date they were started. However, if I insert this into local.sh to re-enter cron job at reboot;

/bin/echo "0 0 * * 1-5 ...backup-$(date +%Y-%m-%d).log

it will of course end up like this:

backup-2014-07-01.log

So the question is how do I write this to cron, so that it ends up as a varible instead of a date?

3
  • 1
    Escaping certain characters might help.
    – devnull
    Jul 1, 2014 at 7:25
  • I'm up for that if I could achieve the same result. Would you elaborate?
    – Phatsta
    Jul 1, 2014 at 8:12
  • Think about it: you need to prevent the command substitution to happen immediately. How would you do that? Answer: Escape the $.
    – devnull
    Jul 1, 2014 at 8:23

1 Answer 1

1

The dollar sign in your string is being expanded at the time the echo is run and causing command substitution to happen then. What you want is to pass that string AS A STRING on to the file. There are two ways you can keep the substitution from happening.

  1. You can escape the dollar sign:

     /bin/echo "0 0 * * 1-5 ../..backup-\$(date +%Y-%m-%d).log"
    
  2. You can use singe quotes instead of double to wrap the string:

    /bin/echo '0 0 * * 1-5 ../..backup-$(date +%Y-%m-%d).log'
    

Either way you can test this statement from any shell to see whether it gives you the literal string or runs the substitution.

1
  • It sure works, just tested it. Thanks for that!
    – Phatsta
    Jul 1, 2014 at 8:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .