自动卸载打包安装的脚步

#!/bin/bash
###############################################################################

#功能:首先判断PTI是否运行,运行则停止。停止后卸载安装包(HUAbraspti),接着删          #
#除安装目录并将以前打包生成的文件也进行删除。接着进行打包,首先执行pkgmk命令。          #
#执行过之后再执行pkgtrans命令,生成安装包文件(HUAbraspti.pkg)。执行安装包文件,       #
#进行安装该安装包。安装成功之后,紧接着更新安装目录下的conf目录中的配置文件。           #
#                                                                             #

#     Author  ChengYu                                                         #

#运行须知:如果在/opt/auto/ 目录下有conf目录则在安装完成之后将会覆盖安装路径下         #
#的conf目录,如果不存在则直接完成安装,在执行启动程序,需要手工修改配置文件。           #
###############################################################################

ps=/usr/ucb/ps
port=22300
#创建自动安装HUAbraspti.pkg安装包的脚本。
createInstallFile()
{
echo "#!/usr/local/bin/expect
spawn pkgadd -d /tmp/HUAbraspti.pkg
#Select package(s) you wish to process (or 'all' to process
#all packages). (default: all) [?,??,q]:  
expect \"Select package(s) you wish to process (or 'all' to process all packages). (default: all)\"
send \"\\n\"
#Do you want this directory created now [y,n,?,q]
expect \"Do you want this directory created now\"
send \"y\\n\"
#Do you want to continue with the installation of <HUAbraspti> [y,n,?]
expect \"Do you want to continue with the installation of <HUAbraspti>\"
send \"y\\n\"
expect eof" > install
}

#创建自动卸载HUAbraspti.pkg安装包的脚本。
createUninstallFile()
{
echo "#!/usr/local/bin/expect
spawn pkgrm HUAbraspti
#Do you want to remove this package? [y,n,?,q]
expect \"Do you want to remove this package?\"
send \"y\\n\"
#Do you want to continue with the removal of this package [y,n,?,q]
expect \"Do you want to continue with the removal of this package\"
send \"y\\n\"
expect eof" > uninstall
}

 

checkpkg()
{
    basedir=`pkginfo -l HUAbraspti 2>&1|/usr/xpg4/bin/grep -F "BASEDIR"`

    #如果basedir不为空,也就是HUAbraspti已经安装。需要对HUAbraspti进行卸载。
    if [[ -n $basedir ]]; then
        #获取安装路径 path
        path=`echo $basedir|sed 's/BASEDIR: //'`
        #判断该路径存在
        if [ -e $path ];then
            echo "Install Path is:"$path
            #判断PTI进程是否存在
            ret=`$ps -auxww | grep PTI |wc -l`
            if [ $ret -gt 1 ];then
                echo "PTI is running! PTI will be stop"
                #PTI进程存在则到该路径的bin目录下,暂停该服务。
                installPath=$path"/bin"
                (
                cd $installPath
                bash stoppti $port > /dev/null 2>&1
                echo "PTI stop success!"
                )
            else
                echo "PTI has been stopped!"
            fi
        else
            echo "Install Directory has been deleted!"
        fi
        #进行卸载。
        (
        cd /opt/auto
        createUninstallFile
        /usr/local/bin/expect ./uninstall > /dev/null 2>&1
        rm uninstall
        )
    else
        echo "The HUAbraspti package is not installed"
    fi    
}

cd /opt/PTI
echo "------root@Primaster #pkgrm HUAbraspti"
#检查PTI是否已经被安装,如果安装,则先卸载。(先查看PTI是否在运行,如果在运行中,则先暂停程序)
checkpkg

#验证卸载是否成功
isSuccess=`pkginfo -l HUAbraspti 2>&1|/usr/xpg4/bin/grep -F "BASEDIR"`
if [[ -n $isSuccess ]]; then
    echo "Uninstall Fail!"
    #如果卸载失败,则退出。
    exit 0;
fi

echo "Uninstall Success!  Delete Install Directory!"
rm -rf /opt/PTI/HuaweiBRAS/
#如果/opt/PTI/HuaweiBRAS/存在则不进行打包,直接退出程序
if [ -e /opt/PTI/HuaweiBRAS/ ]; then
    echo "Delete Install Directory Fail!!!!!!"
    exit 0;
fi

#在运行打包前先删除/tmp/HUAbraspti.pkg文件以及/var/spool/pkg/HUAbraspti目录
rm /tmp/HUAbraspti.pkg
rm -rf /var/spool/pkg/HUAbraspti

#开始打包
echo "------root@Primaster #pkgmk -o -b /opt/PTI"
(
cd /opt/InfoFiles
pkgmk -o -b /opt/PTI > /dev/null 2>&1
echo "pkgmk success!"
)
echo "------root@Primaster #pkgtrans -s `pwd` /tmp/HUAbraspti.pkg"
(
cd /var/spool/pkg
echo -e "\n" | pkgtrans -s `pwd` /tmp/HUAbraspti.pkg > /dev/null 2>&1
echo "pkgtrans success!"
)

#重新安装
echo "------root@Primaster #pkgadd -d /tmp/HUAbraspti.pkg"
(
cd /opt/auto
#创建安装可执行文件
createInstallFile
/usr/local/bin/expect ./install > /dev/null 2>&1
#删除安装可执行文件
rm install
)

#将/opt/auto/conf目录拷贝到PTI安装目录下覆盖原有的conf目录
basedir=`pkginfo -l HUAbraspti 2>&1|/usr/xpg4/bin/grep -F "BASEDIR"`
if [[ -z $basedir ]]; then
    echo "Install Package Fail! \nThe Config File update Fail!"
    exit 0
fi    
echo "Install Package Success ! "
basedir=`echo $basedir|sed 's/BASEDIR: //'`
confPath="/opt/auto/conf/"
[ -e $confPath ] || exit 0
cp -fR /opt/auto/conf/ $basedir/
echo "The Config File update Success!"
#启动服务
(
cd $basedir/bin
./startpti
)

你可能感兴趣的:(脚本,File,delete,bash,Path)