I have a rather large BASH function that I'm working on. This function is a CronJob generator. This script is intended to be run with SUDO privileges, and will allow the user to inspect the unprivileged user's Crontab file. They can create new cronjobs (a few questions and it does the proper syntax for them), they can also remove a cronjob. That's where I've hit a wall.
In this part of my CASE statement, the user has been asked if they want to create a new cronjob -- they reply with "N" or "n" and we get here:
#!/bin/bash
read -r -p $'Would you like to create a new cronjob? [y/n]\n\n--> ' CRON
case "$CRON" in
y|Y)
echo "not pertinent to this discussion"
;;
n|N)
read -r -p $'\n\nWould you like to REMOVE a crontab entry? [y/n]: ' REMOVE
case "$REMOVE" in
Y|y)
declare -a CRONTAB
while IFS= read -r LINE
do
CRONTAB+=("$LINE")
done < <(grep -v '#' /var/spool/cron/"$SCRIPTUSER")
echo -en "\nPlease select a cronjob to remove from the Crontab file:\n\n"
PS3=$'\n\nPlease enter your selection: '
select LINE in "${CRONTAB[@]}"
do
echo "Going to remove \"$LINE\""
read -r -p $'Is this correct? [y/n]' CHOICE
case "$CHOICE" in
Y|y)
sed "s/$LINE/^#&/g" -i /var/spool/cron/"$SCRIPTUSER"
break
;;
N|n)
break
;;
esac
done
echo -en "\n\nCurrent Crontab entries for $SCRIPTUSER:\n\n"
echo -en "\n\n######################################\n\n$(grep -v '#' /var/spool/cron/"$SCRIPTUSER")\n\n######################################\n\n"
sleep 3
break
;;
N|n)
break
;;
esac
;;
esac
The problem I'm having is these are my cronjob entries I'm testing with:
The SED statement doesn't seem to be doing anything at all. I would imagine the '*' and '/' are probably messing with the SED pattern, and I have already tried a sed where I escaped all the '/' but it still passed over it like nothing was there.
I appreciate the extra set of eyeballs, thank you!