Shell Command Substitution

| No Comments | Bookmark and Share

The other day I wanted to diff a pair of files on two different hosts, stg1 and stg2. Normally, I would do so by copying on of the files to the other host, or grabbing them both onto my workstation with scp; "there must be a better way," I thought.

Enter command substitution, a process by which you use the output from an executed command as the input to another. Many people have used this in a simple manner such as ls /usr/src/linux-`uname -r`which takes the output of uname -r, namely the kernel release you're running, and uses that to flesh out the ls command. There is a more complicated form, however, that I use for diffing remote files. In the aforementioned example, I used the following command to diff the files:

vimdiff <(ssh stg1 cat /etc/rc.d/init.d/httpd) <(ssh stg2 cat /etc/rc.d/init.d/httpd)

Much like a subselect in MySQL, the cat commands are executed on the remote machine and then piped to vimdiff allowing for single command, no-file-copying diffs.

Leave a comment

About this Entry

This page contains a single entry by Drew Stephens published on September 9, 2008 5:08 PM.

Speeding up SSH Logins in Ubuntu was the previous entry in this blog.

Rick Ross' Financial Success is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 5.1