Outils pour utilisateurs

Outils du site


expert:script_bashrc

Script bashrc

Origine : Linux Pour Les Nuls
Auteur : frz
Date : 5 juillet 2006


Introduction

Bonjour à tous. Ça ne fait seulement que 1 semaine que j'utilise Linux (Mandriva) et je crois que je me débrouille quand même bien.. :-P Sur les forums ont retrouve souvent des questions sur le fichier bashrc, sur comment éditer son prompt dans le shell etc.. Alors voici vos réponse! :-P

Avant de commencer, voici quelque chose qu'il faut que vous sachiez : Il y a un bashrc :

  • dans le dossier /etc,
  • dans /home/nom-perso ,
  • et dans /root.

Les fichiers dans /home/nom-perso et /root peuvent être configurés séparément. Donc si vous ouvrez votre konsole en mode user normal, c'est donc le fichier bashrc de /home/nom-perso qui sera lu. Si vous faites la commande su pour passer en mode root, alors ce sera le fichier bashrc de /root qui sera lu. Le fichier bashrc de /etc est utilisé par les 2 modes, soit user normal et mode root. Donc que vous soyez en mode user normal ou root, ce sera le fichier bashrc de /etc qui sera utilisé.

IMPORTANT

Les 2 premiers scripts sont pour ceux qui veulent utiliser les 2 bashrc différents. Le dernier est celui de /etc. Donc vous choisissez parmi ces 2 options.


Option 1

NORMAL USERs (/home/frz/.bashrc)

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

# Prompt konsol shell
PS1="[u@H W] "


#### Perso ####
#############

# Défini les couleurs
BLACK=`e[0;30m`
BLUEF=`e[0;34m`
BLUEP=`e[1;34m`
GREENF=`e[0;32m`
GREENP=`e[1;32m`
CYANF=`e[0;36m`
CYANP=`e[1;36m`
REDF=`e[0;31m`
REDP=`e[1;31m`
PURPLEF=`e[0;35m`
PURPLEP=`e[1;35m`
BROWNF=`e[0;33m`
BROWNP=`e[1;33m`
GRAYF=`e[0;37m`
GRAYP=`e[1;37m`
YELLOW=`e[1;33m`
WHITE=`e[1;37m`
NC=`e[0m` # No Color


# Message de bienvenue
clear
# Message de bienvenue
if [ "$USER" = "mathieu" ]; then
  echo -e "${REDP}Welcome master. You are logged on $HOSTNAME.${NC}"
  uname -a
  echo ""
else
  echo -e "${REDP}Welcome $USER. You are logged on $HOSTNAME.${NC}"
  uname -a
  echo ""
fi

# Aliases (variables)
alias home=`cd /home`
alias frz=`cd ~`
alias desktop=`cd /home/frz/Desktop`
alias frzd=`cd /home/frz/Desktop/frz`
alias lsa=`ls -Al` # List all files (+hidden files)


# Functions
function cmds() # savoir mes commandes
{
echo -e " ${REDP}Liste des commandes:"
echo -e "${GREENP} -Alias-"
echo -e "${BLUEP}aliasp: ${NC}Liste les commandes alias persos."
echo -e " ${GREENP} -Functions-"
echo -e "${BLUEP}clog: ${NC}Clear les logs."
echo -e "${BLUEP}info: ${NC}Informations sur la session ouverte et l`ordinateur."
echo ""
}

