0

Hope everyone is having a good day i'm trying to write a script to introduce a cron job via script the following command is working fine when executed in shell script

echo '0 20 * * * touch /global/appaem/aem/wrap-lock/wrap.lock-`date +"%Y%m%d"`' >> /tmp/tmp.txt
more /tmp/tmp.txt 
0 20 * * * touch /global/appaem/aem/wrap-lock/wrap.lock-`date +"%Y%m%d"

But when the same command is being executed from script it's displaying the below o/p

/global/appaem/aem/wrap-lock/wrap.lock-20170707

can some help here

robotTech
  • 63
  • 1
  • 7
  • With what? You have touched a file called global/appaem/aem/wrap-lock/wrap.lock-20170707? With 20170707 derived from the date command. – Raman Sailopal Jul 07 '17 at 14:52
  • Consider [this answer](https://stackoverflow.com/a/878647) – shanmuga Jul 07 '17 at 14:55
  • So, your commands work fine for me in bash, sh, and when plugged directly into terminal. What OS are you running? Possibly try escaping your ` ` ` (grave accent) like so ` \\` `. – Kip K Jul 07 '17 at 15:03
  • @KipK yes it does work in normal shell when u type , but it doesn't when you execute it through shell script , because shell is interpreting and execute date command in echo , Eduard has provided the solution. we should use escape character to make sure shell doesn't interpret it – robotTech Jul 07 '17 at 15:19

1 Answers1

1

First of all, you should prefer $() in bash to fork a subshell.

To print out your desired command without being interpreted by bash, you need to escape $, i.e.

#!/bin/bash
echo "0 20 * * * touch /global/appaem/aem/wrap-lock/wrap.lock-\$(date +\"%Y%m%d\")" >> /tmp/tmp.txt
Eduard Itrich
  • 700
  • 1
  • 7
  • 19