Crontab commands [crontab -e/r] issues

Hello All,

My project needs, people to work with the crontab frequently . As the letters e/r are adjacent to each other , many times

crontab -r

command was issued unintentionally instead of

crontab -e

and we end up deleting the crontab .

Is there a way, that I can make it interactive so that upon executing any

crontab  

command it will ask for confirmation.

Many Thanks.

You could write a script that asks and then executes /bin/crontab. Put this in the PATH before /bin (eg /usr/local/bin):

Script could be something like this as an example. You may not want to prompt if ACTION is empty, I'll leave that as a exercise for you:

function yorn
{
   yn=""
   while [ "${yn}:" = ":" ]
   do
       printf "%s (y/n)? " "$1"
       read yn

       case $yn in
           [yY]|[Yy][Ee][Ss]) true;;
           [nN]|[nN][oO]) false;;
           *) yn="";;
       esac
  done
}

while getopts reu:l arg
do
 case $arg in
   r) ACTION=${ACTION}REMOVE;;
   e) ACTION=${ACTION}EDIT;;
 esac
done

if yorn "Are you sure you want to $ACTION"
then
     # They said yes so execute: /bin/crontab $@
    /bin/crontab $@
else
     echo "Abort"
fi
4 Likes

A bit more portable (ash, dash) is

yorn(){
...
}

And test takes -z

while [ -z "$yn" ]
do
...