#!/bin/sh ves_var_add() { if [ "$#" -lt 2 ]; then printf "ERROR: Insufficient arguments provided.\n" return 2 fi env="" while :; do case $1 in --env=*) env=$( echo $1 | cut -d "=" -f 2 ) shift break ;; --) shift break ;; -?*) printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr return 2 ;; *) break esac shift done active_env=0 if [ -z "$env" ]; then if ! [ -z "$SHVES_ENV_NM"]; then env="$SHVES_ENV_NM" active_env=1 else printf "ERROR: No valid environment active or specified\n" > /dev/stderr return 1 fi fi if ! _shves_check_env_name "$env"; then return 1 fi if ! _shves_check_env_exists "$env"; then return 1; fi # First, we will get the variable value from the environment file and # create our new version of it. fname="$SHVES_ENV_DIR"/"$env" var_value=$(grep "export_var:$var=" $SHVES_ENV_DIR/test2 | cut -d '=' -f 2) if [ -z "$var_value" ]; then var_value="$2" else var_value="$var_value:$2" fi # And then update the file to use the new version. sed -i "s/export_var:$var=.*"/export_var:$var=$var_value"/" "$fname" # If we are updating the currently active environment, export the # new value as well, if [ $active_env -eq 1 ]; then export "$var"="$var_value" fi # Ah, bash would have made this so much nicer... alas # if [ -z ${!1+x} ]; then ... # if [ "$active_env" -eq 1 ] && eval [ -z \${$1+x} ]; then # printf "ERROR: Specified variable [%s] does not exist in environment.\n" $1 > /dev/stderr # printf "\tYou can create it with\n\t ves export %s\n " $1 > /dev/stderr # return 2 # fi }