Created
November 11, 2017 12:15
-
-
Save kmhofmann/c23b0fcea23567fe836122aa2b38dc8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# A small script to choose one of more existing Python virtualenvs, | |
# inside $HOME/.virtualenvs. This script needs to be sourced. | |
function venv_activate { | |
source $1/bin/activate | |
} | |
function venv_deactivate { | |
deactivate > /dev/null 2>&1 | |
} | |
glob="$HOME/.virtualenvs/*" | |
dirs=() | |
options=() | |
for dir in $glob; do | |
if [ -d "$dir" ]; then | |
dirs+=("$dir") | |
options+=("$(basename $dir)") | |
fi | |
done | |
if [ ${#options[@]} -eq 0 ]; then | |
echo "ERROR: no directories ${glob} found." | |
return -1 | |
fi | |
# Make the user choose one | |
PS3='Which virtuenv do you want me to activate? Enter number: ' | |
select opt in "${options[@]}" "Deactivate" "Exit"; do | |
if (( REPLY > 0 && REPLY <= ${#options[@]} )); then | |
venv_deactivate | |
venv_activate ${dirs[$((REPLY-1))]} | |
elif (( REPLY == ${#options[@]} + 1 )); then | |
venv_deactivate | |
else | |
echo "Exiting without changes." | |
fi | |
break | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment