0

Here is the situation:

I have a shell script - script.ksh

I am calling it from a java program and I need the stdout from the script in the java program.

I also need to redirect the output to a file and if the script fails at any point, I have to send a mail with the file attached to it.

so the file must be closed before the script exits so that the mailer can send it.

I have got everything working right.

Except for 1 thing - if there is any 'cd' statement in the script the file doesn't get closed before the script ends. Please help me out here

Not sure why this is happening.

Here's a simplified version of the code.

#!/bin/ksh
#Need to redirect output to file and stdout
exec 6>&1  #save stdout in [6]
exec 1>shell.log 2>&1  #redirect stderr and stdout to file


#if any error happens reset stdout and print the log contents
trap '
echo "Error Trap";
exec 1>&- 1>&6 6>&-;
echo | cat shell.log>&1;
echo "send the log file by mail";
exit 1' ERR

#cd / #Uncomment and the script behaves differently
echo "some task"
./somescript.ksh   #this requires me to cd to its directory and then execute

trap - ERR  #End the ERR trap


#in case of normal exit print the log contents
trap '

echo "Normal Exit"
exec 1>&- 1>&6 6>&-;
echo | cat shell.log>&1;
exit 1' EXIT
2
  • built in commands have problems when executed from java... probably your problem is around that... Commented Feb 4, 2011 at 6:07
  • I do not get any different results when i execute from prompt or java. behaviour changes if i comment the cd command Commented Feb 6, 2011 at 5:29

1 Answer 1

1

You are opening files in the current directory and then changing directories. Try using absolute paths for those files.

Why are you piping echo through cat? It has no effect in your script. Just cat the file. There's no need to redirect stdout to stdout either. cat file is the same as cat file >&1.

Why are you doing exit 1 for a normal exit. I should be exit 0.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for pointing out the exit and echo cat mistakes. using absolute paths does work. But I can't really use absolute paths since i'm calling another script from my script, and the other script uses relative paths. and I can't change the other script. If there is any simple workaround then please tell otherwise I have got a workaround at the java end. but if i could just make the cd work the way i want it would save me lot of work... is this problem being caused by subshells being created by cd? Thats what google results point to.
@Sougata: I assume you can't change the other script. If that's the case, then you need to specify a full absolute path to shell.log. cd doesn't create any subshells. You're just in a different directory than where you started and you're trying to close a file that's not in the new directory. Try using pushd instead of cd then you can do popd right before you close the file in the trap and at the end. Also, it's not necessary to set an EXIT trap right before you exit - just execute those commands directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.