Exit is not working in shellcode

Hi,

I will check if directory exists, if yes it should run the copy command.
If copy fails,then it should echo "Failed" and exit the script.
But it not exiting and run the next shell script.

I also tried below
		( cp "DIRECTORY"/* "ADIRECTORY" && echo copy; ) || ( echo "Failed" && exit 1)

Open in new window

but it also not working as expected.When the cp operation success it should echo copy and run product.sh ,when cp operation failed it should echo Failed and exit the script and not running product.sh

 Maincode:
if [[  -d DIRECTORY && -d ADIRECTORY ]]; 
	then
		( cp "DIRECTORY"/* "ADIRECTORY" && echo copy; ) || ( echo "Failed" ; exit 1;)
		 source product.sh ;
	else
        echo "Dir not found ";
		exit 1;
fi	

Open in new window

LVL 5
magentoAsked:
Who is Participating?
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Just leave out the ( ) at the end:

( cp "DIRECTORY"/* "ADIRECTORY" && echo copy; ) || echo "Failed" && exit 1

If the part before || fails, the part after that is executed. When the echo succeeds (it normally does) the exit command is executed.
0
 
omarfaridCommented:
run the shell script in verbose mode and see where the code goes:

sh -x scripfiletname
0
 
arnoldCommented:
Since you are using or, once the first part is true, the second is not evaluated.



You should use the cp as the condition of an IF statement.
Not sure what you are looking at.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
magentoAuthor Commented:
Hi,

I run as bash -x script.sh

It goes to line exit 1, but not exiting.
+++echo failed
++exit 1
++source product.sh

Thanks
0
 
TintinCommented:
You are running the command in a subshell, so the exit will only exit the subshell.

There's a few different ways of doing it.

if [[  -d DIRECTORY && -d ADIRECTORY ]]
then
     if cp "DIRECTORY"/* "ADIRECTORY"  
     then
           echo Copy
           source product.sh 
    else
          echo "Failed"
          exit 1
    fi
else
     echo "Dir not found "
     exit 1
fi	

Open in new window

0
 
arnoldCommented:
If you wanted to have the execution of product.sh if one of the conditions was met add an && 

Cp && source product.sh if you still need the echo responsess.

Any reason why an absence of either directory you not use mkdir -p to create them?
0
 
arnoldCommented:
 Maincode:
if [[  -d DIRECTORY && -d ADIRECTORY ]]; 
	then
		( cp "DIRECTORY"/* "ADIRECTORY" && echo copy; ) &&   source product.sh ;
	else
        echo "Dir not found ";
		exit 1;
fi	

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.