Multiple if within while loop ksh

Hi All,

I'm trying to write while loop with multiple if conditions. Each and every if condition with different variables. whenever one if condition fails i have remove the file
from filename and have to pick another file and loop should exit until the last file found in filename. Please help how to write a ksh shell script for that. Below i have given code what i tried

Thanks in advance.!!!

example:

#### here filename contain XXXX_YYYY_ZZZZ_DDD.txt as example
while read filename
#splitting a file without extension
full_filename=$filename
noextension=${full_filename%.*}
extension="${full_filename##*.}"

####assigning a patterns to different variable

echo "$noextension"|awk -F'_' '{ print NF-1 }' > countpattern

##### First fi######
if [ "$countpattern" -eq 5 ] ; then
A=$(echo $noextension|cut -d'_' -f1)
B=$(echo $noextension|cut -d'_' -f2)
C=$(echo $noextension|cut -d'_' -f3)
D=$(echo $noextension|cut -d'_' -f4)
E=$(echo $noextension|cut -d'_' -f5)
fi

####Second If########
if [ ! $extension=txt-a $A=HELP] || [ ! $extension=csv -a ! A =HELP ]
then
UPDATE TABLE ########################
rm full_filename
fi

####third if#####
if [ ! $C = "WE" ]
then
UPDATE TABLE ########################
rm full_filename
fi

How about

TMP=csvtxt                                                                     # extension template
while read filename
  do    IFS="._" read -A IARR <<< $filename                                    # split filename into array containing elements and extension (${IARR[-1]})
        if [ ! "${TMP/${IARR[-1]}}" = "$TMP" -a ! "${IARR[0]}" = "HELP" ] ||   # first condition  - former $A is ${IARR[0]} now
           [ ! "${IARR[2]}" = "WE" ]                                           # second condition
          then  # update table                                                 # do whatever necessary
                echo rm $filename                                              # remove echo to remove file
        fi
  done < input

${IARR[-1]} is bash-4.
ksh (and bash-3) need ${IARR[${#IARR[@]}-1]}

read -A and <<< require ksh93 - do not work with the older ksh88.

1 Like

ksh93+, (ksh was the shell the OP specified), also supports negative indexes, to indicate elements from right to left (probably after a certain revision level).
zsh also does this, but the first index of arrays is 1 , rather than 0 ..
bash requires version 4.3 or higher

1 Like

Here is one for all ksh versions:

#### here filename contain XXXX_YYYY_ZZZZ_DDD.txt as example
while read filename
do

### split filename on "."
extension="${filename##*.}"
[ "$extension" = "$filename" ] && extension=""
noextension=${filename%.*}
[ "$noextension" = "$filename" ] && noextension=""

### split noextension on "_" to AR[0] AR[1] ... AR[lenAR-1]
## ksh93 takes:
# IFS="_" read -A AR <<< "$noextension"
##
## ksh88 needs:
oIFS=$IFS
IFS="_"
set -f
set -A AR $noextension
IFS=$oIFS
set +f
##
lenAR=${#AR[@]}

### Conditions...
if
  [ "$extension" != "txt" -a "${AR[0]}" = "HELP" ] ||
  [ "$extension" != "csv" -a "${AR[0]}" != HELP ] ||
  [ "$ARR[2]" != "WE" ]
then
# UPDATE TABLE ########################
   echo rm "$filename"
fi

done

Remove the echo to run the rm command.