function aliasp() #savoir mes alias
{
  echo -e " ${REDP}Liste des aliases persos:"
  echo -e "${BLUEP}home: ${NC}Dossier home (~)."
  echo -e "${BLUEP}frz: ${NC}Dossier frz."
  echo -e "${BLUEP}desktop: ${NC}Dossier Desktop."
  echo -e "${BLUEP}frzd: ${NC}Dossier frz sur le desktop."
  echo -e "${BLUEP}lsa: ${NC}Liste tout les fichiers + fichiers cachés."
  echo ""
}
function colors()
{
  echo -e " ${REDP}Code de couleurs:"
  echo -e "${BLUEF}BLUEF ${NC}<--> ${BLUEP}BLUEP"
  echo -e "${GREENF}GREENF ${NC}<--> ${GREENP}GREENP"
  echo -e "${CYANF}CYANF ${NC}<--> ${CYANP}CYANP"
  echo -e "${REDF}REDF ${NC}<--> ${REDP}REDP"
  echo -e "${PURPLEF}PURPLEF ${NC}<--> ${PURPLEP}PURPLEP"
  echo -e "${BROWNF}BROWNF ${NC}<--> ${BROWNP}BROWNP"
  echo -e "${GRAYF}GRAYF ${NC}<--> ${GRAYP}GRAYP"
  echo -e "${YELLOW}YELLOW"
  echo -e "${WHITE}WHITE"
  echo -e "${NC}NC (NO COLOR)"
  echo ""
}

function info()   # avoir les infos dla session ouverte et pc
{
  echo -e " You are logged on ${BLUEP}$HOSTNAME"
  echo -e " ${REDP}Additionnal information:$NC " ; uname -a
  echo -e " ${REDP}Users logged on:$NC " ; w -h
  echo -e " ${REDP}Current date :$NC " ; date
  echo -e " ${REDP}Machine stats :$NC " ; uptime
  echo -e " ${REDP}Memory stats :$NC " ; free
  my_ip 2>&- ;
  echo -e " ${REDP}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
  echo -e " ${REDP}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"}
  echo ""
}


function clog() # Clear les logs
{
if [ "$UID" = "0" ]; then # 0 = level r00t
 cd /var/log
 cat /dev/null > messages
 cat /dev/null > wtmp
 echo -e "${REDP}Logs cleaned up.${NC}"
 echo ""
else
 echo -e "${REDP}That cmd request a r00t access.${NC}"
 echo ""
fi
}

root bash (/root/bashrc)

# .bashrc
  PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin
ENV=$HOME/.bashrc
USERNAME="root"
export USERNAME ENV PATH

# Source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

# Prompt konsol shell
PS1="[u@H W] "


#### Perso ####
#############

# Défini les couleurs
BLACK=`e[0;30m`
BLUEF=`e[0;34m`
BLUEP=`e[1;34m`
GREENF=`e[0;32m`
GREENP=`e[1;32m`
CYANF=`e[0;36m`
CYANP=`e[1;36m`
REDF=`e[0;31m`
REDP=`e[1;31m`
PURPLEF=`e[0;35m`
PURPLEP=`e[1;35m`
BROWNF=`e[0;33m`
BROWNP=`e[1;33m`
GRAYF=`e[0;37m`
GRAYP=`e[1;37m`
YELLOW=`e[1;33m`
WHITE=`e[1;37m`
NC=`e[0m` # No Color


# Message de bienvenue
if [ "$UID" = "0" ]; then
  echo -e "${REDP}Your are not logged as r00t on $HOSTNAME.${NC}"
  uname -a
  echo ""
else
  echo -e "${REDP}Welcome $USER. You are logged on $HOSTNAME.${NC}"
  uname -a
  echo ""
fi

# Aliases (variables)
alias home=`cd /home`
alias frz=`cd ~`
alias desktop=`cd /home/frz/Desktop`
alias frzd=`cd /home/frz/Desktop/frz`
alias lsa=`ls -Al` # List all files (+hidden files)


# Functions
function cmds() # savoir mes commandes
{
  echo -e " ${REDP}Liste des commandes:"
  echo -e "${GREENP} -Alias-"
  echo -e "${BLUEP}aliasp: ${NC}Liste les commandes alias persos."
  echo -e " ${GREENP} -Functions-"
  echo -e "${BLUEP}clog: ${NC}Clear les logs."
  echo -e "${BLUEP}info: ${NC}Informations sur la session ouverte et l`ordinateur."
  echo ""
}

function aliasp() #savoir mes alias
{
  echo -e " ${REDP}Liste des aliases persos:"
  echo -e "${BLUEP}home: ${NC}Dossier home (~)."
  echo -e "${BLUEP}frz: ${NC}Dossier frz."
  echo -e "${BLUEP}desktop: ${NC}Dossier Desktop."
  echo -e "${BLUEP}frzd: ${NC}Dossier frz sur le desktop."
  echo -e "${BLUEP}lsa: ${NC}Liste tout les fichiers + fichiers cachés."
  echo ""
}
function colors()
{
  echo -e " ${REDP}Code de couleurs:"
  echo -e "${BLUEF}BLUEF ${NC}<--> ${BLUEP}BLUEP"
  echo -e "${GREENF}GREENF ${NC}<--> ${GREENP}GREENP"
  echo -e "${CYANF}CYANF ${NC}<--> ${CYANP}CYANP"
  echo -e "${REDF}REDF ${NC}<--> ${REDP}REDP"
  echo -e "${PURPLEF}PURPLEF ${NC}<--> ${PURPLEP}PURPLEP"
  echo -e "${BROWNF}BROWNF ${NC}<--> ${BROWNP}BROWNP"
  echo -e "${GRAYF}GRAYF ${NC}<--> ${GRAYP}GRAYP"
  echo -e "${YELLOW}YELLOW"
  echo -e "${WHITE}WHITE"
  echo -e "${NC}NC (NO COLOR)"
  echo ""
}

function info()   # avoir les infos dla session ouverte et pc
{
  echo -e " You are logged on ${BLUEP}$HOSTNAME"
  echo -e " ${REDP}Additionnal information:$NC " ; uname -a
  echo -e " ${REDP}Users logged on:$NC " ; w -h
  echo -e " ${REDP}Current date :$NC " ; date
  echo -e " ${REDP}Machine stats :$NC " ; uptime
  echo -e " ${REDP}Memory stats :$NC " ; free
  my_ip 2>&- ;
  echo -e " ${REDP}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
  echo -e " ${REDP}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"}
  echo ""
}

function clog() # Clear les logs
{
if [ "$UID" = "0" ]; then # 0 = level r00t
 cd /var/log
 cat /dev/null > messages
 cat /dev/null > wtmp
 echo -e "${REDP}Logs cleaned up.${NC}"
 echo ""
else
 echo -e "${REDP}That cmd request a r00t access.${NC}"
 echo ""
fi
}

Option 2

ETC bash (/etc/)
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# by default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ "`id -gn`" = "`id -un`" -a `id -u` -gt 99 ]; then
  umask 002
else
  umask 022
fi

# are we an interactive shell?
if [ "$PS1" ]; then
  case $TERM in
  xterm*)
      PROMPT_COMMAND=`echo -ne "]0;${USER}@${HOSTNAME}: ${PWD}
       ;;
  *)
      ;;
  esac
  [ "$PS1" = "s-v$ " ] && PS1="[u@h W]$ "

   if [ -z "$loginsh" ]; then # We`re not a login shell
  # Not all scripts in profile.d are compatible with other shells
  # TODO: make the scripts compatible or check the running shell by
  # themselves.
  if [ -n "${BASH_VERSION}${KSH_VERSION}${ZSH_VERSION}" ]; then
          for i in /etc/profile.d/*.sh; do
          if [ -x $i ]; then
              . $i
          fi
      done
  fi
  fi
fi

unset loginsh


##################
### bashrc perso ###
##################
# Défini les couleurs
BLACK=`e[0;30m`
BLUEF=`e[0;34m`
BLUEP=`e[1;34m`
GREENF=`e[0;32m`
GREENP=`e[1;32m`
CYANF=`e[0;36m`
CYANP=`e[1;36m`
REDF=`e[0;31m`
REDP=`e[1;31m`
PURPLEF=`e[0;35m`
PURPLEP=`e[1;35m`
BROWNF=`e[0;33m`
BROWNP=`e[1;33m`
GRAYF=`e[0;37m`
GRAYP=`e[1;37m`
YELLOW=`e[1;33m`
WHITE=`e[1;37m`
NC=`e[0m` # No Color

# Message de bienvenue
if [ "$UID" = "0" ]; then
echo -e "${REDP}You are not logged as r00t on $HOSTNAME.${NC}"
echo ""
elif [ "$USER" = "mathieu" ]; then
echo -e "${REDP}Welcome master. You are logged on $HOSTNAME.${NC}"
uname -a
echo ""
else
echo -e "${REDP}Welcome $USER. You are logged on $HOSTNAME.${NC}"
uname -a
echo ""
fi

# Aliases (variables)
alias home=`cd /home`
alias frz=`cd ~`
alias desktop=`cd /home/frz/Desktop`
alias frzd=`cd /home/frz/Desktop/frz`
alias lsa=`ls -Al` # List all files (+hidden files)

# Functions
function cmds() # savoir mes commandes
{
echo -e " ${REDP}Liste des commandes:"
echo -e "${GREENP} -Alias-"
echo -e "${BLUEP}aliasp: ${NC}Liste les commandes alias persos."
echo -e " ${GREENP} -Functions-"
echo -e "${BLUEP}clog: ${NC}Clear les logs."
echo -e "${BLUEP}info: ${NC}Informations sur la session ouverte et l`ordinateur."
echo ""
}

function aliasp() #savoir mes alias
{
echo -e " ${REDP}Liste des aliases persos:"
echo -e "${BLUEP}home: ${NC}Dossier home (~)."
echo -e "${BLUEP}frz: ${NC}Dossier frz."
echo -e "${BLUEP}desktop: ${NC}Dossier Desktop."
echo -e "${BLUEP}frzd: ${NC}Dossier frz sur le desktop."
echo -e "${BLUEP}lsa: ${NC}Liste tout les fichiers + fichiers cachés."
echo ""
}

function colors()
{
echo -e " ${REDP}Code de couleurs:"
echo -e "${BLUEF}BLUEF ${NC}<--> ${BLUEP}BLUEP"
echo -e "${GREENF}GREENF ${NC}<--> ${GREENP}GREENP"
echo -e "${CYANF}CYANF ${NC}<--> ${CYANP}CYANP"
echo -e "${REDF}REDF ${NC}<--> ${REDP}REDP"
echo -e "${PURPLEF}PURPLEF ${NC}<--> ${PURPLEP}PURPLEP"
echo -e "${BROWNF}BROWNF ${NC}<--> ${BROWNP}BROWNP"
echo -e "${GRAYF}GRAYF ${NC}<--> ${GRAYP}GRAYP"
echo -e "${YELLOW}YELLOW"
echo -e "${WHITE}WHITE"
echo -e "${NC}NC (NO COLOR)"
echo ""
}

function info() # avoir les infos dla session ouverte et pc
{
echo -e " You are logged on ${BLUEP}$HOSTNAME"
echo -e " ${REDP}Additionnal information:$NC " ; uname -a
echo -e " ${REDP}Users logged on:$NC " ; w -h
echo -e " ${REDP}Current date :$NC " ; date
echo -e " ${REDP}Machine stats :$NC " ; uptime
echo -e " ${REDP}Memory stats :$NC " ; free
my_ip 2>&- ;
echo -e " ${REDP}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
echo -e " ${REDP}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"}
echo ""
}  

function clog() # Clear les logs
{
if [ "$UID" = "0" ]; then # 0 = level r00t
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo -e "${REDP}Logs cleaned up.${NC}"
echo ""
else
echo -e "${REDP}That cmd request a r00t access.${NC}"
echo ""
fi
}

Retour à la page Expertold

expert/script_bashrc.txt · Dernière modification: 2014/05/09 18:56 (modification externe)