Procrastiblog

January 6, 2007

The Unix Shell: Argh

Filed under: Tech — Chris @ 6:26 pm

Suppose you’re trying to write a simple shell script. And suppose you very badly want to put a command into a shell variable before you execute it. And suppose that command very badly needs to accept double-quoted strings as arguments. Oh, the layers of indirection! How will the shell tokenize such a thing?

Let the command in question be the following homely shell script.

string-arg.sh:
#! /bin/sh
# Usage: string-arg.sh [-s STRING]*
while test ${1+set}; do
case $1 in
"-s")
if ! test ${2+set}; then
echo "no string after -s"
exit 1
else
echo "string arg=\"$2\""
shift 2
continue
fi
esac
echo "bad arg: $1"
exit 1
done

Let’s see what happens.

% ./string-arg.sh -s "quoted arg"
string arg="quoted arg"
% cmd="./string-arg.sh -s \"quoted arg\""
% echo $cmd
./string-arg.sh -s "quoted arg"
% $cmd
string arg=""quoted"
bad arg: arg"
% eval $cmd
string arg="quoted arg"

AND REMEMBER, KIDS: echo `foo` gives you the output of foo and { foo; echo $? } gives you its exit value.

if foo 
then bar
fi

executes bar if foo succeeds (conventionally), which is equivalent to

foo && bar

in the short-circuiting parlance.

if ! foo 
then bar
fi

executes bar if foo fails (conventionally), which is equivalent to

foo || bar

in the short-circuiting parlance.

if `foo`
then bar
fi

executes bar if the command output by foo exists and returns exit value 0 (if it doesn’t exist, it will just exit).

if test `foo`
then bar
fi

executes bar if foo doesn’t produce any output.

[UPDATE] One more thing. Setting shell variables.

% CMD=cmd arg
bash: arg: command not found
% CMD= cmd arg
bash: cmd: command not found
% CMD= "cmd arg"
bash: cmd arg: command not found
% CMD="cmd arg"
% echo $CMD
cmd arg

This often trips me up, because make is much more forgiving and I hack more Makefiles than shell scripts.

[UPDATE 1/8/2007] I got the if-then stuff wrong the first time. Which goes to show you how desperately I need this tutorial. In shell-world exit code 0 is “true” and exit code not-0 is “false”. This is sort of the opposite of the C convention, with the caveat that exit values and arithmetic values shouldn’t be conflated.

Another trip-up from C to the shell is that whitespace makes a bigger difference. if ! foo; then bar; fi is not the same as if !foo; then bar; fi.

if foo
then bar
fi

is not the same as if foo then bar fi. {foo; bar;} is not the same as { foo; bar;}. And so on, world without end.

December 3, 2006

Netflix Queue Manager

Filed under: Tech — Chris @ 4:48 am

If you have more than three movies in your Netflix queue, than you need this: drag and drop queue management that works with Firefox or IE. (We actually stumbled across this because H wanted to “shuffle” our queue, which is just ridiculous. Some people…) Why isn’t this the default Netflix interface?

November 10, 2006

Edgy Eft-ing Sound

Filed under: Tech — Chris @ 5:00 pm

This is kind of weird, coming two weeks after I upgraded, but my audio card just completely stopped working. At first, I thought this was one of those little Linux driver farts—I spent half the summer restarting X to get Skype working again—but a reboot didn’t fix it and there were no relevant error messages to speak of. Following these directions fixed it. The key part, I think, is making sure asound.conf exists—it seems that somebody (not me) had deleted it. In fact, I can’t find a Ubuntu package that wants to take credit for installing asound.conf, much less one that wants to take credit for deleting it.

November 4, 2006

Edgy Eft-ing Swap

Filed under: Tech — Chris @ 11:23 pm

Major Complain the First has been addressed by the magic of the Internet. It seems the Ubuntu developers decided to change the already-cryptic Linux disk naming scheme and make it even more cryptic—my swap disk used to be /dev/sda5; it is now UUID=68c2346f-f652-429a-a974-2abac9b7ffbd. Then, to top it off, they didn’t bother to update /etc/fstab or the initramfs settings.

Hibernate is back! Hurray! Suspend still doesn’t work! Boo!

Broadcom, Nvidia: the ball is in your court.

November 2, 2006

Drawing for LaTeX

Filed under: Tech — Chris @ 4:03 pm

Is this (God help me) the figure editor I’ve been dreaming of? The one with a graphical interface and really nice Postscript output, suitable for inclusion in my LaTeX documents? (xfig doesn’t cut it, pstricks is like programming in assembly.)

I think it might be!

POSTSCRIPT: Trouble on the make the easy things easy front: I want a box or a circle with text centered in it. How?

October 31, 2006

Edgy Eft-ing Fonts

Filed under: Emacs, Tech — Chris @ 12:35 am

Where did my fonts go in Emacs? Be calm, child. Somebody got pedantic with path names. Search-and-replace /usr/share/X11/fonts with /usr/share/fonts/X11 in /etc/X11/xorg.conf.

October 30, 2006

Edgy Eft-ing Flash

Filed under: Tech — Chris @ 5:11 pm

