I have a list of many many letsencrypt renew cron files. Each file has a cron schedule that I'm trying replace the hour and minute to be later in the middle of the night. I am trying to do this with a bash script and using sed to update just the hour and minute. For the bloody life of me, I can't get the darn sed to replace. I've tried to look over many of the forums but I can't quite get the right setup. Here's what I got:
for FILE in $(find ./renew -type f -name "letsen*")
do
# This will be the new min / hour
TIMESET="$(($RANDOM % 59 + 0)) $(($RANDOM % 10 + 2))";
sed -e 's/[0-9]+\W[0-9]+})/$TIMESET/' $FILE;
done
Here's an example of the letsencrypt file:
33 19 * * 5 root /bin/bash /home/forge/.letsencrypt-renew/711959 > /home/forge/.letsencrypt-renew/711959.out$
What am I missing? Appreciate any help!
UPDATE
Thanks to Edgar Magallon, here's the solution that worked for me:
for FILE in ./renew/letsen*
do
[[ -f $FILE && ! -L $FILE ]] && {
TIMESET="$(($RANDOM % 59 + 0)) $(($RANDOM % 10 + 2))";
# I think it's best to do a backup like what was suggested.
sed -i'.bak' -Ee "s/[0-9]+[0-9]+/$TIMESET/" "$FILE";
}
done
echo "Done! Have a great day~!";
})
in your sed command? What you want to change is33 19
to the value stored in$TIMESET
or am I wrong? – Edgar Magallon Nov 18 '22 at 23:12$TIMESET
value. – cbloss793 Nov 21 '22 at 16:30