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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.