bash: best way to do tenary operator??

bash: best way to do tenary operator??

am 18.04.2008 19:08:14 von lihao0129

Hi, folks:

In most programming language, we can have a tenary operator like:

A = ( condition ) ? A1 : A2;

How can I do this nicely in bash. Currently I am using:

if [ condition ]; then
A=A1
else
A=A2
fi

which looks baggy, any better ways to handle this, many thanks..

lihao..

Re: bash: best way to do tenary operator??

am 18.04.2008 20:34:09 von Bill Marcum

On 2008-04-18, lihao0129@gmail.com wrote:
>
>
> Hi, folks:
>
> In most programming language, we can have a tenary operator like:
>
> A = ( condition ) ? A1 : A2;
>
> How can I do this nicely in bash. Currently I am using:
>
> if [ condition ]; then
> A=A1
> else
> A=A2
> fi
>
> which looks baggy, any better ways to handle this, many thanks..
>
A=$(( condition ? A1 : A2 ))
Depending on what "condition" is, you might do
test condition; X=$?
A=$(( X ? A1 : A2 ))

Re: bash: best way to do tenary operator??

am 19.04.2008 02:24:55 von lihao0129

On Apr 18, 1:34=A0pm, Bill Marcum wrote:
> On 2008-04-18, lihao0...@gmail.com wrote:
>
>
>
>
>
> > Hi, folks:
>
> > In most programming language, we can have a tenary operator like:
>
> > =A0 =A0 A =3D ( condition ) ? A1 : A2;
>
> > How can I do this nicely in bash. Currently I am using:
>
> > =A0 =A0 if [ condition ]; then
> > =A0 =A0 =A0 =A0 A=3DA1
> > =A0 =A0 else
> > =A0 =A0 =A0 =A0 A=3DA2
> > =A0 =A0 fi
>
> > which looks baggy, any better ways to handle this, many thanks..
>
> A=3D$(( condition ? A1 : A2 ))
> Depending on what "condition" is, you might do
> test condition; X=3D$?
> A=3D$(( X ? A1 : A2 ))- Hide quoted text -
>

nice, thank you very much.. :-)


> - Show quoted text -