FYI: edit crontab without using a tempfile ( using on your own risk !)

FYI: edit crontab without using a tempfile ( using on your own risk !)

Post by Hajo Ehler » Sat, 16 Jun 2001 19:54:56



Hi folks,
since i could not found any crontab editing while using a script without
using a temp on on google i will pass my solution.
Hopefully that "crontab" itself does not use a temp file

Hajo

#!/bin/ksh
# () MAIN
typeset -u Task="$1"
typeset CronEntry=""
typeset Cmd
typesed UID=0

# Check if we are root
if [[ "$( id -u )" != "$UID" ]] ; then
  echo "INFO: Script must be run under root because it changes root's
crontab. Your current id is : $(id)"
fi

# Check if $1 and $2 are supplied
if [[ "$#" != "2" ]] ; then
  echo "INFO: no enough parameter supplied. Use "-r" to remove and "-a"
to add an crontab entry"
  exit 0
fi

# +++ CRONTAB handling +++
#
{
if [[ "$2" = "-" ]] ; then
  # Read from standard in
  while read CronEntry ; do
    echo "$CronEntry"
  done
else
  # Read from supplied command line
  echo "$2"
fi ; } | sed 's/^\"//;s/\"$//' |\
while read CronEntry ; do

  # Skip any empty line
  [[ "$CronEntry" = "" ]] && continue

  # Get current crontab and the command to execute
  Crontab=$(crontab -l)
  Cmd=$(echo "$CronEntry" | awk '{ print $6 }' )

  # Check if we have a command
  if [[ "$Cmd" = "" ]] ; then
      echo "ERROR: Could not modify the crontab because no command is
supplied in $CronEntry"
      continue
  fi

  # What shell we do ? - add or remove
  case $Task in
  "-A") # Add crontab entrys
    echo "$Crontab" |fgrep -qw "$CronEntry"
    if [[ "$?" = "1" ]] ; then
      # Add $CronEntry to root's crontab if fgrep could not find it.
      echo "INFO: Add \"$CronEntry\" to root's crontab"
      Crontab=$Crontab\\n$CronEntry
      echo "$Crontab" | crontab
    else
      echo "INFO: \"$CronEntry\" already exist in root's crontab"
    fi
  ;;
  "-R") # Remove a crontab entry
    echo "$Crontab" |fgrep -qw "$CronEntry"
    if [[ "$?" = "0" ]] ; then
      # Remove $CronEntry from root's crontab if fgrep could find the
entry.
      echo "INFO: Remove \"$CronEntry\" from root's crontab"
      Crontab=$(echo "$Crontab" |fgrep -v "$CronEntry")
      echo "$Crontab" | crontab
    else
      echo "INFO: \"$CronEntry\" does not exist in root's crontab"
    fi
  ;;
  esac
done

exit 0

 
 
 

FYI: edit crontab without using a tempfile ( using on your own risk !)

Post by Kent Squire » Sat, 16 Jun 2001 21:36:38


On Fri, 15 Jun 2001 12:54:56 +0200, Hajo Ehlers


>Hi folks,
>since i could not found any crontab editing while using a script without
>using a temp on on google i will pass my solution.
>Hopefully that "crontab" itself does not use a temp file

sure, it uses a tmp file - created in /var/tmp.   well, assuming when
you do a crontab -e it dumps you into the vi editor.
Quote:

>Hajo

>#!/bin/ksh
># () MAIN
>typeset -u Task="$1"
>typeset CronEntry=""
>typeset Cmd
>typesed UID=0

># Check if we are root
>if [[ "$( id -u )" != "$UID" ]] ; then
>  echo "INFO: Script must be run under root because it changes root's
>crontab. Your current id is : $(id)"
>fi

># Check if $1 and $2 are supplied
>if [[ "$#" != "2" ]] ; then
>  echo "INFO: no enough parameter supplied. Use "-r" to remove and "-a"
>to add an crontab entry"
>  exit 0
>fi

># +++ CRONTAB handling +++
>#
>{
>if [[ "$2" = "-" ]] ; then
>  # Read from standard in
>  while read CronEntry ; do
>    echo "$CronEntry"
>  done
>else
>  # Read from supplied command line
>  echo "$2"
>fi ; } | sed 's/^\"//;s/\"$//' |\
>while read CronEntry ; do

>  # Skip any empty line
>  [[ "$CronEntry" = "" ]] && continue

>  # Get current crontab and the command to execute
>  Crontab=$(crontab -l)
>  Cmd=$(echo "$CronEntry" | awk '{ print $6 }' )

>  # Check if we have a command
>  if [[ "$Cmd" = "" ]] ; then
>      echo "ERROR: Could not modify the crontab because no command is
>supplied in $CronEntry"
>      continue
>  fi

>  # What shell we do ? - add or remove
>  case $Task in
>  "-A") # Add crontab entrys
>    echo "$Crontab" |fgrep -qw "$CronEntry"
>    if [[ "$?" = "1" ]] ; then
>      # Add $CronEntry to root's crontab if fgrep could not find it.
>      echo "INFO: Add \"$CronEntry\" to root's crontab"
>      Crontab=$Crontab\\n$CronEntry
>      echo "$Crontab" | crontab
>    else
>      echo "INFO: \"$CronEntry\" already exist in root's crontab"
>    fi
>  ;;
>  "-R") # Remove a crontab entry
>    echo "$Crontab" |fgrep -qw "$CronEntry"
>    if [[ "$?" = "0" ]] ; then
>      # Remove $CronEntry from root's crontab if fgrep could find the
>entry.
>      echo "INFO: Remove \"$CronEntry\" from root's crontab"
>      Crontab=$(echo "$Crontab" |fgrep -v "$CronEntry")
>      echo "$Crontab" | crontab
>    else
>      echo "INFO: \"$CronEntry\" does not exist in root's crontab"
>    fi
>  ;;
>  esac
>done

>exit 0


 
 
 

1. Using memory instead of a tempfile

Say I had write permission to a file and not the dir it's in, and
I want to modify the contents of the file. I don't want to assume
that /tmp is world writable, because maybe it's not. Instead, I
want to have the output of my modification command (sed or grep,
whatever) to be stored as the contents of an environment variable,
which I can then echo into the file. Is this practical? what
is the limit of the size of an environment var's content?

Thanks in advance,
Moshe

--

Geek code v3.12 (www.geekcode.com):
GCS/E d- s+:-- a-- C++$ UL++>+++$ P+>++ L+++>$ E--- W+ N++ w--
!O M-- V? PS+ PE Y+ !PGP t 5? X+ R- tv b- DI+ D+ G e>++ h r y

2. xlock doesn't work w/xdm

3. cannot print using lpr without PPP connection using Caldera (Red Hat)

4. SiS6215C video driver needed...or fix

5. A Risk of using exec tag in SSI

6. Best partition setup?

7. Security risk on dual boot WinXP/Linux systems using Partition Magic 8.0?

8. My Modem :(

9. FYI: "snow" using S3 Virge with XFree86 v4

10. Crontab - Script that uses variable.

11. Its your own risk to run linuxsdoom

12. using find in crontab

13. Using tin with crontab how?