7

I have a situation where in I have a command in my shell script that must be executed after a exit command is executed in the same script (I know!! It sounds crazy!!)

I want to find a solution for something like

#!/bin/sh
ls -l ~/.
exit $?
touch ~/abc.txt

Here I want the command touch ~/abc.txt execute after exit $? has run and the command touch ~/abc.txt can be any command.

Constraints: 1) I cannot modify the exit $? part of the above script. 2) The command must be executed only after the exit $? command.

I'm not sure if there is a solution for this but any help is appreciated.

latestVersion
  • 458
  • 1
  • 6
  • 17
  • May we know why you cannot modify those first lines ? – arkascha Jul 27 '12 at 12:07
  • Sorry!! I have edited the constraints part of my question now. I can change anything in the script but the exit command has to stay!! – latestVersion Jul 27 '12 at 12:20
  • If you cannot modify the script, how do you expect to add the `touch` command? If you _can_ modify the script, why can't you add it above the `exit`? – Shahbaz Jul 27 '12 at 12:25
  • This does not lead anywhere, you are obviously just trying to change symptoms instead of dealing with the problem. What are you trying to achieve ? Why do you want to do that ? – arkascha Jul 27 '12 at 12:29

4 Answers4

10

The typical approach is to set a trap:

trap 'touch ~/abc.txt' 0

This will invoke the touch command when the shell exits. In some shells (eg bash), the trap will also be executed if the script terminates as the result of a signal, but in others (eg dash) it will not. There is no portable way to invoke the command only if the last command was exit.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

I don't know why you want to do something like that but maybe try something like this: wrap your script in another one. In your parent script evaluate your child script with command like eval or source, then extract from your child script last command and execute it separately same way.

Nykakin
  • 8,657
  • 2
  • 29
  • 42
  • As much as I want to, I cannot use a wrapper on my script as I'm modifying "tcruntime-ctl.sh" script of vFabric tcServer. – latestVersion Jul 27 '12 at 12:22
  • I am bound to modify only this script now! :( – latestVersion Jul 27 '12 at 12:24
  • @latestVersion Why can't you modify that one ? – arkascha Jul 27 '12 at 12:27
  • @latestVersion It' a crazy idea but maybe put that evaluating command in the same script. Something like exec(all other lines of this script) and then exec last one. – Nykakin Jul 27 '12 at 12:34
  • I cannot mess with some parts of the script and the `exit $?` is one of them. I have a situation where I get an "output" when this `exit $?` gets exectued. This generated "output" is not under my control and is part of a Java program. Now, I want to be able to run a command after the `exit $?` command gets executed and it has to be part of the same script as I'm constrained from introducing anything else into the project. I'm tied!! – latestVersion Jul 27 '12 at 13:31
0

I doubt the direct way is possible in any way.

But there might be a workaround: can you wrap the executing script in another process ? Then you could call anything you want in the wrapping script once the wrapped script executed its exit() and is removed from the stack.

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

This should work, even if OP didn't want to move the exit.

#!/bin/sh
ls -l ~/.
RES=$?     #save res for later.
touch ~/abc.txt
exit $RES
Leif Neland
  • 1,416
  • 1
  • 17
  • 40