How to exit from shell script if above condition fails?

HI

cd ${back_home}

if above back_home does not exist, then script shoul exit. Please let us know how to do that

[[ $? -eq 1 ]] && exit $?

Exits if previous command failed

[[ ! -d "/path/to/dir" ]] && exit 1

Exit if path doesnt exists

hth

do i need to mention any if conditions?

If $back_home is not defined then the command cd ${back_home} would evaluate to just cd (since "back_home" would evaluate to nothing). This would indeed take you "back home" (as in to your home directory - since cd with no arguments is equivalent to cd $HOME).

If you want to test if a variable is defined, you should use the test command!

test $back_home || exit

we have $back_home variable is defined but problem is our code is like this

BACK_HOME is under NFS file system, today we have NFS file system was down
so script skips cd ${BACK_HOME} and deleted all jar files from root.

so i need to see if BACK_HOME not available then script should exit

below is the script

cd ${BACK_HOME}

# Remove old files here.
/usr/bin/find . -ctime +/-1 -name "*.jar" -print -exec /usr/bin/rm -rf {} \;

This is a perfectly good example of how perfectly badly a question can be asked. Did you really think your problem was somehow related to that variable? Did you somehow magically expect us to work out it might have been an NFS problem?

how to send email for below condition

[[ ! -d "/path/to/dir" ]] && exit 1

Now your questions are all scatterbrained.

Thread closed.