Nested If condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested If condition
# 1  
Old 10-22-2010
Nested If condition

Hi I have a requirement to create a 2 folder based on there existance

Code:
if [ -d "$var_name" ]
then 
cd $var_name
 if [ -d "$var_name3" ]
 then
 cd $var_name3
 mv -fi *.* $var_TargetPath/$var_name/$var_name3
 else 
 mkdir -p "$var_name3"
 chmod 755 "$var_name3"
 mv -fi *.* $var_TargetPath/$var_name/$var_name3
else
mkdir -p "$var_name/$var_name3"
chmod 755 "$var_name/$var_name3"
mv -fi *.* $var_TargetPath/$var_name/$var_name3
fi
fi


Error " syntax error near unexpected token `else'"
But the above script throws an error, Please help

Last edited by vgersh99; 10-22-2010 at 11:56 AM.. Reason: code tags, please!
# 2  
Old 10-22-2010
Try indenting your code. Then I think it will be obvious where the problem is.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-22-2010
between your 2 "else" you should have a "fi"

Code:
if [ -d "$var_name" ]
then
    cd "$var_name"
    if [ -d "$var_name3" ]
    then
        cd "$var_name3"
        mv -fi *.* "$var_TargetPath/$var_name/$var_name3"
    else
        mkdir -p "$var_name3"
        chmod 755 "$var_name3"
        mv -fi *.* "$var_TargetPath/$var_name/$var_name3"
    fi
else
    mkdir -p "$var_name/$var_name3"
    chmod 755 "$var_name/$var_name3"
    mv -fi *.* "$var_TargetPath/$var_name/$var_name3"
fi


Last edited by ctsgnb; 10-22-2010 at 02:03 PM.. Reason: gave to much double quote to DudeDGPickett
# 4  
Old 10-22-2010
Still I face the same problem.
# 5  
Old 10-22-2010
Quote:
Originally Posted by magesh_bala
Still I face the same problem.
Did you fix your script as per ctsgnb's suggestion ?

Are you saying that ctsgnb's script -

Code:
if [ -d "$var_name" ]
then
    cd $var_name
    if [ -d "$var_name3" ]
    then
        cd $var_name3
        mv -fi *.* $var_TargetPath/$var_name/$var_name3
    else
        mkdir -p "$var_name3"
        chmod 755 "$var_name3"
        mv -fi *.* $var_TargetPath/$var_name/$var_name3
    fi
else
    mkdir -p "$var_name/$var_name3"
    chmod 755 "$var_name/$var_name3"
    mv -fi *.* $var_TargetPath/$var_name/$var_name3
fi

ends up with this error ?

Code:
Error " syntax error near unexpected token `else'"

tyler_durden
# 6  
Old 10-22-2010
Quote:
Originally Posted by magesh_bala
Still I face the same problem.
try double quote everything see red update of my previous post
# 7  
Old 10-22-2010
@ctsgnb: there shouldn't be double quotes around the *.*
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Nested If

I am having a problem with a nested if. I am sure I am overlooking something. I check for the existence of $Pidfl3 and it exists, o this condition I then want to check for the existence of a next file and remove it. The first if is executed, but on the second if I get test: argument expected. My... (4 Replies)
Discussion started by: Charles Swart
4 Replies

3. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Nested if else

Hi, i m trying to create script which logic is like below. if ; then x=`cat /tmp/testoutput.log | grep STOP | wc -l` y=`cat /tmp/testoutput.log | grep RUN | wc -l` if ; then echo "process stop" if ; then echo "process running " else echo "file not found" fi ----------------... (2 Replies)
Discussion started by: tapia
2 Replies

5. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

Nested if in KSH

Trying to clean up the last little thing in this script which is that the mv always performs no matter whether it is needed or not. It doesn't cause any bugs just prints an unsightly "can't mv source and dest are the same". Obviously I want to get rid of it but having issues getting the... (4 Replies)
Discussion started by: Calbrenar
4 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. Shell Programming and Scripting

nested variables

Is there any way to do variable nesting using sh? For example: example_1="a test string" example_2="another test" example_3="etc..." i=2 echo ${example_$i} The shell reports: sh: ${example_$i}: bad substitution If not, maybe someone could suggest another method. Thanks in... (3 Replies)
Discussion started by: kevinl33
3 Replies
Login or Register to Ask a Question