[Novalug] Shell Puzzle Challenge
James Ewing Cottrell 3rd
JECottrell3@Comcast.NET
Fri Mar 16 19:48:19 EDT 2012
Using grep is indeed suboptimal. What internal shell construct could you
use instead?
Actually, there are two answers, and I bet most people would pick the
wrong one.
JIM
On 3/16/2012 6:34 PM, Geoff McNamara wrote:
> Corrections: the EGREP_CMD= line was wrong (amongst other likely
> deficiencies)
>
> I use this in (and then throughout) my .bashrc - it could be
> optimized/greatly improved...
>
> ##########
> pathIT ()
> ##########
> {
> # $1=path to add
> # $2 if = "after" then the new path dir will be added to the end of
> the PATH list
> EGREP_CMD=${EGREP_CMD:'type -p egrep'}
> #echo DEBUG: Now adding $1 to PATH
> if [ "x$2" = "xafter" ]; then
> if ! echo $PATH | $EGREP_CMD "(^|:)$1(:|$)">/dev/null ; then
> PATH=$PATH:$1
> fi
> else
> if ! echo $PATH | $EGREP_CMD "(^|:)$1(:|$)">/dev/null ; then
> PATH=$1:$PATH
> export PATH
> fi
> fi
> #echo AFTER: PATH=$PATH
> }
>
>
>
> On 03/16/2012 06:23 PM, Geoff McNamara wrote:
>> I use this in (and then throughout) my .bashrc - it could be
>> optimized/greatly improved...
>>
>> ##########
>> pathIT ()
>> ##########
>> {
>> # $1=path to add
>> # $2 if = "after" then the new path dir will be added to the end of
>> the PATH list
>> EGREP_CMD={EGREP_CMD:-type -p egrep}
>> #echo DEBUG: Now adding $1 to PATH
>> if [ "x$2" = "xafter" ]; then
>> if ! echo $PATH | $EGREP_CMD "(^|:)$1(:|$)">/dev/null ; then
>> PATH=$PATH:$1
>> fi
>> else
>> if ! echo $PATH | $EGREP_CMD "(^|:)$1(:|$)">/dev/null ; then
>> PATH=$1:$PATH
>> export PATH
>> fi
>> fi
>> #echo AFTER: PATH=$PATH
>> }
>>
>>
>> On 03/16/2012 06: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
>
>
More information about the Novalug
mailing list