Exits from putty instead of shell script

Dear,

I have written below code to initiate the log at top of my script.

#Set the log file
LOGFILE=<path>/<filename.log>
exec > $LOGFILE 2>&1
...............
....
...
..
............
echo -e  "\n\n Script finished OK " `date "+%m/%d/%y %H:%M:%S" ` "\n\n"
exit 0

the logging ends only after giving 'exit 0' in the bottom, but this 'exit 0' also terminates and closes the unix session, but my need is to have the control in next command prompt.

Pls advice.

I believe this happens when you source the script.

if you run:

. /path/to/my/script

It will close the session.

If you run:

/path/to/my/script

It should work.

Notice the 'dot''space' in the first example. Let us know if this is the case.

Now, if this is a subscript, being called from a master script, simply take the exit out.

Doesn't "exec" start a new Shell?
Anyway, what output do you want to go into $LOGFILE ? There should be an alternative technique without using "exec".

What Operating System and Shell is this:

uname -a
echo $SHELL

Dear thx for ur reply.

exec does not start a new shell, but if i dont give exit 0 at the bottom it hangs (logs including the new promt)

$ uname -a
Linux jasmine 2.6.16.60-0.54.5-smp #1 SMP Fri Sep 4 01:28:03 UTC 2009 x86_64 x86_64 x86_64 GNU/Linux
$ echo $SHELL
/bin/bash

Yes i run using dot & space. it is individual program(but no problem when run via master program bcos master pgm run diff machine), i am just logging echo stuffs and other for debugging purpose.

Thanks
Imran

If you still need help, post all the programs you are using and how you are running them.

Try using "return" with dot notation -

$
$
$ cat -n f8.sh
     1  #!/usr/bin/bash
     2  echo "Hello, World!"
     3  return 1
$
$ # The following prints "Hello, World!", and returns control to the
$ # shell prompt with exit status 1
$
$ . f8.sh
Hello, World!
$
$ echo $?
1
$
$

tyler_durden

Hi,

The 'return' also leads to hang, and but log is ok including 4 prompt lines at the end.

Is there any alternate way to establish log without exec & exit.

I am using this logic for 4 inividual scripts (bash) that runs daily, two of them are for sqlldr and others calling SP., I have to login putty 4 time to accomplish this.

Thanks
Imran

Hi Methyl, it does not start a new shell, see: exec: description

Maybe if you could tell us what exactly you are trying to do in your script, we might come up with a few suggestions.

Interaction of the shell with Oracle doesn't have to be complicated. But you got to help us help you here.

tyler_durden

@Scrutinizer
Thanks.
Never found a use for " exec > $LOGFILE 2>&1 " but I see from the link you post how it might work but I cannot match the syntax used by the O/P with any of the examples.
We use "exec" to lock people into a shell menu (which does start a new shell). Any lines in the calling script below the "exec" line are never executed and the "drop-through" in the exec'd shell menu logs you out. That's what I though was happening to the O/P.

I am just trying to capture all the echo comments. The script includes zipping and mailing which turnouts some system messages like below

adding: file1.csv (deflated 92%)
adding: file2.csv (deflated 46%)

redirecting to some o/p file on each single stmt may not be appropriate. I tried to simulate in simple code but again it quits the session after execution...
pls see below

user@server:/path> cat test.sh
#!/bin/bash

LOGFILE=/logpath/testlog.`date "+%y%m%d" `.$$
exec > $LOGFILE 2>&1

echo Line 1

echo Line 2

exit
user@server:/path>
user@server:/path>. test.sh

it exits from the session and need to relogin to see the log

user@server:/logpath> cat testlog.110214.3496
Line 1
Line 2
user@server:/logpath>

hope now my req is clear, appreciate for any alternate idea to get rid of this concept

:wall:

Try:

chmod +x test.sh
./test.sh

You can just remove the exit statement by the way. It serves no purpose...

Thanks a lot it really works with exit stmt as well.

by the way whats difference in dot space and other one.

With dot space you are "sourcing" the commands in the script, which mean you execute them in your current environment, which is your interactive shell. The exit statement exits the shell which is your interactive shell, so your putty session gets terminated...

The other way is called executing and in this case a new shell is started in which the script is run. Once it exits, it only exits that shell and your interactive shell remains active...

Yep i understand, thanks again.