Pour vérifier si une commande a fonctionné avec succès ou non, vous pouvez vérifier l' état de retour , donné par $?, de la commande précédente avec:
echo $?
Un état de retour 0signifie que la commande s'est terminée avec succès, tandis qu'une sortie non nulle ( code d'erreur ) signifierait que certains problèmes ont été rencontrés ou qu'il y a une erreur et que la catégorie peut être connue à partir du code d'erreur. Les codes d'erreur Linux / C sont définis dans /usr/include/asm-generic/errno-base.het /usr/include/asm-generic/errno.h.
Toujours en bash, le .bashrcdéfinit un alias alertqui peut être utilisé pour notifier avec le statut d'achèvement. Vous devez attacher l'alias avec la commande ou le combo de commandes comme ceci:
some_command --some-switch; alert
Vous pouvez ajouter la ligne de code suivante à votre ~/.bashrcfichier pour afficher l' état de retour de la dernière commande exécutée.
# show the return code of last command executed
PS1='${debian_chroot:+($debian_chroot)}\u@\h(lst ret. $(echo $?) ):\w\$ '
(ouvrez le fichier ~/.bashrcavec l'éditeur de texte de votre choix, et copiez la ligne ci-dessus, collez-la dans le fichier et enregistrez. Lancez une nouvelle instance du terminal, et vous devriez l'avoir en action. Ou à la place, vous pouvez définir une fonction et utiliser avec PS1comme comme illustré ci-dessous.)
une petite démo:
hash@precise(lst ret. 0 ):~$ ls -sh someFileThatsNotThere
ls: cannot access someFileThatsNotThere: No such file or directory
hash@precise(lst ret. 2 ):~$
hash@precise(lst ret. 2 ):~$ aCommandThatsNot
aCommandThatsNot: command not found
hash@precise(lst ret. 127 ):~$
hash@precise(lst ret. 127 ):~$ echo "you should get a lst ret. 0, I believe the system has echo installed :)"
you should get a lst ret. 0, I believe the system has echo installed :)
hash@precise(lst ret. 0 ):~$
hash@precise(lst ret. 0 ):~$ sudo touch /tmp/someTestFile
[sudo] password for hash:
hash@precise(lst ret. 1 ):~$
hash@precise(lst ret. 1 ):~$ chown $USER:$USER /tmp/someTestFile
chown: changing ownership of `/tmp/someTestFile': Operation not permitted
Je joue juste avec PS1:) ..un peu plus,
function showRetStat {
## line1: initiliazing retStat with the return status of the previous command
retStat=$?
## line2: Left padding the return status with spaces. If you prefer the unpadded one, you can just replace
# $retStatFtd in the lines initializing noErrStr and errStr among other possible ways.
retStatFtd=$(sed -e :a -e 's/^.\{1,2\}$/ &/;ta' <<< $retStat)
## lines3&4: Setting the strings to display for a successful and unsuccessful run of previous command
# which we are going to display with the prompt string. Change the strings to display text of your
# choice like you may set noErrStr="yippie!" , errStr="oopsie!" in place of what they're now.
noErrStr="retStat "$retStatFtd" :: PASS ^_^"
errStr="retStat "$retStatFtd" :: FAIL x_x"
## line5: Applying the logic and display the proper string at the prompt. Space padded number i.e. retStatFtd, here,
# worked in the logic, originally I intended to use this for the display while retStat in the conditional
# check; you could make the function one statement less if you want to.
echo "$([ $retStatFtd = 0 ] && echo "$noErrStr" || echo "$errStr")"
}
## Combining the function showRetStat into the prompt string.
PS1='${debian_chroot:+($debian_chroot)}\u@\h($(showRetStat)):\w\$ '
(vous pouvez modifier la fonction pour la rendre plus sophistiquée, comme le fait @gronostaj dans son article.)