Linux exit codes

To get the exit code of a command type echo $? at the command prompt. In the following example a file is printed to the terminal using the cat command.

cat file.txt 
hello world 
echo $? 

The command was successful. The file exists and there are no errors in reading the file or writing it to the terminal. The exit code is therefor 0.

cat doesnotexist.txt 
cat: doesnotexist.txt: No such file or directory 
echo $? 

The exit code is 1 as the operation was not successful.

To use exit codes in scripts an “if” statement can be used to see if an operation was successful.

cat file.txt 

if [ $? -eq 0 ] 
 echo "The script ran ok" 
 exit 0 
 echo "The script failed" >&2 
 exit 1 

To set an exit code in a script use “exit0” where “0” is the number you want to return. In the following example a shell script exits with a “1”. This file is saved as “”.

exit 1
echo $? 

Get UUID of Hard Disks on Debian

1st option
# ls -l /dev/disk/by-uuid/

lrwxrwxrwx 1 root root 15 Nov 7 13:00 9c18302c-9aa2-4f55-b2c8-03a9ae68b2a2 -> ../../nvme1n1p1
lrwxrwxrwx 1 root root 15 Nov 7 13:00 b524f8c0-90e7-4fc7-a842-6cb2380086c8 -> ../../nvme0n1p1

2nd option

# blkid /dev/nvme1n1p1

/dev/nvme1n1p1: UUID="9c18302c-9aa2-4f55-b2c8-03a9ae68b2a2" TYPE="ext4" PARTUUID="1e534c5e-e4c8-424c-822b-117e47d72263"