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?