Project scenario:
I built a container in docker. I want to automatically run a shell script when opening the container, prompting to enter the conda environment I want to use. These optional conda environments have already been built (5 in total). The specific shell script is as follows
#!/bin/bash
while true
do
echo "##########################################"
echo "#######welcome to deeplearningenv#########"
echo "###please select the env you want to use##"
echo "#########(e).exit the current env#########"
echo "#########(q).quit the menu################"
echo "##########1.pytorch1.6.0##################"
echo "##########2.pytorch1.8.0##################"
echo "##########3.tensorflow1.14.0##############"
echo "##########4.caffe#########################"
echo "##########5.mxnet#########################"
echo "##########################################"
read -p "Please enter your choice: " choice
case $choice in
1) source activate pytorch1.6.0;break;;
2) source activate pytorch1.8.0;break;;
3) source activate tensorflow1.14.0;break;;
4) source activate caffe;break;;
5) source activate mxnet1.7.0;break;;
e) conda deactivate;;
q) break;;
*) echo "Invalid choice. Please try again.";;
esac
done
After writing the above code into the start.sh script. Put the script root
in the directory and modify .bashrc
the file, adding at the end
bash /root/start.sh
Problem Description
When running the script, after typing in the sequence number corresponding to the selected environment, the environment is not entered, as shown in the figure below, it directly jumps out of the environment.
Cause Analysis:
When executing source activate pytorch1.6.0
the command , it modifies the environment variables of the current shell to use the commands and programs installed in the pytorch1.6.0 environment. These environment variables include PATH, LD_LIBRARY_PATH, PYTHONPATH, etc., which will be set to point to the corresponding directories and files in the pytorch1.6.0 environment. However, when shell
a script , the script runs in its own subshell instead of the current shell. Therefore shell
, executing source activate pytorch1.6.0
a command in a script will not modify the current shell's environment variables, because it will only take effect in the script's subshell.
solution:
In order to activate a Conda environment in a shell script, we can use the following two methods:
Method 1 : Use the command instead
in the script . This is a new way of activating a Conda environment without using the source command, introduced in versions after Conda 4.4, so it can be used in shell scripts. conda activate pytorch1.6.0
source activate pytorch1.6.0
Method 2
Use source activate
the command , but use the bash -l or source command to force the script to run in the current shell instead of a subshell when executing the script. For example:
bash -l start.sh
or
source /root/start.sh
This will cause the script to run in the current shell, allowing the source activate command to modify the current shell's environment variables. But also note that this approach may affect the state of the current shell, so think carefully before executing