How to exit a poetry shell within a shell script [duplicate]

I have the following workflow which I would like to automate (for Unix):

cd repos/data-warehouse poetry shell do my stuff exit (to deactivate the poetry shell) cd .. cd etl poetry shell 

For that, I've created the following shell script, which I have into a .sh file:

#!/bin/bash REPO_NAME="$1" TARGET_DIR=~/repos/$REPO_NAME if [ -n "$VIRTUAL_ENV" ]; then exit fi cd $TARGET_DIR poetry shell 

So that every time I run an alias (i.e fdev), i would just execute fdev data-warehouse and it would execute all the necessary commands to be in that folder with the poetry shell activated.

To make the alias, i've added this line to my nano ~/.zshrc file:

alias fdev="source ~/repos/fdev.sh" 

According to some links, like call "exit" from within shell script, Exit shell script from python and https://superuser.com/questions/1547228/how-to-activate-python-virtualenv-through-shell-script, using the source should work because the exit command will be executed within the same shell.

However, if I already have a poetry shell activated and I run the command from the terminal (i.e fdev etl, it just exits the poetry shell and the shell script stops, and I have to execute the command again to make it effectively change the directory and activate the new poetry shell.

Is there any way of accomplishing what I want to do? Thanks

3

1 Answer

Of course it does that because you have code to do that:

if [ -n "$VIRTUAL_ENV" ]; then exit fi 

If what you're saying is you want to be able to execute the same command INSIDE the subshell as you can outside the subshell, which has it exit the subshell and then enter a new shell, that's really tricky because you want to make the higher level shell do something for you.

All I can think of off hand is something like this:

#!/bin/bash REPO_NAME="$1" TARGET_DIR=~/repos/$REPO_NAME if [ -n "$VIRTUAL_ENV" ]; then echo "$0 $*" >~/poetrycmd.sh exit fi cd $TARGET_DIR poetry shell if [ -f ~/poetrycmd.sh ] then source ~/poetrycmd.sh rm ~/poetrycmd.sh fi 

So basically what happens is if you're in a virtual poetry shell, and you call the command, it writes the command to a file, then exits. When it exits the upper level shell looks if the file exists, and if so executes it from that shell, before removing the temporary command file. So the temp command file allows you to pop out of the sub shell and have it pop straight back into another subshell.

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobm9pYmmGd4COoaawZaSkeqbEyK1kmmWgpLK1vthmqqGdnKF6uLXToaCnZZFiwKmxy6VkrJuinr21