Link to home
Start Free TrialLog in
Avatar of magento
magento

asked on

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

Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

run the shell script in verbose mode and see where the code goes:

sh -x scripfiletname
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.
Avatar of magento
magento

ASKER

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
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
 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