The process of shell synthesis upgrade package

1. Define some names and variables

name=name      #定义名称
unshname=uninstall${name}.sh   #卸载脚本文件的功能
appname=appname             
installpath=/opt/${name}     #安装的路径,并且给被安装的文件命名

2. Check whether there is a file named $unshname, if it exists, execute the file to uninstall the old version of the software

if [ -f "/usr/bin/$unshname" ];then
    echo "=========== 卸载旧版本软件 ============="  
    /usr/bin/$unshname  
    echo "====================================="  
    echo ""  
fi

3. Extract the installation files in the compressed package to a temporary file.

  • -n: Only output edited lines.
  • -e: The argument that follows is the sed script to execute.
  • '1,/^exit 0/!p': "Print all lines from the first line up to the first line matching '^exit 0'". In this example, what it does is extract the content before 'exit 0' in the shell script.
  • $0: is a special variable that represents the name of the currently running script file.
  • sed -n -e '1,/^exit 0/!p' $0 > ${tmpfile_name} 2>/dev/null means to extract the content before 'exit 0' in the Shell script and write it to a temporary file ${tmpfile_name}middle. Also, it will ignore any errors that occur.
tmpfile_name=/tmp/${name}.tgz   #定义了临时文件
echo -e "\t                     #准备安装软件
  
#从当前脚本中分离出压缩包部分,写入到临时文件${tmpfile_name}
sed -n -e '1,/^exit 0/!p' $0 > ${tmpfile_name} 2>/dev/null

tar xzf ${tmpfile_name} -C /tmp #解压压缩包
rm -rf ${tmpfile_name}          #删除压缩包
cd /tmp                         #切换到放置压缩包的目录中

4. Check whether the path exists $installpath, if not, create the path

if [ ! -d $installpath ] ;then
    mkdir -p $installpath
fi

5. Install library files and tool files

sed -i '1i/${installpath}' ${name}/$configname

  • -i: Make modifications directly in the original file, rather than outputting to the terminal or another file.
  • '1i/${installpath}': 1imeans to insert before the first line, ${installpath}it is a variable whose value will be inserted into the specified file.
  • ${name}/$configname: This is the path to the file to modify. ${name}and $confignameare also variables whose values ​​depend on the context.

Therefore, the function of this command is to ${installpath}insert the value of config ${name}/$confignamebefore the first line of the file, and save the modified content back to ${name}/$configname.

echo -e "\t 安装库文件和工具软件" 
configname=${name}.conf                          #配置文件的名称
touch ${name}/$configname                        #创建一个空的配置文件
sed -i '1i/${installpath}' ${name}/$configname   #将前面文件的内容输入到第二个文件的第一行前面
cp ${name}/$configname /etc/ld.so.conf.d/        #将配置文件复制到/etc/ld.so.conf.d/目录
sed -i '1i/${installpath}' ${name}/$configname
cp ${name}/$configname /etc/ld.so.conf.d/
ldconfig                                         #ldconfig命令重新加载库文件
cp -r ${name}/* $installpath                #将${name}文件夹的所有文件复制到${installpath}。

6. Delete the temporary folder ${name},输出“安装完成"

rm -rf ${name}
echo -e "\t 安装完成"  
exit 0

Guess you like

Origin blog.csdn.net/weixin_45981798/article/details/131778219