0

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

Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41
  • The lines after `poetry shell` will be executed by Bash _after you exit_ the Poetry shell. You want to pass those lines as input to Poetry. This is a common duplicate. – tripleee Apr 28 '23 at 04:00
  • If your shell is [tag:zsh], this is a [tag:zsh] question, not a [tag:bash] question. The two are mutually incompatible (though both have a core which supports portable standard POSIX `sh` features) – tripleee Apr 28 '23 at 04:03
  • ... though the part about how to pass input to an external command is generic. – tripleee Apr 28 '23 at 04:25

1 Answers1

0

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.

xpusostomos
  • 1,309
  • 11
  • 15