ksh scripting question
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > ksh scripting question




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    ksh scripting question  
lazyboy_2k@yahoo.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-26-07 06:17 PM

Hi,

I'm looking at a internal script & see this below if statement but I
don't understand it.  Could someone please explain what it means if
you don't mind?

#!/bin/ksh

row=0
if [ $row > 0 ]
then
:              # note I don't understand what " : " notation
actually does & means
else
.......
fi

TIA,
-Chris






[ Post a follow-up to this message ]



    Re: ksh scripting question  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 12:17 AM

On 2007-04-26, lazyboy_2k@yahoo.com wrote:
> Hi,
>
> I'm looking at a internal script & see this below if statement but I
> don't understand it.  Could someone please explain what it means if
> you don't mind?

The script is broken.

> #!/bin/ksh
>
> row=0
> if [ $row > 0 ]

That should be:

if [ $row -gt 0 ]

In the shell, '>' is the redirection operator, not a comparison
operator, although some shells allow it as a string comparison
operator if it is escaped, e.g.:

if [ "$q" \> alpha ]

>   then
>      :              # note I don't understand what " : " notation actually does & 
means

It is a command that does nothing. Any arguments will be expanded,
$_ will be set, and any side effects will be performed:

q=
: ${q:=qwerty} asdfg
printf "%s\n" "$q" "$_"

>   else
>      ........
> fi


--
Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
Shell Scripting Recipes:     |  My code in this post, if any,
A Problem-Solution Approach  |         is released under the
2005, Apress                 |    GNU General Public Licence





[ Post a follow-up to this message ]



    Re: ksh scripting question  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 12:17 PM

2007-04-26, 10:29(-07), lazyboy_2k@yahoo.com:
> Hi,
>
> I'm looking at a internal script & see this below if statement but I
> don't understand it.  Could someone please explain what it means if
> you don't mind?
>
> #!/bin/ksh
>
> row=0
> if [ $row > 0 ]
>   then
>      :              # note I don't understand what " : " notation
> actually does & means
>   else
>      ........
> fi
[...]

Some early versions of ksh didn't have the "!" keyword, so if
you wanted to do: "if not this, then that", then you had to do
"if this, then do-nothing else that".

So:

if [ "$row" -gt 0 ]
then
:
else
..
fi

is the same as standard sh's:

if ! [ "$row" -gt 0 ]
then
..
fi

Note that the "[" command also has a "!" operator even in old
versions of ksh:

if [ ! "$row" -gt 0 ]
then
..
fi

There's also:

if [ "$row" -le 0 ]

though that's not strictly equivalent, for instance when "$row"
doesn't expand to a valid number.

--
Stéphane





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:22 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register