-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddenv
executable file
·47 lines (39 loc) · 1.19 KB
/
addenv
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
41
42
43
44
45
46
47
#!/bin/bash
#takes in a PATH variable and prepends another folder to it
#set vars
script=$(basename $0)
#clean up
trap 'rm ~/temp -f' EXIT SIGTERM 1 2 3 19 20
#params check
if [ $# -ne 2 ];then
echo "$script : format is '$script PATH <arg2>' "
exit 1
fi
#check if <arg2> is in <arg1>, should both be strings
cat ~/bin/partials/SHBANG >> ~/temp
echo "echo \$$1 | grep ^${2}$ > /dev/null" >> ~/temp
echo 'chk1=$?' >> ~/temp
echo "echo \$$1 | grep ^${2}: > /dev/null" >> ~/temp
echo 'chk2=$?' >> ~/temp
echo "echo \$$1 | grep :${2}$ > /dev/null" >> ~/temp
echo 'chk3=$?' >> ~/temp
echo "echo \$$1 | grep :${2}: > /dev/null" >> ~/temp
echo 'chk4=$?' >> ~/temp
#run temp script, sets variables
. ~/temp #doing this instead of eval
#combine tests into one
chk=$((chk1 + chk2 + chk3 + chk4))
# chk = 4 means it already exists in the PATH.
if [ ! $chk -lt 4 ];then
#not found, prepend to arg1 with : separater
echo $2 could not be found in $1 , so we will prepend!
expand=$(eval "echo \$$1")
eval 'export $1="$2:$expand"'
sleep 1
echo
echo "Here is PATH: $PATH"
else
#found, say so and quit
echo $2 was found in $1 already
fi
#test for export outside of current shell