Thinking about installing Ubuntu Linux 6.10 aka Edgy Eft? Thinking about upgrading to Firefox 2.0 on Linux (the first implies the second)? A word to the wise: Firefox 2.0 is highly incompatible with the Flash plugin. Like, your browser will crash in the blink of an eye if there’s so much as a YouTube embed on a web page (and there are precious few YouTube-free web pages these days… ah, Web 2.0). Never fear! Just add

export XLIB_SKIP_ARGB_VISUALS=1

as the next-to-last line in /usr/bin/firefox (before the exec line). Makes Firefox YouTube-arific. For this and other useful Edgy tips, visit The Ubuntu Guide.

P.S. This is actually Major Complaint the Second about my Edgy upgrade. Major Complaint the First is that it has completely hosed my Suspend/Hibernate settings. I am hopeful that this will fix itself with time. Major Complaint the Third is that Broadcom and Nvidia still won’t play nice, about which I am less optimistic (There is actually a potential fix for this in not-CVS, but it is entirely unclear* when it might show up in a mainstream distro.)

* Seriously unclear. As in: maybe next month and maybe never.

September 29, 2006

Notes on a Broken iPod

Filed under: Tech — Chris @ 12:22 am

  1. The Genius Bar is a good thing: free face-to-face troubleshooting for any Apple product. Note, however: (a) you need an appointment, which you can get online, (b) you apparently need to sign in even if you have an appointment, which is not at all clear when you arrive, and (c) there is a separate Genius Bar just for iPods, which is past the regular Genius Bar, and which really, really isn’t clear when you arrive.
  2. It actually can be cost effective to repair an iPod. Portatronics replaced my batter and my clickwheel in under 10 minutes. The charge was, IIRC, $30 for labor, $20 for a the wheel, and $50 for the battery. Which means as long as my hard drive doesn’t die (knock wood), I’ve got an essentially new 30GB iPod for about $100. Apple wanted to sell me a 5GB Nano for $120.
  3. Trying to get your iPod working with Linux is not worth the effort.* The filesystem is liable to get corrupted if you look at it funny and gtkPod is laughably unusable.

* For those of you who are non-technical or who are tired of the Linux posts: I have my reasons!

September 22, 2006

IRQ, PCI, Linux, Broadcom, Nvidia

Filed under: Tech — Chris @ 4:05 pm

(If the title looks like gibberish, move along.)

Oh, woe. Woe. Woe!

The setup: a Dell Inspiron 6400, an nVidia GeForce Go 7300, a built-in Broadcom BCM4401 wireless chip, and Ubuntu Dapper 6.06 with the latest kernel patches (2.6.15-27-686 SMP PREEMPT).

The only way I’ve gotten the Broadcom to work is with a custom-compiled version of ndiswrapper and bcmwl5.inf. The free driver that’s shipped with the kernel could but doesn’t support the particular variant of the wireless chip that I have (code no 4311). (Support has been checked into CVS and will eventually trickle down to peons like me.)

I currently have the graphics card running under a proprietary driver (nvidia-glx) that was installed by Automatix. Previously, I was running a stock driver that caused the display to die on Suspend. (Now, the display only occassionally dies on Suspend.)

Since I installed the graphics driver several days ago, my PC has started periodically crashing, hard. Since this morning, my PC has started crashing hard and often. (It occurs to me that the hardware has gotten itself stuck in some crash-erific state and that possibly a solution is to somehow reset the hardware. However, this strategy, should it succeed, would still leave me facing intermittent crashes in the future.)

Here’s the thing: the nVidia driver and ndiswrapper share IRQ 169. Here’s the log of the crash:


Sep 22 11:33:25 localhost kernel: [17179867.804000] [__report_bad_irq+42/160] __report_bad_irq+0x2a/0xa0
Sep 22 11:33:25 localhost kernel: [17179867.804000] [handle_IRQ_event+61/112] handle_IRQ_event+0x3d/0x70
Sep 22 11:33:25 localhost kernel: [17179867.804000] [note_interrupt+135/240] note_interrupt+0x87/0xf0
Sep 22 11:33:25 localhost kernel: [17179867.804000] [__do_IRQ+253/272] __do_IRQ+0xfd/0x110
Sep 22 11:33:25 localhost kernel: [17179867.804000] [do_IRQ+25/48] do_IRQ+0x19/0x30
Sep 22 11:33:25 localhost kernel: [17179867.804000] [common_interrupt+26/32] common_interrupt+0x1a/0x20

This invariably happens after I bring up the wireless connection.

Egads! What to do?! Live without Suspend? Live without Wireless? Can you call that living?

SIDE NOTE: “Aha!” I thought, “I have an old, reliable Orinoco card. Let’s try that!” But… is it possible my laptop doesn’t have PC Card slot? What gives? What is this PC Card-shaped slot that the card won’t slide into? What is its purpose other than to make me angry?

P.S. I have heard complaints about similar problems from students running other Linux versions/distros.

September 13, 2006

Changing file extensions in bash

Filed under: Tech — Chris @ 9:30 pm

for f in *.old; do mv "$f" "${f%%\.old}.new"; done

The quotes take care of possible spaces in the filename.

« Newer PostsOlder Posts »

Blog at WordPress.com.