Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C++ System Programming Cookbook

You're reading from   C++ System Programming Cookbook Practical recipes for Linux system-level programming using the latest C++ features

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781838646554
Length 292 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Onorato Vaticone Onorato Vaticone
Author Profile Icon Onorato Vaticone
Onorato Vaticone
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with System Programming 2. Revisiting C++ FREE CHAPTER 3. Dealing with Processes and Threads 4. Deep Dive into Memory Management 5. Using Mutexes, Semaphores, and Condition Variables 6. Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory 7. Network Programming 8. Dealing with Console I/O and Files 9. Dealing with Time Interfaces 10. Managing Signals 11. Scheduling 12. Other Books You May Enjoy

Handling a Linux bash error

We've seen that one way to interact with the Linux kernel is through the shell, by invoking commands. A command can fail, as we can imagine, and a way to communicate a failure is to return a non-negative integer value. 0, in most cases, means success. This recipe will show you how to deal with error handling on the shell.

How to do it...

This section will show you how to get errors directly from the shell and via a script, which is a fundamental aspect of script development:

  1. First, run the following command:
root@e9ebbdbe3899:/# cp file file2
cp: cannot stat 'file': No such file or directory
root@e9ebbdbe3899:/# echo $?
1
  1. Create a new file called first_script.sh and type in the following code:
#!/bin/bash
cat does_not_exists.txt
if [ $? -eq 0 ]
then
echo "All good, does_not_exist.txt exists!"
exit 0
else
echo "does_not_exist.txt really DOES NOT exists!!" >&2
exit 11
fi
  1. Save the file, and exit (:wq or :x).
  2. Give execution permission (the x flag) to the current user for the first_script.sh file:
root@e9ebbdbe3899:~# chmod u+x first_script.sh

These steps are detailed in the next section.

How it works...

In step 1, the cp command failed, as file and file2 don't exist. By querying echo $?, we get the error code; in this case, it is 1. This is particularly useful when writing bash scripts where we might need to check for a particular condition.

In step 2, the script just lists the does_not_exist.txt file and reads the error code returned. If all goes fine, it prints an acknowledgment message and returns 0. Otherwise, it returns the error code 11.

By running the script, we get the output as follows:

Here, we notice a couple of things:

  • We logged our error string.
  • The error code is the one we had set in the script.

Under the hood, every time a command is invoked, it enters into kernel space. The command is executed, and a return status is sent back to the user in the form of an integer. It's really important to consider this return status, as we might have a command that apparently succeeded (no output) but eventually failed (returns code different from 0).

There's more...

One important aspect of the return status of the commands is that it can be used to (conditionally) run the next command. Two important operators are used for this purpose: && (AND) and || (OR).

In the two commands here, the second is run if—and only if—the first succeeds (the && operator). file.txt is removed if it is copied to the project folder:

cp file.txt ~/projects && rm -f file.txt

Let's have a look at a second example:

cp file.txt ~/projects || echo 'copy failed!'

In the preceding example, the second command is run only if the first fails (the || operator). copy failed! is printed if the copy fails.

In this recipe, we just showed that commands can be combined on a shell script to create a more complex command, and by controlling the error code, we can control the flow of execution. Man pages are a great resource as they contain all the commands and error codes (for example, man cp and man cat).

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime