Outils pour utilisateurs

Outils du site


linux:exemple_alias

Ceci est une ancienne révision du document !


Alias

Le fichier

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

Partie 2 : architecture switch

Facilite la navigation entre des répertoires qui ont la même structure.
Prenons pour exemple l'architecture de dossiers 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

prod / 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 on fait un cd pour s'y rendre.

winX051 winA052 winA061 rheA060

Petite explications sur la signification de ces codes:

  • win → Windows
  • rhe → Red Hat Enterprise Linux
  • X → architecture x86 (32bit)
  • A → architecture AMD64 (64bit)
  • 051 → version de l'OS

winX051 correspond donc à Windows XP 32bit, winA052 Windows XP 64bit, winA061 Windows 7 64bit, rheA060 Red Hat Enterprise Linux v6.x

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'

on recherche les différentes architectures winX051; winA052, …
si on trouve quelque chose on remplace l'architecture par rheA060
si le répertoire existe on s'y rend
si on a rien trouvé au premier test on se rend directement dans /data/rheA060/prod.

linux/exemple_alias.1503002814.txt.gz · Dernière modification: 2017/08/17 22:46 de pascal