@@ -58,6 +58,13 @@ if [ "$#" -gt 0 ]; then
5858 done
5959fi
6060
61+ custom_func_keyword=" ~/.customfunction"
62+ custom_func_root=~ /.customfunction
63+
64+ funcs_code_begin=($( grep -xn " ^\S*(){" ./config/.customfunction) ) # \S: match non-whitespace character
65+ funcs_code_end=($( grep -xn " }" ./config/.customfunction) )
66+ declare -A funcs_info=([nvm]=$no_nvm [pipenv_correspond]=$no_pipenv_correspond )
67+
6168function Echo_Color(){
6269 case $1 in
6370 r* | R* )
@@ -101,18 +108,15 @@ function Ask_yn(){
101108# ====================================================
102109# Part 2. Main
103110# ====================================================
104- custom_func_keyword=" ~/.customfunction"
105- custom_func_root=~ /.customfunction
106-
107- func_begin_arr=($( grep -xn " ^\S*(){" ./config/.customfunction) ) # \S: match non-whitespace character
108- func_end_arr=($( grep -xn " }" ./config/.customfunction) )
109-
110111function Get_function_code(){
111- for key in ${! func_end_arr[*]} ; do
112- if echo ${func_begin_arr[$key]} | grep --silent -i $1 ; then
113- declare -i begin=$( echo ${func_begin_arr[$key]} | cut -d ' :' -f 1) -1
114- declare -i end=$( echo ${func_end_arr[$key]} | cut -d ' :' -f 1) +1
115- function_code=$( sed -n " $begin ,$end p" ./config/.customfunction)
112+ declare -i key
113+ declare -i begin
114+ declare -i end
115+ for key in ${! funcs_code_end[*]} ; do
116+ if echo ${funcs_code_begin[$key]} | grep --silent -i $1 ; then
117+ begin=$( echo ${funcs_code_begin[$key]} | cut -d ' :' -f 1) -1
118+ end=$( echo ${funcs_code_end[$key]} | cut -d ' :' -f 1)
119+ function_code=$( sed -n " $begin ,$end p" ./config/.customfunction)
116120 fi
117121 done
118122}
@@ -135,9 +139,15 @@ case $SHELL in
135139 ;;
136140esac
137141
138- echo ' #!/usr/bin/env bash' > $custom_func_root
139- if grep -n " source $custom_func_keyword " $profile ; then
140- echo " $custom_func_keyword is already in the $profile "
142+ if [ ! -f " $custom_func_root " ]; then
143+ echo ' #!/usr/bin/env bash' > $custom_func_root
144+ else
145+ Echo_Color y " You already have .customfunction"
146+ fi
147+ Echo_Color g " File path: $custom_func_root "
148+
149+ if [ " $( grep -n " source $custom_func_keyword " $profile ) " != " " ]; then
150+ Echo_Color y " $custom_func_keyword is already in the $profile "
141151else
142152 Echo_Color y " Write: source $custom_func_keyword into $profile "
143153 printf " \n# custom func. by self\nsource $custom_func_keyword \n" >> $profile
146156if ! $no_nvm ; then
147157 # delete original nvm setting in the profile
148158 num_original_line=$( grep -xn ' export NVM_DIR="$HOME/.nvm"' $profile | head -n 1 | cut -d ' :' -f 1)
149- if [ " $num_original_line " -gt 0 ]; then
150- Echo_Color y " remove the office support method..."
159+ if [ " $num_original_line " != " " ]; then
160+ Echo_Color y " remove the official support method..."
151161 sed -i " $num_original_line , $(( $num_original_line + 2 )) d" $profile
152162 fi
153-
154- Get_function_code " nvm" ; printf " \n$function_code \n" >> $custom_func_root
155163fi
156164
157- if ! $no_pipenv_correspond ; then
158- Get_function_code " pipenv_correspond" ; printf " \n$function_code \n" >> $custom_func_root
159- fi
165+ for key in ${! funcs_info[*]} ; do
166+ if ! ${funcs_info[$key]} ; then
167+ if [ " $( grep " $key " $custom_func_root ) " != " " ]; then
168+ Echo_Color y " You already have $key () in $custom_func_root ."
169+ else
170+ Get_function_code " $key " ; printf " \n$function_code \n" >> $custom_func_root
171+ fi
172+ fi
173+ done
160174
161175Echo_Color g " Done config!!"
0 commit comments