Operating System - HP-UX
1834817 Members
2459 Online
110070 Solutions
New Discussion

looking for a shorten if...then... ksh statement.

 
Hanry Zhou
Super Advisor

looking for a shorten if...then... ksh statement.

I want to achieve the following:

if [[ `uname -r` = "B.11.11" ]]; then
echo "I'M 11.11"
else
echo "I'm 11.0"
fi

Now, for some reasons, I have to make the if/then/else statement short, and I remember there is a way to achieve that withought using if/the/else statement. I just can not remember it now.

Something like: (`uname -r` ?; | B.11.11; echo "I'm 11.11|...). Again, I can not recall what exactly the statement is.

Please help me out.
none
9 REPLIES 9
Sundar_7
Honored Contributor

Re: looking for a shorten if...then... ksh statement.

[[ $(uname -r} = "B.11.11" ]] && echo "I'M 11.11" || echo "I'm 11.0"
Learn What to do ,How to do and more importantly When to do ?
steven Burgess_2
Honored Contributor

Re: looking for a shorten if...then... ksh statement.

hello

simply

[[ `uname -r` = "B.11.11" ]] && echo "im 11.11" || echo "im 11.00"

Regards


Steve
take your time and think things through
Sundar_7
Honored Contributor

Re: looking for a shorten if...then... ksh statement.

Sorry a typo.

[[ $(uname -r) = "B.11.11" ]] && echo "I'M 11.11" || echo "I'm 11.0"
Learn What to do ,How to do and more importantly When to do ?
Bharat Katkar
Honored Contributor

Re: looking for a shorten if...then... ksh statement.

Hi,
This works for me on all versions 10.x,11.x

# uname -r | echo "This version is HPUX `cut -d "." -f 2-3`"


Regards,
You need to know a lot to actually know how little you know
Peter Nikitka
Honored Contributor

Re: looking for a shorten if...then... ksh statement.

Hi,

the solutions
cmd && echo ok || echo failed
are ok. But keep in mind, that this is only
the case, when the second command ('echo ok')
never fails!
If your question was just a common example,
keep in mind, that something like this

cmd && false || echo something

will execute 'false' AND echo 'something' -
since the commans before the '||' failed!

A command sequence like

[ -d dir ] || mkdir dir && echo re-using dir

will do the echo, even if 'dir' was just created.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hanry Zhou
Super Advisor

Re: looking for a shorten if...then... ksh statement.

Here is some addition:

I actually wanted to run this entire statement on the remote server by using ssh:
ssh remoteserver â [[ `uname â r` = â B.11.11 ]] && CMD1_on_remote || CMD2_on_remoteâ

However, my questions is when I run uname â r, is this command really running on local or remote site?
I want to run CMD1 and CMD2 on remote site, based on the different version of uname â r on remote site.

It doesnâ t seem to b
none
Hanry Zhou
Super Advisor

Re: looking for a shorten if...then... ksh statement.

Here is the correct statement:

ssh remoteserver "[[ `uname -r` = "B.11.11" ]] && CMD1_on_remote || CMD2_on_remote"
none
Hanry Zhou
Super Advisor

Re: looking for a shorten if...then... ksh statement.

it did start CMD2 on remote site, not matter what result of uname -r
none
Sridhar Bhaskarla
Honored Contributor

Re: looking for a shorten if...then... ksh statement.

Hi Hanry,

I wouldn't try shell's built-in commands with remsh and ssh. You could try escaping the quotes and all but here is the easy way.

ssh remotehost "uname -r |grep -q B.11.11 && CMD1_onremote || CMD2_onremote"

-Sri
You may be disappointed if you fail, but you are doomed if you don't try