if then else logic with while loop problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if then else logic with while loop problem
# 1  
Old 03-01-2011
if then else logic with while loop problem

Hi Friends,
I have to do write a shell file based on one flag.If that flag value is 'N' then process look in $DATA are and the normal process continue.If vaule is 'P' then it check for the files in different location $CONV and move those file in $DATA area and rest of the process continue.But if the vaule is 'J' then i have to look in both place i.e $DATA and $CONV area and then move the file from $CONV area to the $DATA area.
Now the problem is how to scan through bot are in one go(i.e) the 3rd condition.
Here is the code:-

----------------------------
Code:
  if   [ $CONV_FLAG= "'N'"]
      then
           PLACE_TO_CHECK=$DATA
      elif [ $CONV_FLAG= "'P'"]
    then 
           PLACE_TO_CHECK=$CONV       
      fi
while [ ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE01 -o ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE02 -o \
        ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE03 -o ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE04 -o \
        ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE05 -o ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE06 -o \
        ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE07 -o ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE08 -o \
        ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE09 -o ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE10 -o \
        ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE11 -o ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE12 -o \
        ! -f $PLACE_TO_CHECK/$AMPS_COMPLETE13 -o \
        ! -f $PLACE_TO_CHECK/$ADD_FILE -o ! -f $PLACE_TO_CHECK/$ANS_FILE -o \
        ! -f $PLACE_TO_CHECK/$CDQ_FILE -o ! -f $PLACE_TO_CHECK/$COR_FILE -o \
        ! -f $PLACE_TO_CHECK/$CMP_FILE -o ! -f $PLACE_TO_CHECK/$ISH_FILE -o \
        ! -f $PLACE_TO_CHECK/$JNL_FILE -o ! -f $PLACE_TO_CHECK/$JRC_FILE -o \
        ! -f $PLACE_TO_CHECK/$JRF_FILE -o ! -f $PLACE_TO_CHECK/$PRC_FILE -o \
        ! -f $PLACE_TO_CHECK/$PRH_FILE -o ! -f $PLACE_TO_CHECK/$PYH_FILE -o \
        ! -f $PLACE_TO_CHECK/$RDR_FILE -o ! -f $PLACE_TO_CHECK/$REV_FILE -o \
        ! -f $PLACE_TO_CHECK/$RTS_FILE -o ! -f $PLACE_TO_CHECK/$SRC_FILE -o \
        ! -f $PLACE_TO_CHECK/$CCF_FILE -o ! -f $PLACE_TO_CHECK/$CUR_FILE -o \
        ! -f $PLACE_TO_CHECK/$CVR_FILE -o ! -f $PLACE_TO_CHECK/$DSP_FILE -o \
        ! -f $PLACE_TO_CHECK/$GEO_FILE -o ! -f $PLACE_TO_CHECK/$ISC_FILE -o \
        ! -f $PLACE_TO_CHECK/$LET_FILE -o ! -f $PLACE_TO_CHECK/$PYM_FILE -o \
        ! -f $PLACE_TO_CHECK/$SBJ_FILE -o ! -f $PLACE_TO_CHECK/$SCT_FILE -o \
        ! -f $PLACE_TO_CHECK/$STS_FILE -o ! -f $PLACE_TO_CHECK/$SUB_FILE -o \
        ! -f $PLACE_TO_CHECK/$SVR_FILE -o ! -f $PLACE_TO_CHECK/$TMC_FILE -o \
        ! -f $PLACE_TO_CHECK/$TWN_FILE -o ! -f $PLACE_TO_CHECK/$USE_FILE -o \
        ! -f $PLACE_TO_CHECK/$PYF_FILE -o ! -f $PLACE_TO_CHECK/$PPY_FILE -o \
        ! -f $PLACE_TO_CHECK/$WRD_FILE -o ! -f $PLACE_TO_CHECK/$SDV_FILE -o \
        ! -f $PLACE_TO_CHECK/$JTC_FILE -o ! -f $PLACE_TO_CHECK/$RCL_FILE -o \
 ! -f $PLACE_TO_CHECK/$ACP_FILE -o \
        ! -f $PLACE_TO_CHECK/$ANF_FILE -o ! -f $PLACE_TO_CHECK/$COF_FILE -o \
        ! -f $PLACE_TO_CHECK/$ISF_FILE -o ! -f $PLACE_TO_CHECK/$PYF_FILE -o \
 $PMON_COUNT -eq 0 ]
