Issue
I'm starting to code an installation shell script. Therefore, I want to check that I've the right Conda environment either active or it exists. But I have some issues, every time when the right environment is active there do not detect it inside the 'if' requested.
That is my piece of code for this functionality:
#!/bin/bash
desired_env="gnuradio"
# Check if the desired environment exists
if conda env list | grep -q "^$desired_env "; then
# Check if the desired environment is active
if conda info --envs | grep -q "^\*.*$desired_env"; then
echo "The '$desired_env' environment is currently active."
else
echo "Error: The '$desired_env' environment should be active."
echo "Please activate the '$desired_env' environment using:"
echo "conda activate $desired_env"
read -p "Do you want to activate environment $desired_env? ([Y]/n): " choice
if [[ "$choice" == "y" || -z "$choice" ]]; then
conda activate $desired_env
else
exit 1
fi
fi
else
read -p "The '$desired_env' environment does not exist. Do you want to create it? ([Y]/n): " choice
if [[ "$choice" == "y" || -z "$choice" ]]; then
conda create -n $desired_env
echo "The '$desired_env' environment has been created. Please activate it using:"
echo "conda activate $desired_env"
else
exit 1
fi
fi
And also some further information from my terminal, where you can see that I'm already had the right environment activated:
(gnuradio) X@X:~/Documents/Temp$ conda info --envs
# conda environments:
#
base /home/X/miniconda3
gnuradio * /home/X/miniconda3/envs/gnuradio
tf /home/X/miniconda3/envs/tf
(gnuradio) X@X:~/Dokumente/Temp$ bash test.sh
The 'gnuradio' environment does not exist. Do you want to create it? ([Y]/n):
Maybe someone had some ideas to fix that problem. Thank u!
Solution
I've fixed it myself, for other people here's my solution, which works pretty well.
#!/bin/bash
desired_env="gnuradio"
# Check if the desired environment exists in the list of environments
if conda env list | grep -q "\b$desired_env\b"; then
# Check if the desired environment is active using conda info --envs
if conda info --envs | grep -q "^\s*$desired_env\b"; then
echo "Error: The '$desired_env' environment should be active."
echo "Please activate the '$desired_env' environment using:"
echo "conda activate $desired_env"
read -p "Do you want to activate environment $desired_env? ([Y]/n): " choice
if [[ "$choice" == "y" || -z "$choice" ]]; then
# Either you change it manually or use 'source' instead of 'bash' *.sh
source /home/X/miniconda3/etc/profile.d/conda.sh
conda activate $desired_env
else
exit 1
fi
else
echo "The '$desired_env' environment is currently active."
fi
else
read -p "The '$desired_env' environment does not exist. Do you want to create it? ([Y]/n): " choice
if [[ ${choice:-y} == y ]]; then
conda create -n $desired_env
echo "The '$desired_env' environment has been created. Please activate it using:"
echo "conda activate $desired_env"
else
exit 1
fi
fi
Answered By - MrX Answer Checked By - Candace Johnson (WPSolving Volunteer)