Add define_env function for define environment

This commit is contained in:
Yorick Barbanneau 2019-11-23 00:06:46 +01:00
parent da921ae09a
commit 7680d8c96c
3 changed files with 45 additions and 1 deletions

View file

@ -103,6 +103,21 @@ Install a systemd service as user, can be useful for timers for example :
service_install "services/myservice.service" 1 service_install "services/myservice.service" 1
``` ```
### define_env
Define an evirnment variable into the file `~/.config/environment`, you have to
source this file on your `.bashrc` or your `.zprofile`.
- **input*** :
- `$1` : name of the variable
- `$2` : value, `""` if empty
- **output** : no value, but exit the function if the variale is already
defined (install) or not in file (uninstall)
```
define_env "NOTMUCH_CONFIG" "~/.config/notmuch"
```
### Avaible variables ### Avaible variables
- `$repository` : the root folder of the repository - `$repository` : the root folder of the repository

View file

@ -37,3 +37,6 @@ install_service "services/mail.timer" 1
# Symblink an executable to $HOME\.local\bin\ # Symblink an executable to $HOME\.local\bin\
install_bin "bin/check_mails.sh" install_bin "bin/check_mails.sh"
# Define the environment variable for Notmuch
define_env "NOTMUCH_CONFIG" "~/.config/notmuch"

View file

@ -6,7 +6,7 @@ BIN_DIRECTORY="${HOME}/.local/bin/"
LIB_DIRECTORY="${HOME}/.local/lib/" LIB_DIRECTORY="${HOME}/.local/lib/"
SYD_DIRECTORY="${HOME}/.config/systemd/user" SYD_DIRECTORY="${HOME}/.config/systemd/user"
DOTREPO="${HOME}/.config/dotrepo/" DOTREPO="${HOME}/.config/dotrepo/"
ENV_FILE="${HOME}/.config/environment"
# DEFAULT VALUES # DEFAULT VALUES
OVERWRITE_DIRECTORY=0 OVERWRITE_DIRECTORY=0
@ -281,6 +281,32 @@ install_bin ()
fi fi
} }
define_env ()
{
# Put a couple name value into $ENV_FILE file or remove it
# on uninstall mode
# $1 variable name
# $2 value
[ -z $1 ] && { error "You must define a name"; return; }
[ ! -f $ENV_FILE ] && touch $ENV_FILE
if [ $install -eq 1 ]
then
printf "\nCreate an environment variable %s : " "$1"
[ $(grep -c -m 1 $1 $ENV_FILE) -eq 1 ] && { error "already exist"; return; }
local line
line="export ${1}=\"${2}\""
echo "$line" >> $ENV_FILE
printf "\e[32mdone\e[0m\n"
else
printf "\nRemove an environment variable %s : " "$1"
[ $(grep -c -m 1 $1 $ENV_FILE) -eq 0 ] && { error "not exist"; return; }
sed -i "/^export $1=*/d" $ENV_FILE
printf "\e[32mdone\e[0m\n"
fi
}
## create bin directory ## create bin directory
[ ! -d $BIN_DIRECTORY ] && mkdir -p $BIN_DIRECTORY || printf "bin exist\n" [ ! -d $BIN_DIRECTORY ] && mkdir -p $BIN_DIRECTORY || printf "bin exist\n"