do   
if   [ $NO_OF_PAUSES -gt $MAX_PAUSES ]
      then
           echo "\nLoad process now paused $NO_OF_PAUSES times - Job Cancelled"
           touch $LOGS/main/FAILED-dataload
    touch $LOGS/main/FINISHED-dataload
           $SCRIPTS/msg_fail.ksh
           exit 1
      fi
      if   [ $PMON_COUNT -gt 0 ]
      then
           echo "\nAMPS file(s) missing - load process sleeping for \c"
           echo "$SLEEP_MINS minute(s)"
      else
           echo "\nOracle Instance $ORACLE_SID not running - load process \c"
           echo "sleeping for $SLEEP_MINS minute(s)"
      fi
      sleep $PAUSE_TIME
      date
      PMON_COUNT=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep | wc -l`
      NO_OF_PAUSES=`expr $NO_OF_PAUSES + 1`
      echo "NO_OF_PAUSES  = $NO_OF_PAUSES"
done
if [ $CONV_FLAG= "'P'"]
    then 
           mv $CONV/$DATAFILE_PFX*.[Tt][Xx][Tt] $DATA      
 fi
 
if [ $CONV_FLAG= "'J'"]
  then
          PLACE_TO_CHECK_DATA=$DATA
          PLACE_TO_CHECK_CONV=$CONV 
    
while [ ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE01 -o ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE02 -o \
        ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE01 -o ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE02 -o \
        ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE05 -o ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE06 -o \
  ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE05 -o ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE06 -o \
        ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE07 -o ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE08 -o \
  ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE07 -o ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE08 -o \
        ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE09 -o ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE10 -o \
  ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE09 -o ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE10 -o \
        ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE11 -o ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE12 -o \
  ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE11 -o ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE12 -o \
        ! -f $PLACE_TO_CHECK_DATA/$AMPS_COMPLETE13 -o \
  ! -f $PLACE_TO_CHECK_CONV/$AMPS_COMPLETE13 -o \
        ! -f $PLACE_TO_CHECK_DATA/$ADD_FILE -o ! -f $PLACE_TO_CHECK_DATA/$ANS_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$ADD_FILE -o ! -f $PLACE_TO_CHECK_CONV/$ANS_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$CDQ_FILE -o ! -f $PLACE_TO_CHECK_DATA/$COR_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$CDQ_FILE -o ! -f $PLACE_TO_CHECK_CONV/$COR_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$CMP_FILE -o ! -f $PLACE_TO_CHECK_DATA/$ISH_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$CMP_FILE -o ! -f $PLACE_TO_CHECK_CONV/$ISH_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$JNL_FILE -o ! -f $PLACE_TO_CHECK_DATA/$JRC_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$JNL_FILE -o ! -f $PLACE_TO_CHECK_CONV/$JRC_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$JRF_FILE -o ! -f $PLACE_TO_CHECK_DATA/$PRC_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$JRF_FILE -o ! -f $PLACE_TO_CHECK_CONV/$PRC_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$PRH_FILE -o ! -f $PLACE_TO_CHECK_DATA/$PYH_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$PRH_FILE -o ! -f $PLACE_TO_CHECK_CONV/$PYH_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$RDR_FILE -o ! -f $PLACE_TO_CHECK_DATA/$REV_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$RDR_FILE -o ! -f $PLACE_TO_CHECK_CONV/$REV_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$RTS_FILE -o ! -f $PLACE_TO_CHECK_DATA/$SRC_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$RTS_FILE -o ! -f $PLACE_TO_CHECK_CONV/$SRC_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$CCF_FILE -o ! -f $PLACE_TO_CHECK_DATA/$CUR_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$CCF_FILE -o ! -f $PLACE_TO_CHECK_CONV/$CUR_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$CVR_FILE -o ! -f $PLACE_TO_CHECK_DATA/$DSP_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$CVR_FILE -o ! -f $PLACE_TO_CHECK_CONV/$DSP_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$GEO_FILE -o ! -f $PLACE_TO_CHECK_DATA/$ISC_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$GEO_FILE -o ! -f $PLACE_TO_CHECK_CONV/$ISC_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$LET_FILE -o ! -f $PLACE_TO_CHECK_DATA/$PYM_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$LET_FILE -o ! -f $PLACE_TO_CHECK_CONV/$PYM_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$SBJ_FILE -o ! -f $PLACE_TO_CHECK_DATA/$SCT_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$SBJ_FILE -o ! -f $PLACE_TO_CHECK_CONV/$SCT_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$STS_FILE -o ! -f $PLACE_TO_CHECK_DATA/$SUB_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$STS_FILE -o ! -f $PLACE_TO_CHECK_CONV/$SUB_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$SVR_FILE -o ! -f $PLACE_TO_CHECK_DATA/$TMC_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$SVR_FILE -o ! -f $PLACE_TO_CHECK_CONV/$TMC_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$TWN_FILE -o ! -f $PLACE_TO_CHECK_DATA/$USE_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$TWN_FILE -o ! -f $PLACE_TO_CHECK_CONV/$USE_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$PYF_FILE -o ! -f $PLACE_TO_CHECK_DATA/$PPY_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$PYF_FILE -o ! -f $PLACE_TO_CHECK_CONV/$PPY_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$WRD_FILE -o ! -f $PLACE_TO_CHECK_DATA/$SDV_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$WRD_FILE -o ! -f $PLACE_TO_CHECK_CONV/$SDV_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$JTC_FILE -o ! -f $PLACE_TO_CHECK_DATA/$RCL_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$JTC_FILE -o ! -f $PLACE_TO_CHECK_CONV/$RCL_FILE -o \
 ! -f $PLACE_TO_CHECK_DATA/$ACP_FILE -o \
 ! -f $PLACE_TO_CHECK_CONV/$ACP_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$ANF_FILE -o ! -f $PLACE_TO_CHECK_DATA/$COF_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$ANF_FILE -o ! -f $PLACE_TO_CHECK_CONV/$COF_FILE -o \
        ! -f $PLACE_TO_CHECK_DATA/$ISF_FILE -o ! -f $PLACE_TO_CHECK_DATA/$PYF_FILE -o \
  ! -f $PLACE_TO_CHECK_CONV/$ISF_FILE -o ! -f $PLACE_TO_CHECK_CONV/$PYF_FILE -o \
 $PMON_COUNT -eq 0 ]
do
   if   [ $NO_OF_PAUSES -gt $MAX_PAUSES ]
      then
           echo "\nLoad process now paused $NO_OF_PAUSES times - Job Cancelled"
           touch $LOGS/main/FAILED-dataload
    touch $LOGS/main/FINISHED-dataload
           $SCRIPTS/msg_fail.ksh
           exit 1
      fi
      if   [ $PMON_COUNT -gt 0 ]
      then
           echo "\nAMPS file(s) missing - load process sleeping for \c"
           echo "$SLEEP_MINS minute(s)"
      else
           echo "\nOracle Instance $ORACLE_SID not running - load process \c"
           echo "sleeping for $SLEEP_MINS minute(s)"
      fi
      sleep $PAUSE_TIME
      date
      PMON_COUNT=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep | wc -l`
      NO_OF_PAUSES=`expr $NO_OF_PAUSES + 1`
      echo "NO_OF_PAUSES  = $NO_OF_PAUSES"
