Monday, June 15, 2009

I finally learn how to do realpath

Kevin Fenzi said I should send my shell-level realpath off to the maintainer of an appropriate package -- coreutils, maybe -- for possible inclusion.

So, I wrote unit tests, a simple Makefile and documentation: man page, README, INSTALL, stuff like that.

Kevin, wiser than I, looked and found documentation that suggested readlink -f would do the trick. And it does. I could prove it to myself because I had unit tests.

One difference in functionality, which I can live with: if the file is missing, readlink -f succeeds and reports it's in the current directory.

So, to find the path to an executable, this does the trick:
path2 () {
local _cmd=$(type -p $1)
[ -f $_cmd ] && readlink -f $_cmd
}

Tuesday, June 9, 2009

iPods and Banshee

Scott Mann posted a note to the Boulder Linux Users' Group Mailing list, asking for advice about iPods, MP3 players, and what software to use to manage them under Linux.

As you'd expect, much of the advice was hyperbolic. "Don't get an iPod. They're unuseable." Quick, someone tell Steve Jobs.

I replied with my recent experience.

A few months ago, I switched my girlfriend, Kristina, over to Linux; her old, G3, iBook was finally biting the dust. She's been completely happy with Ubuntu.

Last week, she bought an iPod Nano on eBay.

She's going to fill it with
her Dad's favorite music and give it to him.

When it arrived, she took it out of the box, turned to me, and said, "How do I put music on this?"

I took Scott Mann's advice and installed Banshee. It mostly just worked. It rips CDs to ogg by default, but I kept throwing gstreamer packages at it until it said it knew how to create mp3s, too.

To my mild surprise, it now also converts flac, ogg, wmv, and wv files to mp3s, on the fly, when I drag them onto the iPod. I found this out by accident. I mean, uh, by carefully reading all the documentation.

Still, after reviewing this thread, I started to wonder whether, instead, I should have traded Kristina in for a smarter girlfriend -- one who'd have bought an mp3 player with a much better interface and then re-flashed it with Linux or RockBox herself.

When I asked her if she thought so, too, she ignored the question.

Maybe she's smart enough.

Tuesday, June 2, 2009

What's the Absolute Path to a File?

There's no standard command that gives you the absolute path to a file. That's insane. Here's an implementation.

Yes, it resolves symlinks and works on directories, too.

I couldn't figure out how to get Blogger to stop re-formatting my code, so I passed it through this, web-based, C#-formatting tool (!), and now it seems to look okay.

Argh.

// realpath - what's the real path to a file?
// Turn relative paths into absolute paths,
// resolve '.' and '..',
// follow symlinks

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
char path[_POSIX_PATH_MAX];

if (argc != 2) {
fprintf(stderr, "usage: %s filename\n", argv[0]);
exit(EINVAL);
}

if (!realpath(argv[1], path)) {
perror(NULL);
exit(errno);
}

printf("%s\n", path);
return(0);
}

Friday, May 29, 2009

Google Runs Rings Around M$ This Week

Google seems to be in fine form this week.
  • Wednesday, they gave away Android phones to everyone at the Google I/O conference.
  • Yesterday, they announced Google Wave.
  • Today they have up the full video of the Google Wave talk. And the product will be Open Source.
And Microsoft? Their big announcement, yesterday, was bing, a new search engine to compete with Google. Which, at this point, even if it eventually works and comes out, is soooo 20th-century. Sorry.

It'll be interesting to see how Google ties this to ads.

Thursday, May 28, 2009

Editing Parallel Trees

Sometimes, I need to work in parallel trees -- trees with the same structure, but some important difference (different revision numbers, different compiler flags, ...). When I'm deep inside one tree, getting over to the homologous place in the other can be painful. Not only is it a lot of typing, but I'm often made aware that I should work at "Typos R Us."

The string substitution syntax ${FOO/bar/mumble}, which returns $FOO, but with mumble substituted for bar, solves my problem. For example, if I'm in the tree /home/jsh/tree1/a/b/c/d, and I want to pop over to /home/jsh/tree2/a/b/c/d, I just say

