Skip to content

Instantly share code, notes, and snippets.

@kmhofmann
Created November 11, 2017 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmhofmann/c23b0fcea23567fe836122aa2b38dc8a to your computer and use it in GitHub Desktop.
Save kmhofmann/c23b0fcea23567fe836122aa2b38dc8a to your computer and use it in GitHub Desktop.
#! /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