Problem with if statement, cannot get to right directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with if statement, cannot get to right directory
# 1  
Old 03-09-2017
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
Code:
 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:
Code:
 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
# 2  
Old 03-09-2017
Please translate:
Quote:
If daily directory created and I start weekly, it is anyway getting to daily directory.
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 dones.

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.
# 3  
Old 03-09-2017
This script is called from another script , which using
Code:
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
Code:
 tar and zip files on remote server when three directories, daily, weekly, monthly

The script went to weekly directory
Code:
 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,
Code:
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?
# 4  
Old 03-09-2017
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:
Code:
 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.
# 5  
Old 03-09-2017
I just explained you:
I have three if statement with and condition:
Code:
 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,
Code:
 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
# 6  
Old 03-09-2017
You think your W argument goes into the CASENAME variable.
But this is obviously not the case - CASENAME has the value D.
# 7  
Old 03-09-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with If statement

Hi All, I am writing an if statement to check multiple conditions, but when I try to execute the script it is breaking at the point of if statement by showing the issue below. Code I am using is given below. if -a ] then .... else ... fi I am not understanding... (3 Replies)
Discussion started by: ginrkf
3 Replies

2. Shell Programming and Scripting

Problem if statement

echo "Enter the variable: " " read var1 echo " " for i in ib eb atm do if ; then mv properties environment.properties break else echo "No changes to $var1 " fi done When i run and enter the eb it's not working.Any suggestions please.. (7 Replies)
Discussion started by: bhas85
7 Replies

3. UNIX for Dummies Questions & Answers

Having problem with if statement

Could someone help me out with this if statement? It's supposed to get a person's website, but it isn't working when I run it. website="" echo "Would you like to enter a website? Enter Yes/No" read choice if then while do echo "Please enter a website:"; read... (4 Replies)
Discussion started by: Sotau
4 Replies

4. Shell Programming and Scripting

problem with if/while statement

I'm trying to have the script check if a file has data or not, and then process it accordingly. If the file is empty, I want it to return "nothing to do", if not, I want it to process the file line by line. This is what I have so far, but it always returns "nothing to do", even if the file is not... (4 Replies)
Discussion started by: ddrew78
4 Replies

5. Shell Programming and Scripting

if statement problem

Hi I have a bash script like this if then echo "A" else echo "B" fi $1 is something like 02350 (there is always a trailing '0') and I would like to have an if based on the value of the digits after the 0. Can anybody help? Thanks, Sarah (3 Replies)
Discussion started by: f_o_555
3 Replies

6. UNIX for Dummies Questions & Answers

if statement problem

See https://www.unix.com/shell-programming-scripting/96846-if-statement-problem.html (0 Replies)
Discussion started by: f_o_555
0 Replies

7. Shell Programming and Scripting

If Statement Problem..

The problem I am having here is that only the 1st option is executed, no matter if I pick yes or no. What am I doing wrong? How can I get this working right without resorting to a case statement? echo "This is the max size your lvol can be:" echo $MAXSIZE echo echo Do you want to max out... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

8. UNIX for Dummies Questions & Answers

if statement problem

hi all. i just have a very small problem. i have a menu of 7 choices. i want an if statement so that if the user chooses anything except inside the 1 to 7 range, i can handle the error for it. i tried this: if ] then ....... fi (but it dont work) ...any suggestions? ... (4 Replies)
Discussion started by: djt0506
4 Replies

9. Shell Programming and Scripting

problem with an IF statement

I need an IF statement that will compare the contents of the variable CX with the actual string "CP". ie. If the contents of $CX are NOT equal to the actual string "CP" then blah blah blah. I have tried a number of things including the following....... if ]; then if ]; then if ];... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

10. UNIX for Dummies Questions & Answers

if statement problem

I keep getting an error at line 21, it doesn't like my if statement. Previously I have tried using (( )), but still get errors. The current error is that server_busy is not found. This is the script: #! /bin/ksh server_busy="na" for file in $1 $2 $3 $4 $5 $6 do echo " ${file}\t\c" ... (1 Reply)
Discussion started by: coughlin74
1 Replies
Login or Register to Ask a Question