Skip to content

Instantly share code, notes, and snippets.

@mike623
Last active January 19, 2019 05:30
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 mike623/d9a4b7cb71dbabb53b2414e9a2058e65 to your computer and use it in GitHub Desktop.
Save mike623/d9a4b7cb71dbabb53b2414e9a2058e65 to your computer and use it in GitHub Desktop.
wrap-env
#!/bin/bash
#
# Place this script into your script folder and use like this:
#
# ./script/wrap-env.sh .env $(npm bin grunt)/grunt serve
#
# Your .env file must have exports in it, not just a list of variable like Docker env files work
#
usage="$(basename "$0") [-h] .env_file command [arg ...]
where:
first argument must be env variable file
second argument must be a command you want to run, for example \`grunt\`
optionally followed by the arguments to the command"
while getopts ':h' option; do
case "$option" in
h) echo "$usage"
exit
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ "$#" -le 1 ]; then
printf "Must provide at least 2 arguments.\n" >&2
printf "\n$usage";
exit 1
fi
if ! [ -f "$1" ]; then
printf "Environment file $1 does not exist.\n" >&2
printf "\n$usage";
exit 1
fi
path=$1;
shift;
env $(grep -v '^#' $path | sed 's/export/''/g' | xargs) $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment