Array in ksh with if-else

Hi All,

My Requirement is as follows:

  1. User will input Source Sytem Code as input.
  2. I have source system codes as 11, 34, 56, 99, 45 etc.

OS Version: SunOS 5.8 Generic_117350-62 sun4u sparc SUNW,Sun-Fire-V890

My code is like...

  echo 'Source System Code: \c'
  read varSSCode # Reading Source System Code
if [ "varSSCode" == "11" -o "varSSCode" == "56" -o "varSSCode" == "93" ]; then
    ls -ltr; cut -d~ -f14,37,48 data_file|sort|uniq -c; wc -l data_file
else
    ls -ltr; cut -d~ -f12,37,48 data_file|sort|uniq -c; wc -l data_file

So I want the following code

 if [ "varSSCode" == "11" -o "varSSCode" == "56" -o "varSSCode" == "93" ]; 

to be in array (if possible).

Like...
If varSSCode in that array then
do this and this...

Please suggest.

Cheers,
Saps.

First, there are missing $ in your script :

if [ "$varSSCode" == "11" -o "$varSSCode" == "56" -o "$varSSCode" == "93" ]; then

A possible solution (using a list of values, not an array) :

#!/usr/bin/ksh

validCodes=",11,56,93,"

read varSSCode?"Source System Code: "
if [[ "${validCodes}" == *,${varSSCode},* ]]
then
    echo "ok. ls -ltr; cut -d~ -f14,37,48 data_file|sort|uniq -c; wc -l data_file"
else
    echo "KO! ls -ltr; cut -d~ -f12,37,48 data_file|sort|uniq -c; wc -l data_file"
fi

Jean-Pierre.

1 Like
read varSSCode?"Source System Code: "
case $varSSCode in
     11|56|93) echo " code OK" ; F='-f14,37,48' ;;
     *) echo "code KO" ; F='-f12,37,48' ;;
esac
ls -ltr
eval cut -d~ $F data_file|sort|uniq -c
wc -l data_file 

Thanks for the heads-up.. I really missed it..!

Apologies for the delay in replying. Due to unavailability of UAT Server, I was not able to test the script.

But now, I've tested it and works fine with list of values.

Thanks a lot Jean.

Cheers,
Saps