Work the Shell - Understanding Exit Codes
Now that you know how to capture and analyze the exit codes from system commands, what if you want to have the error message be one from your script, not one from the command itself?
That's done with another new shorthand notation: >&, which redirects the stderr/error output stream. Here's how I use that to hide all error messages from the mkdir command being used in our sample scripts:
mkdir /usr >& /dev/null
You also can use &> or 2>&1 instead of >&.
If you don't test the results of the command, of course, you seriously can hose things up, but this makes the output more elegant for sure:
$ ./test.sh mkdir /usr failed: we have an exit code of 0
Hmmm...I'm still getting that false 0. Oh! I haven't added the code to save the exit code value as “error”. One slight tweak later and:
$ ./test.sh mkdir /usr failed: we have an exit code of 1
That's more like it!
I'm going to call this a wrap for this month. Next month, I'll demonstrate how the exit command lets you send exit codes back to the calling program from procedures and functions, just as if they were separate Linux commands rather than part of the same shell script.
Dave Taylor has been hacking shell scripts for a really long time, 30 years. He's the author of the popular Wicked Cool Shell Scripts, and he can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Server Hardening
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- The Humble Hacker?
- New Container Image Standard Promises More Portable Apps
- The Death of RoboVM
- The US Government and Open-Source Software
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- Open-Source Project Secretly Funded by CIA
- Why Python?
- ACI Worldwide's UP Retail Payments
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide