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
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
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