done      
mv $DATA/$DATAFILE_PFX*.txt $DATA/$DATAFILE_PFX*.cnv
cat $CONV/$DATAFILE_PFX*.txt $DATA/$DATAFILE_PFX*.cnv >$DATA/$DATAFILE*.txt
rm $CONV/$DATAFILE_PFX*.txt
fi
echo "\nAll AMPS files in Data directory - starting Warehouse Dataload...\n"
# name the scripts
S0=load_txt.ksh
S1=merge_process.ksh

----------------------------------------------

Last edited by pludi; 03-01-2011 at 11:52 AM..
# 2  
Old 03-01-2011
Yes, a wrapper loop seems like the right way to go for one, the other or both. Is your while a loop or an if? All the file names to check should be in a variable (line feeds between is perfectly legal and good form), and applied in a 'for' loop. You should get more mileage out of ( cd some_dir, . . . ), where the parens make just that code run in the target dir, not the parent loop. Use more lines, and one item per line, formatted identically. Case is a great place to test, ensure all cases are covered and hang comments. Indent hierarchically, consistently. Put a blank line between structures. Then, it is much more readable and maintainable:
Code:
FILE_LIST="
   $AMPS_COMPLETE01
   $AMPS_COMPLETE02
 .
 .
 .
"
 
case $CONV_FLAG in
(P)
     places_to_check=$DATA
     ;;
