[Novalug] Shell Puzzle Challenge

John Holland jbholland@gmail.com
Fri Mar 16 18:49:13 EDT 2012


On 3/16/12 7:45 PM, James Ewing Cottrell 3rd wrote:
> Well, it certainly works, but you used an external command (tr) when
> you didn't have to.
>
> Of course you could have done this:
>
> IFS=$IFS:
what is this?


> set $PATH
doesn't this set PATH to nothing?
> for entry # in * is assumed
how does this reference the PATH var?

> do
>     what you said
> done
>
> I had also asked for a shell function rather than a script, which
> would mean you'd have to save an restore IFS:
>
> OFS=$IFS
> IFS=$IFS:
> script
> IFS=$OFS
>
> Also...no reason to quote the colon on tr.
>
> JIM
>
> P.S. The optimization I was thinking about cannot be applied to your case.
>
what is the optimization? Or has that not been revealed yet?

> On 3/16/2012 5:18 PM, John Holland wrote:
>> I'll take the bait:
>>
>>
>>
>> #!/bin/bash
>> for entry in `echo $PATH | tr ":" "\n"`
>> do
>>     if [ $entry = $1 ]
>>        then
>>         exit 0
>>        fi
>> done
>> exit 1
>>
>>
>> On 3/16/12 6:00 PM, 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
>> >
>> > if ispath /i/love/selinux
>> > then echo Hi, I am Peter
>> > else echo Yo, I be JIM
>> > fi
>> >
>> > -> echo Yo, I be JIM
>> >
>> > I'll tell you right now that there is a trick to it that makes it
>> > simpler. This trick is not only useful in this case, but is useful to
>> > programming in general.
>> >
>> > JIM
>> >
>> > P.S. You can use "return 0" to return a true result and "return 1" to
>> > return false
>> >
>> >
>> > _______________________________________________
>> > Novalug mailing list
>> > Novalug@calypso.tux.org
>> > http://calypso.tux.org/mailman/listinfo/novalug
>>
>>
>>
>>
>> _______________________________________________
>> Novalug mailing list
>> Novalug@calypso.tux.org
>> http://calypso.tux.org/mailman/listinfo/novalug
>
>
>
> _______________________________________________
> Novalug mailing list
> Novalug@calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/novalug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.firemountain.net/pipermail/novalug/attachments/20120316/9437f366/attachment.htm>


More information about the Novalug mailing list