Using $* in shell scripts is Nearly Always Wrong

(Bourne and Bash) Shell script writers bandy ‘$*’ about like it’s a stick of celery, but the truth is $* is nearly always the wrong thing to use. (For the clueless, $* expands to all the “positional parameters”, that is, all the parameters to the current script or function; it’s useful in cases where you want to pass the argument values for those parameters on to another program).

So why is $* wrong? Because it is evaluated before word splitting. In other words, if any of the arguments to your script had a space or other whitespace, then those arguments will become two when $* is evaluated. The following shell script can be used to verify this:

#!/bin/sh
# Call this script “mytouch”
touch $*

… Now try to “mytouch” a file with a space in its name (yeah, well done, you just created two files with one command).

Does quoting it work? will “$*” solve your problems? It will fix the above example, right up to the point where you try to “mytouch” two or more files at once and then you’ll see the fallacy (hint: “$*” always expands to a single “word”).

So what is the correct solution? It’s simple: use “$@” instead. Note, that’s $@, with quotes around it. Hence “$@”. This specifically expands to one string for each actual argument. It works. Hallelujah. (And while I’m blathering on shell scripts, note that any unquoted variable expansion is also Nearly Always Wrong for pretty much the same reasons).

Advertisements

The Joy of Wireless Networking with Linux

I just bought a cheap D-Link DWL-G120 “AirPlus” wireless adapter which connects to the PC via USB. I was hoping to either set it in AP (“Master”) mode and use it as an access point (so I can connect to my server from my MacBook wirelessly) or, failing that, in Ad-hoc mode; however, it seems that the prism (p54usb) driver in linux supports neither mode. Not that this is actually documented anywhere or anything, no, that would be too helpful! You can actually set Ad-hoc mode using iwconfig, but then trying to bring up the interface (with “ifconfig wlan0 up”) generates:

SIOCSIFFLAGS: Operation not supported

Needless to say, that’s not very helpful, and there’s no dmesg output which gives any clue as to what the problem is. Your humble narrator spent several hours sifting through kernel source code before he discovered where the problem was (drivers/net/wireless/p54/p54common.c, p54_add_interface() method refuses anything but managed mode; no trivial fix).

I found a patch which apparently adds support for both Master and Ad-hoc modes, and though I managed to apply it against my 2.6.26.3 kernel tree, it doesn’t work and caused a kernel panic when I was messing around with it (so, yeah, not good really). It didn’t apply cleanly so though I assume that there might be some kernel tree out there with working Master mode for the p54, but if there is I can’t find it. The patch doesn’t seem to be applied in the wireless-next tree. Looks like I’ll just have to find a wireless router for the time being.

Links:

http://wireless.kernel.org/en/users/Drivers/p54 (yeah, ok, here it says that only Station mode (= Managed mode) works).

Edit 2008-09-01: Hmm. I now have a linksys router running (as an AP) and still can’t connect to it using the D-Link. Can’t be bothered to debug it just now, but I suspect the driver just Does Not Work at all (though it can list various networks it finds on the airwaves, so it works partially at least).

Apple Sues Again

A while ago I wrote an entry saying how Psystar, which appears to be a Florida-based company, has begun producing Mac clones which can run Mac OS X. These clones were never sanctioned by Apple, and there was some speculation at the time that Apple would fire its legal cannons at some point.

It seems that time has now come.

It will be interesting to see how this plays out. From what I can gather, Apple are arguing that trademark infringements have occurred and weakened Apple’s name. Without knowing much detail, this sounds pretty weak. On the other hand, Apple has a lot of money and a lot of lawyers. Also, as I’ve discussed previously, Apple software EULAs often specifically state that you may not use their software on non-Apple hardware – a term which may or may not be enforcable in the courts.

Only time will tell.

2008-8-7: Psystar has countersued Apple, for anticompetitive business practices.

Re-thinking the website sign-up

There are a bunch of websites where you have to sign up as a user, a proces which normally requires that you hand over an email address which the site will validate by sending you an email with some sort of secret token (a URL) that you need to actually activate the account. As it happens, I’m implementing such a feature at the moment. Assuming that these sites aren’t actually harvesting the addresses for purposes of on-selling them or spamming them directly, it’s usually because they need a way to do things like:

  • prevent a single person from creating an unlimited number of accounts
  • prevent all but the most clever bots from creating accounts
  • permanently ban troublesome users

All of these are perfectly valid concerns. Most sites that I am aware of require you to provide an email address when you create your account, at which time you also choose your username and password (and provide whatever other details are deemed necessary). However, there are problems with this approach, and I think there’s a better way to do it.

Continue reading

Hardware that Works with Linux

It’s sometimes surprisingly difficult to find out whether hardware will work with Linux. Partly, this is because a piece of hardware often consists internally of chips manufactured by someone other than the hardware itself, and it’s the chips which you must have drivers for. For instance, I have a Leadtek TV tuner card (PCI) and the chips onboard it are a couple of Connexant chips plus a Philips (tuner) chip. This is one of the few products I have seen which actually lists the chips on the box, however. Finding out what chips a product uses can be difficult in itself. Often the easiest way (for PCI cards) is to plug the card in and use “lspci” or equivalent to find out what’s on it, but by that stage you’ve already laid down your cash for the product.

Even knowing the chips and knowing that there is a linux driver for them doesn’t always tell you how well your device will be supported. The aforementioned TV tuner card required that I add a subsystem ID to the driver and recompile the kernel. Once I’d done that, it worked fine (well, it received digital TV fine; I never tried analog. I also got the remote to work fairly easily).

I bought a new (external) ADSL/2/2+ modem recently, an “OpenNetworks iConnectAccess621”. It has a single ethernet port as well as a USB port. I wondered if I would be able to talk to the modem via the USB port and thereby free up two ethernet ports – one on the PC and one on the modem. As it turned out, the iConnectAccess621 use a Texas Instruments (TI) chip, and I could talk to it using the “RNDIS” driver in Linux (RNDIS apparently is a badly-documented Microsoft-developed protocol for ethernet-over-USB); however, restarting the computer seemed to lock up the connection (couldn’t talk to the modem anymore) until I also power-cycled the modem. I partly blame the modem and partly blame Linux’s USB implementation (which seemed to have a lot of trouble dealing with the resulting situation on the USB bus; kept on giving error messages and took ages to boot).

Continue reading

The Alternative Mac?

Recently I wrote a post about Apple’s EULA for various of their software products requiring that the software only be installed and run on Apple-manufactured hardware. Just recently, a company called Psystar Corporation (yeah, I don’t know how to pronounce it either) made an interesting announcement: they are producing Mac-compatible PCs which can run OS X. Not only that, but they’re willing to sell you OS X Leopard pre-installed on the machines.

I wonder what Apple’s response to this will be. Apple are fairly litigous and I will think they will probably try for an injunction if they think they have any chance of succeeding. Of course, they might decide they don’t. I’ve already said that I think the terms of the EULA are anti-competitive and should be illegal; now, we might find out what the courts think (or at least, whether Apple believes its own terms are enforcable. If it doesn’t, that certainly leads to the question of why they inclue these terms in the first place…).

Edit 17/04/08: You might want to be careful before you give this company any money. See what OSNews says.