Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: simplify and if then else



2008/9/24 Mikael Magnusson <mikachu@xxxxxxxxx>:
> 2008/9/24 Peter Stephenson <pws@xxxxxxx>:
>> zzapper wrote:
>>> Hi
>>> Having a Homer Simpson moment, but this must be easy to simplify
>>> if [[ "$LOGNAME" != davidr ]]
>>> then
>>> cd /o/inetpub/wwwdev/www.some.co.uk/epsystem/epadmin
>>> else
>>> cd /c/inetpub/wwwdev/www.some.co.uk/epsystem/epadmin
>>> fi
>>>
>> cd \
>> ${${${LOGNAME:#davidr}:+/o}:-/c}/inetpub/wwwdev/www.some.co.uk/epsystem/epadmin
>>
>> although if LOGNAME is empty it's treated the same as if it were davidr;
>> if that's an issue it gets even more clumsy:
>>
>> ${${${${LOGNAME:-x}:#davidr}:+/o}:-/c}
>
> A bit longer but arguably still simpler :)
> cd /$([[ "$LOGNAME" != davidr ]] && echo o || echo c)/inetpub/wwdev/blabla

If all you want to do is avoid writing the full dirname twice, this also
works in very new versions:
() {
local dir
if [[ "$LOGNAME" != davidr ]]; then
  dir=o
else
  dir=c
                           fi
cd /$dir/inetpub/wwwdev/www.some.co.uk/epsystem/epadmin
}

If it's not a very new version, you have to name the function. Or hope the
variable name is unique.

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author