|
Home > Archive > Unix Programming > April 2007 > ksh scripting question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
ksh scripting question
|
|
| lazyboy_2k@yahoo.com 2007-04-26, 1: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
| |
| Chris F.A. Johnson 2007-04-26, 7:17 pm |
| 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
| |
| Stephane CHAZELAS 2007-04-27, 7:17 am |
| 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
|
|
|
|
|