-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvedit
40 lines (33 loc) · 947 Bytes
/
envedit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Do not make this executable!
# use a . .command to invoke perhaps with an alias like pathed
if [ "$0" == "$BASH_SOURCE" -o -z "$1" ]
then
echo Error. You must source this script
echo Usage: . envedit VAR
echo or . envedit alias
echo E.g.: . $0 PATH
else
if [ "$1" = "alias" ]
then
alias enved=". $BASH_SOURCE"
echo Alias created
else
# Uncomment out next line to override defaults
#ENVEDIT_EDITOR="emacs -nw"
ENVEDIT_FN=$(mktemp) # temporary file
echo ${!1} >"$ENVEDIT_FN" # set up path
ENVEDIT_OLD=$(ls -li $ENVEDIT_FN) # remember last time
# execute the editor as best we ecan
command ${ENVEDIT_EDITOR:-${VISUAL:-${EDITOR:-vi}}} "$ENVEDIT_FN"
# examine new date
ENVEDIT_NEW=$(ls -li $ENVEDIT_FN )
# if file changed and is not empty
if [ "$ENVEDIT_OLD" = "$ENVEDIT_NEW" -o ! -s "$ENVEDIT_FN" ]
then
echo "No changes"
else
ENVEDIT_CMD=$1=\"$(cat "$ENVEDIT_FN")\" # set new var
eval "$ENVEDIT_CMD"
fi
fi
fi