forked from Alfonova-Node/NODE_TESTNET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_variable.sh
114 lines (111 loc) · 3.25 KB
/
insert_variable.sh
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# Default variables
name=""
value=""
type="export"
rename=""
delete_type="none"
# Options
. <(wget -qO- https://raw.githubusercontent.com/SecorD0/utils/main/colors.sh) --
option_value(){ echo "$1" | sed -e 's%^--[^=]*=%%g; s%^-[^=]*=%%g'; }
while test $# -gt 0; do
case "$1" in
-h|--help)
. <(wget -qO- https://raw.githubusercontent.com/SecorD0/utils/main/logo.sh)
echo
echo -e "${C_LGn}Functionality${RES}: the script inserts variable or alias to ${C_LGn}$HOME/.bash_profile${RES}"
echo
echo -e "${C_LGn}Usage${RES}: script ${C_LGn}[OPTIONS]${RES}"
echo
echo -e "${C_LGn}Options${RES}:"
echo -e " -h, --help show the help page"
echo -e " -n, --name NAME variable or alias NAME (mandatory)"
echo -e " -v, --value VALUE variable or alias VALUE (leave blank for manual input)"
echo -e " -a, --alias inserting an alias"
echo -e " -r, --rename NAME_2 rename NAME_2 to NAME"
echo -e " -d, --delete delete the first occurrence of a variable or alias by NAME"
echo -e " -da, --delete-all delete all occurrences of a variable or alias by NAME"
echo
echo -e "You can use either \"=\" or \" \" as an option and value ${C_LGn}delimiter${RES}"
echo
echo -e "${C_LGn}Useful URLs${RES}:"
echo -e "https://github.com/SecorD0/utils/blob/main/miscellaneous/insert_variable.sh - script URL"
echo -e "https://t.me/letskynode — node Community"
echo
return 0 2>/dev/null; exit 0
;;
-n*|--name*)
if ! grep -q "=" <<< "$1"; then shift; fi
name=`option_value "$1"`
shift
;;
-v*|--value*)
if ! grep -q "=" <<< "$1"; then shift; fi
value=`option_value "$1"`
shift
;;
-a|--alias)
type="alias"
shift
;;
-r*|--rename*)
if ! grep -q "=" <<< "$1"; then shift; fi
rename=`option_value "$1"`
shift
;;
-d|--delete)
delete_type="delete"
shift
;;
-da|--delete-all)
delete_type="delete_all"
shift
;;
*|--)
break
;;
esac
done
# Functions
printf_n(){ printf "$1\n" "${@:2}"; }
# Actions
touch $HOME/.bash_profile
. $HOME/.bash_profile
if [ ! -n "$name" ]; then
printf_n "${C_R}You didn't specify a name via${RES} -n ${C_R}option!${RES}"
return 1 2>/dev/null; exit 1
fi
if [ "$delete_type" != "none" ]; then
if [ "$delete_type" = "delete" ]; then
sed -i "0,/ ${name}=/{/ ${name}=/d;}" $HOME/.bash_profile
elif [ "$delete_type" = "delete_all" ]; then
sed -i "/ ${name}=/d" $HOME/.bash_profile
fi
unset "$name"
unalias "$name" 2>/dev/null
else
if [ -n "$rename" ]; then
sed -i "s%${rename}%${name}%" $HOME/.bash_profile
unset "$rename"
unalias "$rename" 2>/dev/null
if [ ! -n "$value" ]; then
. $HOME/.bash_profile
return 0 2>/dev/null; exit 0
fi
fi
if [ ! -n "$value" ]; then
printf "${C_LGn}Enter the value:${RES} "
read -r value
fi
if ! cat $HOME/.bash_profile | grep -q " ${name}="; then
echo "${type} ${name}=\"${value}\"" >> $HOME/.bash_profile
elif ! cat $HOME/.bash_profile | grep -qF "${name}=\"${value}\""; then
sed -i "s%^.*${name}*=.*%${type} ${name}=\"${value}\"%" $HOME/.bash_profile
fi
variable=`cat $HOME/.bash_profile | grep -qF "${name}=\"${value}\""`
if ! grep -q "${type}" <<< "$variable"; then
sed -i "s%^.*${name}*=.*%${type} ${name}=\"${value}\"%" $HOME/.bash_profile
fi
fi
sed -i '/^$/d' $HOME/.bash_profile
. $HOME/.bash_profile