Displaying the Exit status of a UNIX or Linux command

Easy Linux Commands: Working Examples of Linux Command SyntaxFrom my upcoming book Easy Linux Commands:

Whenever a command or shell script completes successfully, it sets a hidden status code of zero. If the command is unsuccessful, it sets a nonzero hidden status code. This completion status code is known as the exit status. The exit status of the last command or script that was run is contained in the special shell variable, $?.

Most of the time we never look at this value and instead check to see if the command did what we want or look for errors in the output of commands. In a shell script, however, we may want to check the exit status to make sure everything is going OK. The exit status of the last command can be displayed as follows:

$ ls
example1.fil example2.xxx examples test.bsh umask_example.fil
$ echo $?
$ ls *.txt
ls: *.txt: No such file or directory
$ echo $?

The value of the exit code can then be used in a conditional statement or be transferred to another variable.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, linux command, exit status

Easy Linux Commands – Working Examples of Linux Command Syntax

Easy Linux Commands: Working Examples of Linux Command SyntaxI’m happy to announce that my book Easy Linux Commands: Working Examples of Linux Command Syntax can now be preordered from Rampant TechPress!

This book by Terry Clark and I is designed to serve as both a progressive how-to for getting comfortable with the Linux environment and as a reference for the most commonly used Linux commands and options. After presenting a little background information the books jumps right in to how to navigate the Linux file structure, manipulate files and directories, search for specific objects and get around in the vi text editor.

More advanced topics like shell scripting, using cron to schedule tasks and monitoring and administration tasks are also covered all with the focus on giving working examples of the Linux commands you will find useful. The command examples in this book can often be used with little or no modification saving considerable time and experimentation.

This book organized into logical task-based chapters making it easy to find the commands you need when you need them.

For a full table of contents and index check out the book’s page on Rampant TechPress. It’s available for preorder through Rampant and should be in book stores in just a couple months!

linux, linux commands

Synergy – Cross Platform Mouse and Keyboard Sharing

SynergyTim Haroutunian recently discovered the open-source, cross-platform utility Synergy which allows you to seamlessly share a keyboard and mouse with several computers of varying platforms.

The behavior of Synergy is similar to having multiple monitors on a single computer with the added advantage that you’re controlling multiple systems. Since I have both a Mac OSX desktop and a Windows laptop on my desk it was nice to reduce myself to one keyboard and mouse! Even clipboard data is shared between the systems.

Setup was a little less than intuitive but well worth it. Out of the box, security is fairly weak, however the Synergy project page does have instructions on how to tunnel Synergy traffic through SSH. Mac, Windows and Linux binaries are available along with the source code for those DIYers.

kvm, cross-platform, software, systems administration, linux, OSX

SSH Without A Password

Zach has posted a good quick reference for setting up SSH to use a shared key for authentication instead of a password on a UNIX system. It’s important to keep your keys secure, but this can allow you to set up scripts to execute commands or move files between multiple hosts without prompting for passwords.

If memory serves this type of authentication is enabled by default on most ssh servers, but if it doesn’t work talk to your sys-admin to see if it is disabled.

unix, linux, solaris, mac osx, osx, ssh, security

UNIX Find And Execute

Zach tells me I need to post how to find files and execute a command on those files. While the man pages are always the definitive reference, these are the options I use frequently. As usual, anything in italics should be replaced with the details for your search.

Note: WordPress loves to convert quotes into fancy quotes (pointing in from each side) so these commands may not work too well if copied and pasted. On the bright side, there’s no time like the present to start commiting these to muscle memory.

Find a file and execute a specific command:

find path conditions -exec command {} \;

find ./ -name "*.php" -exec cat {} \;

The open/close curly bracket marks where the name of each file found will be substituted and the backslash-semicolon marks the end of the command to execute.

Here are a few other options I use frequently:

Search for a file by name:

find path -name filename

find ./ -name style.css

Search by name with wildcards:

find path -name "filename"

find ./ -name "*.html"

Search for files modified within the past n days:

find path -mtime -n

find ./ -mtime -7

Search for files modified before the past n days:

find path -mtime +n

find /tmp -mtime +3

These are mostly based on my experience in Solaris using the bash shell. They should work just about everywhere, but check the man pages as mileage will vary.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

unix, solaris, linux, mac osx, osx, sysadmin, systems administration