Skip to content

Instantly share code, notes, and snippets.

@nfvs
Created March 28, 2014 17:11
Show Gist options
  • Save nfvs/9837830 to your computer and use it in GitHub Desktop.
Save nfvs/9837830 to your computer and use it in GitHub Desktop.
Enable and disable cron jobs in a file by commenting/uncommenting them.
#!/usr/bin/env bash
function usage () {
cat <<EOF
Usage: $0 [-p] -e|-d -f <file>
where:
-e enable cron jobs
-d disable cron jobs
-p print only
-f modify existing file
-h show help
EOF
}
sed_opts='-i'
while getopts 'edf:hp' flag; do
case "${flag}" in
e)
sed_regex='/^#[ ]*\([\*0-9/]\+ \)\{5\}.*$/s/^#[ ]*//'
action='enable'
;;
d)
sed_regex='/^\([\*0-9/]\+ \)\{5\}.*$/s/^/#/'
action='disable'
;;
f)
file=${OPTARG};;
p)
print_only=true
sed_opts=''
;;
h)
echo "Enable/disable cron jobs by commenting/uncommenting them."
usage
exit 0
;;
esac
done
if [ -z "${file}" ] ; then
echo "Error: no file specified."
usage
exit 1
elif [ ! -f "${file}" ] ; then
echo "Error: specified file ${file} does not exist."
usage
exit 1
elif [ -z "${action}" ] ; then
echo "Error: no action specified."
usage
exit 1
fi
if [ -n "${print_only}" ] ; then
echo "Printing output only."
fi
sed ${sed_opts} "${sed_regex}" "${file}" && echo "Cron jobs in ${file} ${action}d.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment