Recently on the Fediverse I posted a puzzle about a little shell script :
A silly little Unix shell thing that I've vaguely wanted for ages but only put together today. See if you can guess what it's for:
#!/bin/sh
trap 'exec $SHELL' 2
"$@"
exec $SHELL(The use of this is pretty obscure and is due to my eccentric X environment.)
The actual version I now use wound up slightly more complicated, and I call it 'thenshell'. What it does (as suggested by the name) is to run something and then after the thing either exits or is Ctrl-C'd, it runs a shell. This is pointless in normal circumstances but becomes very relevant if you use this as the command for a terminal window to run instead of your shell, as in '
xterm -e
thenshell <something>
'.
Over time, I've accumulated a number of things I want to run in my eccentric desktop environment , such as my system for opening URLs from remote machines and my alert monitoring . But some of the time I want to stop and restart these (or I need to restart them), and in general I want to notice if they produce some output, so I've been running them in terminal windows. Up until now I've had to manually start a terminal and run these programs each time I restart my desktop environment, which is annoying and sometimes I forget to do it for something. My new 'thenshell' shell script handles this; it runs whatever and then if it's interrupted or exits, starts a shell so I can see things, restart the program, or whatever.
Thenshell isn't quite a perfect duplicate of the manual version. One obvious limitation is that it doesn't put the command into the shell's command history, so I can't just cursor-up and hit return to restart it. But this is a small thing compared to having all of these things automatically started for me.
(Actually, I think I might be able to get this into a version of
thenshell
that knows exactly how my shell and my environment handle history, but it would be more than a bit of a hack. I may still try it, partly because it would be nifty.)