(J)
     places_to_check="$CONV $DATA"
    ;;
(N)
     places_to_check=$CONV
    ;;
(*)
     echo "Bad input CONV_FLAG='$CONV_FLAG'" >&2
     exit 1
esac
 
for PLACE_TO_CHECK in $places_to_check
do
  (
  cd $PLACE_TO_CHECK
 
  for file in $FILE_LIST
  do
    .
    .
    .
  done
  .
  .
  .
  )
done


Last edited by DGPickett; 03-01-2011 at 02:43 PM..
# 3  
Old 03-01-2011
Hi Thanks for the reply.
In my while loop i am checking for the existance of the file (like $AMPS_COMPLETE01 etc ) and also the [$PMON_COUNT -eq 0 ].if any of the condition is not met then i do the wait or the error raising.
Now if $CONV_FLAG is passed as 'J' then how to compair all the file in both are (i.e $DATA and $CONV)Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk if logic problem

nawk '{ fmt="%3s %22s %48s %35s %21s\n"; if ($3==$6 && $1=="STOPLOSS") { tpy="Successful Match"; jnme=$1; sts="File will be loaded"; cntrl=$3; audit=$6; printf (fmt, tpy,jnme,sts,cntrl,audit) >> "'${AUDIT_DATA_FILE}/${AUDIT36}'" }else if ($3!=$6 && $1=="STOPLOSS") { tpy="Mis-Match ";... (4 Replies)
Discussion started by: wawa
4 Replies

2. Shell Programming and Scripting

Shell/Perl logic for loop

Hi, I have a requirement as follows. Have 3 files. Need to match up the data in each one of them and sum up the data by a field and display it. example given below. File 1 : Name, Emp id File 2 : Empid, Subject, File 3 : Subject, Score, Class Match Emp id in File 1 and File 2 and then... (7 Replies)
Discussion started by: preethgideon
7 Replies

3. Shell Programming and Scripting

while loop logic

Hi, Here I am trying to query database and check a value, if the value not matches then I wants to re-query the database.Once the value matches, I want to email the reqidstatus_log.txt file. Database query produces a file reqidstatus_log.txt which contains result. But the query not working as... (3 Replies)
Discussion started by: rajsp217
3 Replies

4. Shell Programming and Scripting

Please help me to figure out the logic for this problem

Hello, i am on 2.6.13-1.1526_FC4smp i have directory named /home , and in there i have directory 2009_10_10 and two files : 2009_10_10.log and 2009_10_11.log How to write shell script which will delete only file/s that don't have his/their 'parent' directory, so in this case , only... (7 Replies)
Discussion started by: tonijel
7 Replies

5. Shell Programming and Scripting

loop logic inside of an inline redirect?

i need to log the feedback from the ftp server as i'm performing some deletes. the only way i know of to do this is with the inline redirect << EOF ... but from there to the closing EOF, it's like i'm at the ftp command prompt, so I don't know how to have ksh script logic in there I have an... (3 Replies)
Discussion started by: tlavoie
3 Replies

6. Shell Programming and Scripting

for loop logic with multiple parameters

hi, unix wizards, i have a question about the logic of my inner for loop below. first, what i am trying to do is to write a script called create_account that automatically creates mysql accounts. the user can provide a user_name or a group_id as an argument (and the script can take multiple... (1 Reply)
Discussion started by: ankimo
1 Replies

7. Shell Programming and Scripting

While Loop Logic

I would need to with making while loop logic working in shell program when I am new into the shell programing 1) I would need to try to get the file from the remote side ----need to try 15 mins apart for 4 times and terminate the program if file is not available.... I would need to know how I... (4 Replies)
Discussion started by: sambakamba
4 Replies

8. Shell Programming and Scripting

Problem with if logic

Hi there, In my shell program I have two if statements the first one checks that the date in yyyymm format is equal to a date from the parameter file in yyyymm format, and that the file and sends an appropiate email The second one checks that the date in yyyymm format is equal to a... (1 Reply)
Discussion started by: rjsha1
1 Replies
Login or Register to Ask a Question