在Linux開發過程中對環境變量的修改是難免的,手工修改環境變量痛苦不言而喻,而export也只能影響到當前shell的。在腳本中修改的變量只能通過source才能使父shell生效。自己寫了一個備份環境變量和恢復的腳本,有待完善,但能用。
BAK="env.bak"
function bak(){
echo "export PATH=$PATH" > ${BAK}
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> ${BAK}
echo "export SDGHOME=$SDGHOME" >> ${BAK}
echo "export Oracle_HOME=$ORACLE_HOME" >> ${BAK}
}
function change(){
AR=$1
RE=`sed -n '/${AR}=/'p ${RC}`
RB="export `sed -n '/${AR}=/'p ${BAK}`"
if [ "${RE}" == "" ];then
echo ${RB} >> ${RC}
else
sed -i "s/^\(${AR}=\).*/&${RB}" >> ${RC}
fi
}
function restore(){
RC=~/.bashrc
change "PATH"
change "LD_LIBARY_PAHT"
change "SDGHOME"
change "ORACLE_HOME"
}
short_options="b:r:n"
long_options="back:,restore:,name:"
opts=`getopt -o $short_options --long $long_options`
case $1 in
-b|--back)
echo "back enviroment, you can use \"source ${BAK}\" use bak enviroment"
bak
;;
-r|--restore)
echo "restore enviroment for [[`whoami`]],you must user new show,enable this enviroment"
restore
;;
-n|--name)
BAK=$1
;;
*)
echo "Internal error!"
exit 1
;;
esac