KORN SHELL ?? What are the rules for using array variables in a conditional statement in a korn shell script??

KORN SHELL ?? What are the rules for using array variables in a conditional statement in a korn shell script??

Post by bman.. » Sun, 08 Jul 2001 02:09:37



What are the rules for using array variables in a conditional statement
in a korn shell script??

Can it be used as in the experssion in RED>>

if ( [[ "(($MONTH - 1))" -eq $PMONTH ]]  &&
         [[ "(( ( ${DAYS_OF_YR}[$PMONTH] - $PDAY) + $DAY))" -lt 30 ]] )
then

thanks

 
 
 

KORN SHELL ?? What are the rules for using array variables in a conditional statement in a korn shell script??

Post by Chris F.A. Johnso » Wed, 11 Jul 2001 17:16:00



Quote:> What are the rules for using array variables in a conditional statement
> in a korn shell script??

You will find the rules in "man ksh"

Quote:> Can it be used as in the experssion in RED>>

I don't see anything in red (apart from the metaphorical flag that goes
up when people try to post to a text medium, such as usenet, using
non-text extensions).

Quote:> if ( [[ "(($MONTH - 1))" -eq $PMONTH ]]  &&
>          [[ "(( ( ${DAYS_OF_YR}[$PMONTH] - $PDAY) + $DAY))" -lt 30 ]] )
> then

What is your question?

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

KORN SHELL ?? What are the rules for using array variables in a conditional statement in a korn shell script??

Post by Dan Merc » Wed, 11 Jul 2001 21:13:05




> What are the rules for using array variables in a conditional statement
> in a korn shell script??

> Can it be used as in the experssion in RED>>

> if ( [[ "(($MONTH - 1))" -eq $PMONTH ]]  &&
>          [[ "(( ( ${DAYS_OF_YR}[$PMONTH] - $PDAY) + $DAY))" -lt 30 ]] )
> then

> thanks

A. Don't post in html.
B. If DAYS_OF_YR is an array, you index it with ${DAYS_OF_YR[index]}
   The index does not require a dollar sign - an index is
   inherently arithmetic.  However,  when you debug a script by
   setting "-x" or "-o xtrace",  using the dollar sign makes the
   value visible on assignment:

      $ set -A A 0 1 2 3 4 5
      $ x=3
      $ set -x
      $ A[x]=b
      + A[x]=b
      $ A[$x]=b
      + A[3]=b


   index:

      $ set -A A "one two three" "four    five   six"
      $ for i in "${A[*]}";do print "$i";done
      one two three four    five   six
      $ for i in ${A[*]};do print "$i";done
      one
      two
      three
      four
      five
      six

      one
      two
      three
      four
      five
      six

      one two three
      four    five   six


"${A[*]}" is equivalent to "${A[0]}d${A[1]}d${A[n]}"
where d is the first character of the $IFS string.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

KORN SHELL ?? What are the rules for using array variables in a conditional statement in a korn shell script??

Post by brian hile » Thu, 12 Jul 2001 03:01:13



> if ( [[ "(($MONTH - 1))" -eq $PMONTH ]]  &&
>          [[ "(( ( ${DAYS_OF_YR}[$PMONTH] - $PDAY) + $DAY))" -lt 30 ]] )

This is truly horrible/inefficient/unreadable code having not even
the grace of working properly; use the facilities of ksh that are
already provided to amend all these issues:

if (( MONTH-1==PMONTH && ${DAYS_OF_YEAR[PMONTH]}-PDAY+DAY < 30 )) ...

=Brian