11

So im having an issue where this portion of my script is not working. I'm not sure exactly whats going on, but I get the feeling its because I have not escaped certain characters inside the variables. I tried the following with no luck:

crontab -l | sed "s%$CTMP%\*\/$FREQ \* \* \* \* cd $DIR && \.\/$SCRIPT%" | crontab -

Here is whats in each of the variables:

The CTMP variable is just a line from the crontab file

CTMP='*/5 * * * * cd /home/admin/whatever && ./test.sh'

The FREQ variable is the time in minutes setting of the cron file

FREQ=5

The DIR variable is the current working directory

DIR='/home/admin/whatever'

And the SCRIPT variable is just the script file name

SCRIPT='test.sh'

I feel like the issue is the sed statement is not updating the crontab file because it's not escaping the characters it needs to inside these variables above.

Is that correct or is something else wrong?

* UPDATE *

CTMPESC=$(sed 's/[\*\.]/\\&/g' <<<"$CTMP")
DIRESC=$(sed 's/[\*\.]/\\&/g' <<<"$DIR")
SCRIPTESC=$(sed 's/[\*\.]/\\&/g' <<<"$SCRIPT")
crontab -l | sed "s%$CTMPESC%\*/$FREQ \* \* \* \* cd $DIRESC && \./$SCRIPTESC%" | crontab -

* UPDATE *

Here is the crontab output

*/10 * * * * cd /home/administrator/anm-1.5.0 */7 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh*/7 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh ./anm.sh

and I also echoed the three escaped variables just to troubleshoot and here they are:

\*/7 \* \* \* \* cd /home/administrator/anm-1\.5\.0 && \./anm\.sh
/home/administrator/anm-1\.5\.0
anm\.sh

The three escaped variables actually look good.

* UPDATE *

Cron file before the replace:

*/10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh

Cron file after the replace:

*/7 * * * * cd /home/administrator/anm-1.5.0 */10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh*/10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh ./anm.sh

See all the extra junk that gets thrown in there somehow?

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
Atomiklan
  • 5,164
  • 11
  • 40
  • 62
  • Try setting `noblog`. `set -f` before setting `CTMP`. – devnull Jun 24 '13 at 10:30
  • possible duplicate of [Escape a string for sed search pattern](http://stackoverflow.com/questions/407523/escape-a-string-for-sed-search-pattern) – l0b0 Jun 24 '13 at 10:47

1 Answers1

12

Yes, the problem is likely that $CTMP contains asterisks, which are interpreted by sed as quantifiers. . is also a special character. Try escaping them:

CTMP_ESC=$(sed 's/[\*\.]/\\&/g' <<<"$CTMP")

and then use CTMP_ESC instead of CTMP.

Also, you don't need to escape / in your sed command, because you are not using it as the s/// separator.

Edit: you also need to escape & in the replacement, because sed interprets & as "the matched string". So your script should read:

CTMPESC=$(sed 's/[\*\.&]/\\&/g' <<<"$CTMP")
DIRESC=$(sed 's/[\*\.&]/\\&/g' <<<"$DIR")
SCRIPTESC=$(sed 's/[\*\.&]/\\&/g' <<<"$SCRIPT")
crontab -l | sed "s%$CTMPESC%\*/$FREQ \* \* \* \* cd $DIRESC \&\& \./$SCRIPTESC%" | crontab -
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • 1
    That almost worked! It finally updated, but it added some duplicate stuff to the cron file as well, but at least this means we're very close. See my code above. – Atomiklan Jun 24 '13 at 10:35
  • @user2272450 What do you mean by duplicate stuff? It's hard to tell without seeing the crontab. Did the substitution happen more than once? What were the matching lines? – Lev Levitsky Jun 24 '13 at 10:40
  • @user2272450 And what was the initial content of the crontab? – Lev Levitsky Jun 24 '13 at 10:50
  • Different test some number may be different. Before Replace: */10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh After replace: */7 * * * * cd /home/administrator/anm-1.5.0 */10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh*/10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh ./anm.sh The only thing that should change in this case is the 10 should change to a 7 – Atomiklan Jun 24 '13 at 10:52
  • @user2272450 I get it now! Will update the answer in a moment – Lev Levitsky Jun 24 '13 at 10:55
  • Doing a bit more testing but I think you got it! Thank you very much for actively helping me with this!!! – Atomiklan Jun 24 '13 at 11:02