Para verificar si algún comando funcionó correctamente o no, puede verificar el estado de retorno , dado por $?, del comando anterior con:
echo $?
Un estado de retorno de 0significa que el comando se completó con éxito, mientras que una salida distinta de cero ( código de error ) significaría que se encontraron algunos problemas o hay un error y la categoría se puede conocer a partir del código de error. Los códigos de error de Linux / C se definen en /usr/include/asm-generic/errno-base.hy /usr/include/asm-generic/errno.h.
También en bash, .bashrcdefine un alias alertque se puede usar para notificar con el estado de finalización. Tendría que adjuntar el alias con el comando o combo de comandos como este:
some_command --some-switch; alert
Puede agregar la siguiente línea de código a su ~/.bashrcarchivo para mostrar el estado de retorno del último comando ejecutado.
# show the return code of last command executed
PS1='${debian_chroot:+($debian_chroot)}\u@\h(lst ret. $(echo $?) ):\w\$ '
(abra el archivo ~/.bashrccon el editor de texto de su elección, y copie la línea anterior, péguelo en el archivo y guárdelo. Inicie una nueva instancia del terminal, y debería tenerlo en acción. O en su lugar podría definir alguna función y usar con lo PS1que se ilustra a continuación.)
una pequeña demostración:
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
Solo jugando con PS1:) ..un poco más,
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\$ '
(puede modificar la función para que sea más elegante, algo como lo hace @gronostaj en su publicación).