dimanche 24 février 2013

Astuce: personnaliser son prompt Linux et SQL Plus

Lorsqu'on travaille sur les bases Oracle nous avons généralement plusieurs terminaux Linux ouverts et connectés sur différentes bases. On peut avoir ainsi à la fois des fenêtres connectées sur des bases de PROD et d'autres sur des bases DEV.

Pour éviter la confusion entre ces différentes connexions j'aime modifier mon prompt pour y afficher notamment le nom de l'instance sur laquelle pointe mon $ORACLE_SID. En ouvrant mon terminal je suis sûr de savoir sur quelle base je pointe.

Pour pouvoir modifier l'affichage du prompt il suffit de modifier la variable système $PS1 en ajoutant des variables BASH et/ou des variables d'environnement telles que $ORACLE_SID.

Personnellement je définie ma variable $PS1 de la manière suivante:
PS1='[\u@\W $ORACLE_SID]$ '

J'obtiens ainsi le prompt suivant:
[oracle@oradata orcl1]$ 

La variable BASH \u indique le username Linux, la variable \W indique le répertoire courant. Si je me connecte en SYSDBA je sais d'avance que je pointerai sur l'instance ORCL1.

Bien sûr il est préférable de définir cette variable dans votre fichier .bash_profile pour que votre prompt soit défini automatiquement.

Vous pouvez personnaliser votre prompt à votre guise en choisissant parmi les variables BASH dont la liste se trouve dans le manuel BASH (man bash):
PROMPTING
       When executing interactively, bash displays the primary prompt PS1 when it
       is ready to read a command, and the secondary prompt  PS2  when  it  needs
       more  input to complete a command.  Bash allows these prompt strings to be
       customized by inserting a number of backslash-escaped  special  characters
       that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
              \D{format}
                     the format is  passed  to  strftime(3)  and  the  result  is
                     inserted  into the prompt string; an empty format results in
                     a  locale-specific  time  representation.   The  braces  are
                     required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first ‘.’
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell’s terminal device name
              \n     newline
              \r     carriage return
              \s     the  name of the shell, the basename of $0 (the portion fol-
                     lowing the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the current working directory, with $HOME abbreviated with a
                     tilde (uses the value of the PROMPT_DIRTRIM variable)
              \W     the  basename  of  the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
          \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin a sequence of non-printing characters, which could  be
                     used to embed a terminal control sequence into the prompt
              \]     end a sequence of non-printing characters
En plus de personnaliser le prompt du terminal linux vous pouvez modifier le prompt SQL Plus en affichant par exemple le nom de l'instance.
Pour ce faire il suffit de modifier la variable SQL Plus sqlprompt de la manière suivante:
SQL> SET sqlprompt &_CONNECT_IDENTIFIER>
orcl1>
En ajoutant cette commande dans le fichier glogin.sql (qui se trouve dans $ORACLE_HOME/sqlplus/admin) la personnalisation du prompt se fera automatiquement à chaque connexion SQL Plus.

Aucun commentaire:

Enregistrer un commentaire