You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB

#!/usr/bin/env bash
# global variables
# functions
function bashrc_count_lines {
wc -l "$HOME/.bashrc" | cut -d' ' -f1
}
function bashrc_watch_start {
if [[ -z "${BASHRC_LINES+set}" ]]; then
BASHRC_LINES=$(bashrc_count_lines)
else
BASHRC_EVOLVED=true
fi
}
function bashrc_watch_end {
bashrc_lines=$(bashrc_count_lines)
if [[ "$BASHRC_LINES" -lt "$bashrc_lines" || -n "${BASHRC_EVOLVED+set}" ]]; then
echo
echo '*****************************'
echo '* BASHRC has evolved! *'
echo '* please synchronize: *'
echo '* *'
echo "* source ~/.bashrc *"
echo '* *'
echo '*****************************'
echo
fi
}
function bashrc_env_sync {
env_var="$1"
env_value="$2"
if printenv | grep -q "$env_var"; then
real_value=$(eval "echo \$$env_var")
echo "success for $env_var = $real_value"
else
session_line="export ${env_var}=${env_value}"
append_or_replace "^${session_line}.*" "$session_line" "$HOME/.bashrc" &>/dev/null
eval "$session_line"
set +e
eval "BASHRC_LINES=$BASHRC_LINES $0"
exit $?
fi
}
# main
set -Eeu
bashrc_watch_start
echo 'init'
bashrc_env_sync 'SESSION_RESTART' true
bashrc_env_sync 'TOTO' fluo
echo 'end'
bashrc_watch_end