Tag Archives: bash

Being everywhere at once: the MultiSSH tool

I’m going to begin by saying you should never ever do what I’m about to describe, as it’s bad practice, sloppy and dangerous. But then, so is working late on a Friday night, so you might as well choose the lesser of two evils.

As a sysadmin my overriding concern is to do as little work as possible, as quickly as possible. If that’s at all possible. And by work, I mean those repetitive and tedious tasks. There is probably nothing worse than performing the same command, or series of commands, over and over again on multiple hosts. It’s the adult equivalent of writing out lines on a blackboard, and unfortunately this seems to crop up more often than it should.
StateLibQld 1 102016 Interior of Brisbane Technical College Signwriting class, ca. 1900

The best practice way of addressing multiple updates is by implementing some form of configuration management system. This kind of software allows the administrator to centrally manage the files, packages and patches of all their hosts from a single central point. Puppet, Chef, Spacewalk and Satellite are all excellent products that approach this problem from different angles, with each enjoying differing degrees of favour among different factions of the Linux community.
Continue reading

Good bash tricks are hard to find – command within command

Every once in a while I accidentally discover a useful and clever trick and no matter how hard I try, I cannot find it in any sort of documentation. This bash trick is one of my favorites.

Sometimes you want to use a bash command that takes a file as an argument but you don’t actually have a file but another command that creates it or something like that. So what you would normally do is to create a temporary file, use it and delete it afterwards. That’s not so complicated but the extra steps might be annoying and interrupt your flow. What if the command you want to use is part of a one-liner and it would really be a shame to “ctrl-c” the whole thing to create the temporary files, wouldn’t it?

The trick is simple then, all you have to do is put the command that creates the temporary file you want to use inside “<( )” and you’re done. For example, let’s say you have two files, one has 1000 lines and the other has 100 and you want to paste them. Let’s say you don’t want empty spaces on the 900 uncommon lines, so you want the head of the 1000 line file. Now instead of doing:

head -100 1000_line_file.txt > tmpfile
paste tmpfile 100_line_file.txt > pasted.txt
rm tmpfile

You can do it in one line:

paste 100_line_file.txt <(head -100 1000_line_file.txt)

What’s going on being the scenes there is bash actually creates a file descriptor out of the command you put in the parentheses and gives it to the main command (paste, in this example), you can see this happen if you write something like:

ls <(head -100 1000_line_file.txt)

Such a simple yet hardly documented feature, I’m sure remembering this little fellow exists will make your life easier some day.
And you’ll get a result similar to:

/dev/fd/63

Such a lovely trick, yet hardly documented. I’m sure it will make your life easier some day, just try to remember it exists.