Thursday, January 28, 2010

Changing Gnome panel clock from 24hr time to 12hr time on OpenSUSE 11.2

Fire up your shell, and type in gconf-editor.  Then, on the left, select Apps --> panel --> applets --> applet0 --> prefs (This was slightly different for me between two different machines.  It might be ...applets --> clock_screen0 --> prefs.)  First you want to right click on 'format' on the right side of the gconf-editor, and select 'Edit Key'.  In the dialogue box that comes up, enter 'custom' (without the quotes).  Then click ok.  Next, you will need to enter 'custom_format' (about 6 entries above 'format').  Again, right click and select 'Edit Key'.  Here you will enter <b>%a %b %d, %l:M %p</b> and click ok.  This will get you a format such as Thu Jan 28, 12:20.  If you prefer a different format, look at some of the options available in the date man page.

Read more here.  (Don't forget to look at the comments section at the bottom as well).

Monday, January 18, 2010

Shell shortcuts part deux.

In my last post, I demonstrated how you can use the !! and !$ features of your Bash & zsh history to get your work done more quickly.   I use the former all the time, but  the latter?  Well, not so much.  This is because I use something even better for interactive shell use.  It is M-.  That is Meta plus period.  All you Emacs zealots users will know what I mean by Meta.  :)  The Meta-key is Alt on your computer, or sometimes Esc.  Both work on my machine, but I prefer Alt over Esc, because it is less work.

Try doing M-. in your shell (Press and hold the Alt key, and then press the period key).  Like !$, M-. will give you the last part of the last command entered, but this time you can see it immediately.  Better still, you can cycle through all of your commands.  Just keep hitting M-. and the shell will display the last part of each command, in order, from newest to oldest.

Note:   If you use the ESC key for your 'Meta' key, then you need to release the ESC key each time.  Otherwise, if you hold it down and tap the . (period) key, it will return the end of the last command only once, and then start echoing the period after it.

As an example, say you had just finished a

grep fred /etc/passwd

You are ready to use the command line history feature:

vi M-.

Which would expand to (immediately):

vi /etc/passwd

Then, if it looks right, just press Enter.

Saturday, January 16, 2010

Shell shortcuts

In Bash and zsh, you can access the command history by using several shortcuts.  For example, by typing !![Enter], the previous command will be executed.  If you are not sure of what the previous command was, you could do !!:p[Enter] and the previous command will be displayed, but not executed.  Now, if you like what you see, you can do !![Enter], and the shell will execute that command.

Another ! shortcut is !$.   This will give you the end of the previous command.  So if my previous command was:

grep fred /etc/passwd

doing !$ would give me /etc/passwd.  This can be handy.  Say for example the user fred exists on the system, and grep returns his record from /etc/passwd.  If there is something I see in one of the fields that I wish to change, all I need to do (provided that grep was the last command I did) is:

vi !$

This will expand to

vi /etc/passwd

If you are not sure how !$ will expand for you (ie., you cannot remember for certain what your last command was exactly), you can use the :p option:

vi !$:p

Friday, January 15, 2010

Pagers

I won't give you a history of documentation viewers (aka pagers) on *nix systems, other than to say that there used to be pg and more. Then along came less, which was a lot like more, but way better.  Apparently there is a play on words there, so maybe it is true that less is more.

Anyway, I have long followed the advice to use the less pager where it is available.  I don't follow that advice strictly any more.  I now use most. From the most home page:

"MOST is a powerful paging program for Unix, VMS, MSDOS, and win32 systems. Unlike other well-known paging programs most supports multiple windows and can scroll left and right. Why settle for less?"

Indeed.

Thursday, January 14, 2010

Making your Bash scripts look a little better

One thing I have noticed about this blog is that shell scripts are _very_ difficult to make look nice.  I am not sure if that is a Blogger problem, or an issue with this template.  Either way, I don't like mucking around with the way things look.  I want to get my ideas down and not have to be constantly tweaking the text formatting.

Anyway, after monkeying around for a while with the shell script on the previous post, I decided to search for a script beautifier, along the lines of GNU indent, but for shell scripts.  Eventually, I came across a ruby script, written by Paul Lutus, and aptly named Bash Script Beautifier.  So far, it has worked very well for me.

As always, utilities such as this one may not work exactly the way you want.  You would do well to make a backup copy of your script before you run it through the beautifier.  Also, be sure to read the documentation on the download page.  You can read all about it, and download the script here.

Tuesday, January 12, 2010

Optimizing your shell scripts

A few days ago I came across a couple of posts, by Brock Noland, (blog site is dead now) about splitting strings natively with the shell.  You can find them here here (Internet archive) and here here. Basically, the writer demonstrated that using shell builtins and variables is much more desirable than using outside programs, such as cut and awk for the same purpose.  The reason for this is due to speed.  Every time you make a call to an outside program with the shell, you fork a new process, and that takes time.

I decided to tinker with this idea a bit more, to get a better impression of how much slower using cut and awk really is. I made each of the following functions iterate 100 times.  I am pretty sure some of you will be shocked by the speed difference between cut and awk, versus the shell native string splitting.