ksh scripting question - Unix

This is a discussion on ksh scripting question - Unix ; 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 : # ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: ksh scripting question

  1. ksh scripting question

    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


  2. Re: ksh scripting question

    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 |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  3. Re: ksh scripting question

    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

+ Reply to Thread