$ pushd ${PWD/tree1/tree2}
or even

$ cd ${PWD/1/2}
Presto! I'm there faster than you can say, "xyzzy." Or, at least as fast.

Not only am I in the new tree, but now pushd, with no arguments, (or cd - ) takes me back and forth.

Wednesday, May 27, 2009

How to Change File Extensions

Changing file extensions is easier than people make it out to be. Even on sites that provide shell programming tips, I see recipes like this:
#!/bin/sh

for file in *.dat ;

do mv $file `echo $file | sed 's/\(.*\.\)dat/\1txt/'` ;

done
It's easier than that. Honest.
$ for file in *.dat; do mv $file ${file/.dat/.txt}; done
All in the same, parent shell. One process. No pipes, no sed, no magic regex metacharacters. So easy, you can type it on the command line.

I do it all the time.

If I need to be careful, I start like this:
      $ for file in *.dat; do echo mv $file ${file/.dat/.txt}; done
Then, if the output commands look like what I intended, I recall the loop, and tack a pipe of its output to bash on the end, like this:
      $ for file in *.dat; do echo mv $file ${file/.dat/.txt}; done | bash

Tuesday, May 12, 2009

Shell Programming Texts

There are no brief but substantial shell-programming texts.

There are two, alternative models for programming textbooks: K&R, and almost any Java book you pick up. The Java books are boat anchors. They're comprehensive, verbose, detailed, and replete with extensive terminology. K&R is brief, to-the-point.

Shell-programming books seem to follow the Java model. What would a K&R-style shell book look like?

I can't write as well as Brian Kernighan or Dennis Ritchie, but let's take a shot at a few pieces.

Beginnings.

Unlike languages like Java or C, talking to the shell is programming in the shell. Just open a terminal window and type:

$ echo hello, world
hello, world
(Please try all the examples, even if you think you understand them. It's easy enough, so why not?)

You could type this a few times, for practice, but it's easier to recall the command than to retype it. Press the up-arrow once and you'll see the last command reappear. Press enter and the shell will execute the command.
$ ^
$ echo hello, world
The dollar sign is the prompt. It says the shell is ready to listen to your next command. Until you see the dollar sign, it's not. You can type while waiting for a prompt, and the terminal will buffer up your typing to give to the shell when it's finally done with what it's doing and ready for your next command.

A command will normally only be executed after you press ENTER, whether you've typed it or are just recalling it.
$ sleep 10
echo hello, world
[ten seconds elapses, then the next line appears, along with the execution of your command.]
$ echo hello, world
hello
You can press the up arrow a few times to look back through your history, or you can just type the command history.
$ history
1 echo hello, world
2 echo hello, world
3 sleep 10
4 echo hello, world
(Now that you've typed it, the command history is in your history.
$ history
1 echo hello, world
2 echo hello, world
3 sleep 10
4 echo hello, world
5 history
)

You can refer to and re-execute a command by its history number, but we'd like to discourage that. There are more powerful ways to use history, and you should build the habit of using them from the start.


Type control-R (^R), then, slowly, "hel" and watch what happens.
(reverse-i-search)`':
(reverse-i-search)`h': history
(reverse-i-search)`he': echo hello, world
(reverse-i-search)`hel': echo hello, world
The shell is looking, in reverse, through your history to find a command that matches the string you're giving it. This is incremental search. 'h' matched "history", so that's what it found first. As soon as you typed the 'e' , it looked further and found "echo hello, world" The "l" still matched, so it didn't look for a new line.

As soon as you've found the history line you want, press ENTER to execute it.

Exercise 1-1: Search for a command that isn't in your history.

If you find yourself marooned on a line that you do not want to execute, just press ^C to cancel it and get back to a prompt. Try this both while you're typing and while you're searching through your history. Notice, by examining your history when you do this, that canceled commands don't go into your history. Only executed commands are recorded.