[Novalug] Shell Puzzle Challenge

Jon LaBadie novalugml at jgcomp.com
Sat Mar 17 01:42:49 EDT 2012


On Fri, Mar 16, 2012 at 06:00:33PM -0400, James Ewing Cottrell 3rd wrote:
> Since there seems to be an interest in Shell Scripting right now, I'd 
> like to issue the following challenge:
> 
> Write a shell function called "ispath" which returns true if its 
> argument is part of $PATH, otherwise false
> 
> In other words, I want to use it like this:
> 
> export PATH=/sbin:/usr/sbin:/bin:/usr/bin
> 
> if ispath /bin
> then echo YES
> else echo NO
> fi
> 
> -> YES
> 

A general comment in dealing with the PATH variable.
Often overlooked are "null" fields and "." fields.

A leading or trailing colon ":" or pair of colons "::"
implies the current directory ".".  Of course the dot
could be given explicitly.

So some considerations:

Does your code handle a missing (null) argument as an error?

If a null argument is not handled as an error, might it
match a null field in $PATH?

If the user runs "ispath .", and $PATH has a null field,
does ispath correctly report "yes"?

If the users current directory is not specifically
listed in $PATH, but $PATH contains "." or a null field,
does "ispath $PWD" report "yes"?  Should it?

jl
-- 
Jon H. LaBadie                  novalugml at jgcomp.com
 11226 South Shore Rd		(703) 787-0688 (H)
 Reston, VA  20190		(609) 477-8330 (C)


More information about the Novalug mailing list