From: lihao0129 on
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..
From: Bill Marcum on
On 2008-04-18, lihao0129(a)gmail.com <lihao0129(a)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 ))
From: lihao0129 on
On Apr 18, 1:34 pm, Bill Marcum <marcumb...(a)bellsouth.net> wrote:
> On 2008-04-18, lihao0...(a)gmail.com <lihao0...(a)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 ))- Hide quoted text -
>

nice, thank you very much.. :-)


> - Show quoted text -

 | 
Pages: 1
Prev: Wget
Next: cd is not working as expectd