How to check exit status of a command line in BASH ?
$? Operator gives the exit status code
To check the exit status in bash, you can user the specific operator $?, for example here, we check if there is an ldaps server(not ldap but ldaps !!!)
root@kali:~# cat /etc/ldap/ldap.conf
BASE dc=example,dc=com
URI ldap://ldap.example.com ldap://ldap-master.example.com:666
TLS_CACERT /etc/ssl/certs/ca-certificates.crt
root@kali:~# grep ldaps /etc/ldap/ldap.conf
Next we define, the exit STATUS using operator $?:
root@kali:~# STATUS="$?"
root@kali:~# [ "$STATUS" -eq 0 ] && echo "STATUS OK"
root@kali:~# [ "$STATUS" -ne 0 ] && echo "STATUS KO"
STATUS KO
STATUS equal to 0 means a success (failure if different to 0). In our case, string ldaps is not in {ldap.conf}, it is a failure.
If you found this post or this website helpful and would like to support our work, please consider making a donation. Thank you!
Help UsArticles in the same category
- Trouver rapidement et facilement l'adresse mac courante sous Linux
- How to Get or Extract filename and extension in bash
- How to check exit status of a command line in BASH ?
- Fast and easy way to get mac address on Linux
- Error "standard in must be a tty" when doing ssh username@server 'sudo command'
- Disable the usage of history using HISTSIZE,HISTFILE
- Désactiver l'historique en utilisant HISTSIZE,HISTFILE
- Comment vérifier le statut d'exécution d'une commande en BASH ?
- Comment extraire le nom et l'extension d'un fichier en Bash
- Back to previous directory in shell cd,$OLDPWD
- Linux Bash