jueves, 5 de mayo de 2011

Script Bash para modificar algunas opciones de .bashrc

Presentamos un script en bash para modificar algunas propiedades del fichero .bashrc

#!/bin/bash 


function menu(){
echo "1. Cambia HISTCONTROL"
echo "2. Cambia HISTSIZE"
echo "3. Cambia UMASK"
echo "4. Cambia alias"
echo "5. Salir"
}

function cambia_historycontrol(){
 echo "Introduce el nuevo valor de HISTCONTROL"

 read hist
 rep=`cat $1'/.bashrc'  | grep HISTCONTROL | wc -l`
 if [ rep -eq 0 ]
 then
  echo 'HISTCONTROL='$hist >> $1'/.bashrc'  #añadimos al final
 else
  sed 's/HISTCONTROL=.*/HISTCONTROL='$hist'/' $1'/.bashrc' > $1'/.bashrc' #modificamos su valor
 fi
}

function cambia_historysize(){
 echo "Introduce el nuevo valor de HISTSIZE"
 read hist
 rep=`cat $1'/.bashrc'  | grep HISTSIZE | wc -l`
 if [ rep -eq 0 ]
 then
  echo 'HISTSIZE='$hist >> $1'/.bashrc' #añadimos al final
 else
  sed 's/HISTSIZE=.*/HISTSIZE='$hist'/' $1'/.bashrc' > $1'/.bashrc' #modificamos su valor
 fi
}

function cambia_umask(){
 echo "Introduce el nuevo valor de UMASK"
 read mask
 rep=`cat $1'/.bashrc'  | grep $mask | wc -l`
 if [ $rep -eq 0 ]
 then
  echo 'umask='$mask >> $1'/.bashrc' #añadimos al final
 else
  sed 's/umask=.*/umask='$mask'/' $1'/.bashrc' > $1'/.bashrc' #modificamos su valor
 fi
}

function cambia_alias(){
 echo "Introduce un alias"
 read alia
 num=`cat $1'/.bashrc'  | grep '^'$alia | wc -l`
 if [ $num -le 9]
 then
  rep=`cat cpbash | grep '^'$alia | wc -l`
  if [ rep -eq 0 ]
  then
   echo 'alias='$alia >> $1'/.bashrc' #añadimos al final
  else
   sed 's/alias=.*/umask='$mask'/' $1'/.bashrc' > $1'/.bashrc' #modificamos su valor
  fi
 else
  echo "ya tenemos mas de 10 alias"
 fi
}


#Introduce el path donde deseas modificar el fichero .bashrc
var='ko'
while [ $var = 'ko' ]
do

 echo "Introduce el path"
 read path

 if [ -d $path ]
 then
  echo "Correcto"
  while true
  do
   menu
   echo "elegir opcion"
   read opcion
   case $opcion in
    "1")cambia_historycontrol $path;;
    "2")cambia_historysize $path;;
    "3")cambia_umask $path;;
    "4")cambia_alias $path;;
    "5")exit;;
    *)echo "Opcion de 1 a 5";;
   esac
  done
  var='ok'
 else
  echo "La ruta indicada no existe"
  var='ko'
 fi

done

No hay comentarios:

Publicar un comentario