Problem with if statement, cannot get to right directory

I have several scripts , which are calling one. That script, depends of arguments, processing daily, weekly and monthly files. After process daily, I started process weekly

 exec ${LOCL_WORKING_DIR}/sftp_ondemand.sh /export/data/mbsesb/config/ondemand.cfg /export/data/mbsesb/config/filename.lst W
 

in the calling script I have lines:

 if [ -d ${COOLDIR}/cool_${Today}/daily ] && [ "${CASENAME}" = "D" ]
 then
 for...
 do
 do something
 done
 fi
 if [ -d ${COOLDIR}/cool_${Today}/weekly ] && [ "${CASENAME}" = "W" ];then
 for ...
 do
 do something
 done
 fi
 if [ -d ${COOLDIR}/cool_${Today}/monthly ] && [ "${CASENAME}" = "M" ];then
 for
 do
 do something
 done
 fi
 

If daily directory created and I start weekly, it is anyway getting to daily directory.
Where Am I wrong?

Thanks for contribution

Please translate:

to English and explain what you are trying to do.

The code you have shown us does absolutely nothing if the three directories it is looking for do not exist (assuming we get past the syntax errors). If the directories do exist, you have three do loops with no matching done s.

If the code you have shown us in your script is placed after the exec command that processes your daily, weekly, monthly files; it will NEVER be run anyway.

This script is called from another script , which using

exec

command.
Do0 you mean I don't have to use it?

---------- Post updated at 04:55 PM ---------- Previous update was at 04:42 PM ----------

I will try to explain you.
I have a script, which

 tar and zip files on remote server when three directories, daily, weekly, monthly
 

The script went to weekly directory

 tar, zip files,exit to another server, on which it scp ziped file to our weekly directory, unzip and untar files and
 run another script:
 ${LOCL_WORKING_DIR}/sftp_ondemand.sh /export/data/mbsesb/config/ondemand.cfg /export/data/mbsesb/config/filename.lst W
  
 

sftp_ondemand.sh script is supposed to start, cd, to weekly directory on this server and rename files.
However,

if [ -d ${COOLDIR}/cool_${Today}/daily ] && [ "${CASENAME}" = "D" ]
then
rename daily files, start another script with daily files, also rename
fi
if [ -d ${COOLDIR}/cool_${Today}/weekly ] && [ "${CASENAME}" = "W" ]
then
rename weekly files

fi
if [ -d ${COOLDIR}/cool_${Today}/weekly ] && [ "${CASENAME}" = "M" ]
then
rename monthly files
fi

when I work with weekly files, it is anyway goes to the first if statement and create daily files on weekly directory. Which I don't want to do. I want it to go to the second if statement, but it goes to first, even condition doesn't exists.

Now I explained better?

I mean I don't see any relationship between the code you have shown us and the question I think you are asking.

  1. You need to explain what you are trying to do.
  2. You need to explain what is not working.
  3. You need to show us the code that is not working.
  4. You need to show us the output produced by the code you're using (including any diagnostic messages).
  5. You need to show us the output you want your code to produce.

Note that:

 for...
 do
 do something
 done

is NOT very useful when trying to diagnose errors in code. All we can say is that the 2nd do does not have a matching done and that ... and something might be the source of your problem.

And, showing us code in one script and asking whether or not another script (whose code you have not shown us) can figure out how to do something else baffles me.

I just explained you:
I have three if statement with and condition:

 if [ -d "${COOLDIR}/cool_${Today}/daily" ] && [ "${CASENAME}" = "D" ]
 then
       echo "Daily dir"
 fi
 if [ -d "${COOLDIR}/cool_${Today}/daily" ] && [ "${CASENAME}" = "W" ]
 then
      echo "weekly dir"
 fi
 if [ -d "${COOLDIR}/cool_${Today}/daily" ] && [ "${CASENAME}" = "M" ]
 then
      echo "monthly dir"
 fi
 

It is brief. It doesn't matter, what it does inside.
Everything I want,

 if CASENAme = "W", print weekly dir
 But it prints daily dir
 

---------- Post updated at 05:19 PM ---------- Previous update was at 05:10 PM ----------

The script inside each if statement has 1000 lines, shows no errors. It just goes to incorrect if, even directory doesn't exist and I supply CASENAME "W", it goes under first IF

You think your W argument goes into the CASENAME variable.
But this is obviously not the case - CASENAME has the value D .

As I suggested in your other thread, add set -x to the top of your script and run the script.
This will output what script is doing and how the variables are expanded - this is the usual debug technique you should get accustomed to.

I will try
Script call:

 ${LOCL_WORKING_DIR}/sftp_ondemand.sh /export/data/mbsesb/config/ondemand.cfg /export/data/mbsesb/config/filename.lst W
 

inside the script

 case ${3} in
        D)
                cd ${COOLDIR}/cool_${Today}/daily
                ;;
        W)
                cd ${COOLDIR}/cool_${Today}/weekly
                ;;
        M)
                cd ${COOLDIR}/cool_${Today}/monthly
                ;;
esac
echo "CASENAME == ${CASENAME}"
PWD=`pwd`
echo "PWD == $PWD"
if [ -d "${COOLDIR}/cool_${Today}/daily" ] && [ "${CASENAME}" = "D" ]
then
     for file in *_${Today}*.csv *_${Today}*.txt
do
 echo "Start woprking with ${file} to prepare rename them to Ondemand format"
        firstpreffix=`echo ${file} | cut -d'_' -f 1`
        ext=`echo $file | rev | cut -d'.' -f 1 | rev`
        echo "firstpreffix $firstpreffix"
        echo "EXT = $ext"
echo "end of for loop"
 done
 exec ${LOCL_WORKING_DIR}/brkvol_day_sftp_ondemand.sh
 fi
 if [ -d "${COOLDIR}/cool_${Today}/weekly" ] && [ "${CASENAME}" = "W" ];then
for file in *_${Today}*.csv
do
        echo "Start woprking with ${file} to prepare rename them to Ondemand format"
        filename=`echo "${file}" | awk -F'_' '{for (i=1; i<NF; i++) printf("%s_", $i)}'`
        echo "filename $filename"
done
 fi
 if [ -d "${COOLDIR}/cool_${Today}/monthly" ] && [ "${CASENAME}" = "M" ];then
 for file in *.${Today}*.csv *.${Today}*.txt
do
        echo "Start woprking with ${file} to prepare rename them to Ondemand format"
        filename=`echo $file | cut -d'.' -f 1`
echo "filename = ${filename}"
        ext=`echo $file | rev | cut -d'.' -f 1 | rev`
done
 fi
 

It prints

 echo "CASENAME == W}"
PWD=`pwd`
echo "PWD == /xxxx/xxxx/xxxx/xxxx/weekly"
Start woprking with *.20170309*.csv to prepare rename them to Ondemand format
 firstpreffix = *.20170309.csv
 ext = csv
 end of for loop
 

When I expect it to go to the second if

 Start working with 21688-1_UsrAccChgRpt_20170309.csv to prepare rename them to Ondemand format
 filename 21688-1_UsrAccChgRpt_
 

No error messages.
Now is better?

---------- Post updated at 05:36 PM ---------- Previous update was at 05:33 PM ----------

I did it, doesn't show an error or mistake. I print

 CASENAME = W 

that's really "nice and informative" seeing more of your code, but it's not really helpful (at least to me).
Have you tried to follow the previously mention suggestion of adding set -x to the script and following the execution?
I'm not sure how *I* can help if I can keep seeing more code.....

I did it, nothing is working

thanks for sharing - this is very "informative".
Care to show the output of set -x "not working"?

You can tell us the code doesn't matter all you want to. But we don't believe you. All three of your if statements above test whether or not the daily directory is there. Earlier posts had your three if statements testing different directories (but, usually not testing three different directories). The code above will print no more than one of Daily dir , weekly dir , or monthly dir . It will NEVER print daily dir (with a lower case leading d ).

And there is a HUGE difference between CASENAme , $CASENAme , $CASENAME , $CaseName , and any other differences in upper and lower case letters in strings and (when there is a leading $ , variable names).

Every time you give us an excerpt of your code, little details change. Those little details greatly affects the way your code works, and every typo greatly affects our ability to guess at what is wrong.

I would have to assume that one or more of the typos that we see in the excerpts you have posted here are indicative of mistakes in your thousands of lines of code and that one or more of those mistakes is what is causing your problem. A missing $ when you intended to perform a variable expansion; a misspelled variable name; using daily , or weekly , or monthly when you intended to use one of the others; et cetera can all cause the problems you have described and are consistent with discrepancies between what you say is code that you are running and output that you say is being produced by that code.

It has been suggested several times that you trace your code while it is running with set -x (which is a great idea). If you would do that and show us (accurately) the code you are running and the output produced (accurately), we might be able to help you.

With the inconsistencies between the code segments you have shown us and the output you say those code segments produce, I don't see much hope of anyone being able to diagnose your problem. I wish you luck, but I don't see any way that I can help you further on this topic.

3 Likes