Advertisement
sergio_educacionit

cron_abm-v3.sh

Oct 5th, 2022 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.25 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. # funcion
  5.  
  6. func_root () {
  7.  
  8.     test $UID -eq 0
  9. }
  10.  
  11. func_help () {
  12.     echo "Ejecute este script con privilegios de 'root'."
  13.     echo "Modo de uso:"
  14.     echo "  $0 opciones argumentos"
  15.     echo "  Donde argumentos es un usuaio del sistema"
  16.     echo ""
  17.     echo "-c    añade un cron"
  18.     echo "-d    elimna un cron"
  19.     echo "-i    imprime un cron"
  20.     echo "-h --help imprime esta ayuda"
  21.     echo ""
  22. }
  23.  
  24. func_user () {
  25.     local user=$1
  26.  
  27.     if grep ^$user /etc/passwd > /dev/null 2>&1 ; then
  28.            # si el usuario existe el exit status es 0
  29.         return 0
  30.     else
  31.            echo -e "El usuario no exite\n"
  32.            # de lo contrario es 1
  33.            return 1
  34.     fi
  35. }
  36.  
  37.  
  38. # Parametros
  39.  
  40. arr_params=( $1 $2 )
  41.  
  42.  
  43. cron_dir="/var/spool/cron/crontabs"
  44.  
  45. cron_tab="0 1 * * * /bin/false"
  46.  
  47. user="${arr_params[1]}"
  48.  
  49.  
  50. case ${arr_params[0]} in
  51.  
  52.     -h|--h)
  53.         func_help
  54.         exit   
  55.         ;;
  56.     -c)
  57.         if ! func_root ;then
  58.             func_help
  59.             exit 1
  60.         elif ! func_user $user; then
  61.                 exit 1
  62.         else
  63.             echo "$cron_tab" >> $cron_dir/${arr_params[1]}
  64.         fi
  65.     ;;
  66.     -d)
  67.         if func_root ;then
  68.            
  69.             rm $cron_dir/${arr_params[1]}
  70.         else
  71.             func_help
  72.             exit 1
  73.         fi
  74.  
  75.     ;;
  76.     -i)
  77.     cat $cron_dir/${arr_params[1]}
  78.     ;;
  79.     *)
  80.     echo "No se paso niguna opcion válida o el usuario no existe"
  81.     exit 1
  82.  
  83. esac
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement