Outils pour utilisateurs

Outils du site


linux:exemple_alias

Ceci est une ancienne révision du document !


Alias

Le fichier

Ces alias sont écris pour être utilisés avec le shell bash.

alias.sh
# navigation
alias ..1='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
alias ..5='cd ../../../../..'
alias ll='ls -l'
alias la='ls -Al'
 
# arch. switch
alias prod='dir=`pwd | sed "s#/test#/prod#"`; if [ -d "$dir" ]; then cd "$dir"; fi'
alias test='dir=`pwd | sed "s#/prod#/test#"`; if [ -d "$dir" ]; then cd "$dir"; fi'
alias winX051='pwd | grep -qe "win[AX][0-9]\{3\}" -e "rheA[0-9]\{3\}"; if [ $? -eq 0 ]; then dir=`pwd | sed "s|/win[AX][0-9]\{3\}|/winX051|;s|/rheA[0-9]\{3\}|/winX051|"`; if [ -d "$dir" ]; then cd "$dir"; fi; else cd /data/winX051/prod; fi'
alias winA052='pwd | grep -qe "win[AX][0-9]\{3\}" -e "rheA[0-9]\{3\}"; if [ $? -eq 0 ]; then dir=`pwd | sed "s|/win[AX][0-9]\{3\}|/winA052|;s|/rheA[0-9]\{3\}|/winA052|"`; if [ -d "$dir" ]; then cd "$dir"; fi; else cd /data/winA052/prod; fi'
alias winA061='pwd | grep -qe "win[AX][0-9]\{3\}" -e "rheA[0-9]\{3\}"; if [ $? -eq 0 ]; then dir=`pwd | sed "s|/win[AX][0-9]\{3\}|/winA061|;s|/rheA[0-9]\{3\}|/winA061|"`; if [ -d "$dir" ]; then cd "$dir"; fi; else cd /data/winA061/prod; fi'
alias rheA060='pwd | grep -qe "win[AX][0-9]\{3\}" -e "rheA[0-9]\{3\}"; if [ $? -eq 0 ]; then dir=`pwd | sed "s|/win[AX][0-9]\{3\}|/rheA060|;s|/rheA[0-9]\{3\}|/rheA060|"`; if [ -d "$dir" ]; then cd "$dir"; fi; else cd /data/rheA060/prod; fi'
 
# snapshot switch (NetApp)
alias snapin='pwd | grep -qe "/\.snapshot/\(hour\|night\|week\)ly\.[0-9]"; if [ $? -ne 0 ]; then s1=`pwd | cut -d/ -f-3`; s3=`pwd | cut -d/ -f4-`; dir="$s1/.snapshot/hourly.0/$s3"; if [ -d "$dir" ]; then cd "$dir"; fi; fi'
alias snapout='pwd | grep -qe "/\.snapshot/\(hour\|night\|week\)ly\.[0-9]/"; if [ $? -eq 0 ]; then dir=`pwd | sed "s#/\.snapshot/\(hour\|night\|week\)ly\.[0-9]##"`; if [ -d "$dir" ]; then cd "$dir"; fi; fi'
alias hourly='pwd | grep -qe "/\.snapshot/\(night\|week\)ly\.[0-9]/"; if [ $? -eq 0 ]; then dir=`pwd | sed "s#/\(night\|week\)ly.[0-9]#/hourly.0#"`; if [ -d "$dir" ]; then cd "$dir"; fi; fi'
alias nightly='pwd | grep -qe "/\.snapshot/\(hour\|week\)ly\.[0-9]/"; if [ $? -eq 0 ]; then dir=`pwd | sed "s#/\(hour\|week\)ly.[0-9]#/nightly.0#"`; if [ -d "$dir" ]; then cd "$dir"; fi; fi'
alias weekly='pwd | grep -qe "/\.snapshot/\(hour\|night\)ly\.[0-9]/"; if [ $? -eq 0 ]; then dir=`pwd | sed "s#/\(hour\|night\)ly.[0-9]#/weekly.0#"`; if [ -d "$dir" ]; then cd "$dir"; fi; fi'
alias snap+='[[ `pwd` =~ (.*\.snapshot/(hour|night|week)ly\.)([0-9])(.*$) ]] && dir="${BASH_REMATCH[1]}$((BASH_REMATCH[3] + 1))${BASH_REMATCH[4]}"; if [ -d "$dir" ]; then cd "$dir"; fi'
alias snap-='[[ `pwd` =~ (.*\.snapshot/(hour|night|week)ly\.)([0-9])(.*$) ]] && dir="${BASH_REMATCH[1]}$((BASH_REMATCH[3] - 1))${BASH_REMATCH[4]}"; if [ -d "$dir" ]; then cd "$dir"; fi'
 
# add home to path
echo $PATH | grep -q "<my_login>";
if [ $? -ge 1 ];
then
	PATH="${PATH}:/home/<my_login>";
fi
 
# custom prompt
export PS1="\n\w\n\u@\h \$ "

Explications du code

Partie 1 : navigation

Cette partie est assez basique et ne nécessite pas d'explications particulières.

Partie 2 : arch. switch

Petite explications sur la signification des codes :

win A 061
OS architecture version
code signification
winX051 Windows XP 32bit
winA052 Windows XP 64bit
winA061 Windows 7 64bit
rheA060 Red Hat Enterprise Linux 6.0 64bit

Ces alias facilitent la navigation entre des répertoires qui ont la même structure.
Prenons pour exemple l'arborescence suivante :

   +- data
     +- winA052
     | +- prod
     | | +- soft1
     | | | +- V12
     | | +- soft3
     | |   +- V3
     | |   +- V4
     | +- test
     |   +- soft1
     |     +- V13
     +- winA061
     | +- prod
     | | +- soft2
     | |   +- V1
     | +- test
     |   +- soft2
     |     +- V2
     +- winX051
     | +- prod
     |   +-soft1
     |     +-V12
     +- rheA060
       +- prod
       | +- soft2
       |   +- V1
       +- test
         +- soft2
           +- V2

ALIAS prod, test

Agissent comme une bascule : prod permet de passer de test vers prod et inversement pour test.
Pour naviguer de /data/winA052/prod/soft1 vers /data/winA052/test/soft1 la commande cd ../../test/soft1 ici est remplacée par l'alias test.

alias test='
dir=`pwd | sed "s#/prod#/test#"`;
if [ -d "$dir" ];
then
  cd "$dir";
fi'

sed remplace “prod” par “test” dans le répertoire courant (pwd)
si ce répertoire existe alors on fait un cd pour s'y rendre.

ALIAS winX051, winA052, winA061, rheA060

Si on veut passer de /data/winA061/prod/soft2/V1 vers /data/rheA060/prod/soft2/V1 l'alias rheA060 remplacera cd ../../../../rheA060/prod/soft2/V1.

alias rheA060='
pwd | grep -qe "win[AX][0-9]\{3\}" -e "rheA[0-9]\{3\}";
if [ $? -eq 0 ];
then
  dir=`pwd | sed "s|/win[AX][0-9]\{3\}|/rheA060|; s|/rheA[0-9]\{3\}|/rheA060|"`;
  if [ -d "$dir" ];
  then
    cd "$dir";
  fi;
else
  cd /data/rheA060/prod;
fi'

grep recherche le code de l'architecture (winA061, …) dans le répertoire courant (pwd)
si grep a trouvé quelque chose alors sed remplace le code d'architecture par rheA060
si le répertoire existe on s'y rend avec cd
si rien a été trouvé par grep au premier test on se rend directement dans /data/rheA060/prod.

linux/exemple_alias.1503088150.txt.gz · Dernière modification: 2017/08/18 22:29 de pascal