Saturday, 13 October 2012

Bashing the shell

As ever, I continue to climb learning curve, and am happy to report that the vertigo and nose-bleeds have lessened, although the view from up here is excellent :-)

So today's new challenge ? To make more progress with the history feature of the Bourne Again Shell ( bash ) on various Linux distributions, including Red Hat Enterprise Linux (RHEL) and Ubuntu.

The history command is awesome - it keeps a track of all the commands that one types whilst using the shell, and I find it an excellent way of keeping track of what I've been up to, especially when writing documentation ( and blog posts ! ).

In the past, I've always used a command such as: -

$ history | grep awk

or: -

$ history | grep -i startserver

which is useful, but can often give me a huge great listing of commands, still requiring me to use copy/paste to get the command back into a state where I can re-submit it.

So, today, I Google'd and, as ever, found the answer in a matter of picoseconds.

This article: -

Using Bash's History Effectively

gave me the solution: -

...
Before you begin typing your command, type ctrl-r. This will put you into history search mode (actually, reverse incremental history search mode).

Now when you begin typing, the most recent command matching what you've typed so far will appear on the line with a cursor at the start of the match. (Try playing around with this feature; there are a few interesting behaviors in there.)

When you've found what you're looking for, you have a couple of options. Just pressing Enter will immediately recall and execute the command. ctrl-j or Escape will retrieve the command, but allow you to continue editing. If you can't find what you're looking for or if you just change your mind, hit ctrl-g or ctrl-c to cancel.
...

Lo and behold, I can hit [Ctrl][R] whilst within Bash and then start to type the first few characters of the command for which I'm looking e.g. apt which gives me this: -

(reverse-i-search)`ap': sudo apt-get install java6

If I then go ahead and press [Enter], my command - sudo apt-get install java6 is executed.

The other thing that this article showed me is that, if I'm looking at a whole list of historical commands: -

...
1264  sudo umount /mnt
1265  sudo palimpsest
1266  exit
1267  cd /media/DaveHay/Software/WAS7Fixes/
1268  ls | grep 21
1269  ls -al | grep 21
1270  chmod 777 7.0.0-WS-WAS-LinuxX64-FP0000021.pak
1271  sudo chmod 777 7.0.0-WS-WAS-LinuxX64-FP0000021.pak
1272  exit
1273  cd /media/DaveHay/Software/WAS7Fixes/
1274  ls * | grep 21
1275  exit
1276  history | grep apt
1277  sudo apt-get install java6
1278  clear
1279  history
...

I can simply type: -

!1265

and press [Enter]  to execute the command ( sudo palimpsest ) to launch Disk Utility as root.

Nice, eh ?

No comments:

Note to self - use kubectl to query images in a pod or deployment

In both cases, we use JSON ... For a deployment, we can do this: - kubectl get deployment foobar --namespace snafu --output jsonpath="{...