-d option not working. . .?
Hi All, I've got a script that I'm trying to set up, but it keeps telling me that "[-d command not found". Can someone please explain what is wrong with this?: #!/bin/sh for i in $* do { if [-d $i] then echo "$i is a directory! Yay!" else echo "$i is not a directory!" fi } done Regards, Michael
Re: -d option not working. . .?
On Sep 12, 2007, at 2:56 AM, Bob Proulx wrote: The [ is a shell builtin, not a shell metacharacter. Shell metacharacters do not need to be separated by whitespace but the test program needs to be apart or it won't be parsed right. That is why you are seeing "[-d" not found. It is not a parenthesis as in some programming languages. The [ is a synonym for the "test" operator. It is a command like grep, sed, awk, etc. Being that I'm not a bash (or any other shell for that matter) guru, is there any reason that parsing occurs this way? Why is it not more like other programming languages? [ -d /tmp ] && echo /tmp is a dir Do it this way. if [ -d "$i" ] Note that you should quote the argument to protect against whitespace there. Thanks, that worked perfectly!
Re: -d option not working. . .?
Two words: history and POSIX. It's been done that way for more than 20 years, so it was standardized that way. Changing it would break too many existing scripts. Forgive me for saying so, and please appreciate both the sarcasm and irony, but I've never been one for "that's the way it's always been". I mean, if we all thought that way, we'd be a bunch of bloodletting flat-earthers. . .no? ;) That said, I'm sure these BASH and POSIX guys were (and still are) a lot smarter than myself. So I'll rest my faith on that and see about doing a bit more getting familiar. Best Regards All! Michael