Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a shell become interactive in the middle of a script?

I'd like to do something like:

do lots of stuff to prepare a good environement
become_interactive
#wait for Ctrl-D
automatically clean up

Is it possible with bash? If not, do you see another way of doing the same thing?

like image 863
static_rtti Avatar asked Dec 30 '22 03:12

static_rtti


2 Answers

Structure it like this:

test.sh

#!/bin/sh
exec bash --rcfile environ.sh

environ.sh

cleanup() {
    echo "Cleaning up"
}
trap cleanup EXIT
echo "Initializing"
PS1='>> '

In action:

~$ ./test.sh 
Initializing
>> exit
Cleaning up
like image 111
Josh Lee Avatar answered Dec 31 '22 17:12

Josh Lee


You can invoke another shell in the middle of the script, but changes to e.g. environment variables would not be reflected outside of it.

like image 21
Ignacio Vazquez-Abrams Avatar answered Dec 31 '22 16:12

Ignacio Vazquez-Abrams