linux下bin安装包制作教程

基本思想:


      制作简单的安装包的时候可以简单的用cat命令连接两个文件,然后头部是脚本文件,执行的时候把下面的文件分解出来就行了。一般这个后部分的文件是个压缩包,那样,就能够打包很多文件了,在脚本中解压出来即可。这就是Linux那些bin啊run啊等安装脚本的简单制作了。


制作步骤:


编写shell脚本


shell脚本主要有两个作用:

1.将压缩包从bin安装包中分离出来并解压    
2.实现安装过程中需要进行的相关的文件操作(从压缩包中复制文件到系统指定路径下)

例:以下是安装脚本 CFCA_UKEY.sh,脚本中添加了必要的注释便于理解相关操作。

#!/bin/bash
#安装前先卸载旧版本软件
if [ -f "/usr/bin/Uninstall_CFCA_UKEY.sh" ];then
	echo "VVVVVVV 卸载旧版本软件 VVVVVVV"
	/usr/bin/Uninstall_CFCA_UKEY.sh
	echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
	echo ""
fi

#分离出的压缩包名称
tmpfile_name=/tmp/CFCA_UKEY_Client.tgz
echo -e "\t 准备安装软件..."
#从bin中分离出${tmpfile_name}压缩包,'1,/^exit 0$/!p'代表从一行到以exit开头以0结尾的行即整个shell脚本,!代表取反,所以最后分离的是出了shell脚本的部分也即压缩包 $0代表脚本本身即从bin安装包中实现分离
sed -n -e '1,/^exit 0$/!p' $0 > ${tmpfile_name} 2>/dev/null
#将分离出的脚本解压缩到/tmp文件夹下
tar xzf ${tmpfile_name} -C /tmp
#解压完成后删除压缩包
rm -rf ${tmpfile_name}
#转到解压文件的文件夹/tmp下
cd /tmp

####---- main ----####

echo -e "\t 安装库文件和工具"
#复制库文件以及管理工具到系统目录下(需要root权限,所以需要sudo执行)
cp -r CFCA_UKEY_Client/CFCA_UKEY_Client.app /Applications
cp -r CFCA_UKEY_Client/CFCA_UKEY_Dlg.bundle /Library/Bundles
sudo cp CFCA_UKEY_Client/Uninstall_CFCA_UKEY.sh /usr/bin
sudo cp CFCA_UKEY_Client/libCFCA_UKEY_*.* /usr/lib
#安装过程完成后,删除解压后的文件目录
rm -rf CFCA_UKEY_Client
echo -e "\t 安装完成"
exit 0


以下是脚本Uninstall_CFCA_UKEY.sh ,实现在每次安装时对旧版本的文件进行删除卸载。(该脚本与bin安装包的制作无关)
#!/bin/bash
name=UKEY
sudo echo ""
echo "=========================================="
echo "|| Client[${name}] Uninstall starting...   ||"
echo "=========================================="
echo "||                                      ||"
sudo ps -ax | grep CFCA_${name}_Client | awk '{print $1}' | xargs kill -9 > /dev/null 2>&1
echo "||  close CFCA_${name}_Client.app          ||"
sudo rm -rf /usr/lib/libCFCA_${name}_lib.dylib
echo "||  delete libCFCA_${name}_lib.dylib       ||"
sudo rm -rf /usr/lib/libCFCA_${name}_P11.dylib
echo "||  delete libCFCA_${name}_P11.dylib       ||"
sudo rm -rf /usr/lib/libCFCA_${name}_P11.sig
echo "||  delete libCFCA_${name}_P11.sig       ||"
sudo rm -rf /Library/Bundles/CFCA_${name}_Dlg.bundle
echo "||  delete CFCA_${name}_Dlg.bundle         ||"
sudo rm -rf /Applications/CFCA_${name}_Client.app
echo "||  delete CFCA_${name}_Client.app         ||"
sudo rm -rf /usr/bin/Uninstall_CFCA_${name}.sh
echo "||  delete Uninstall_CFCA_${name}.sh       ||"
echo "||                                      ||"
echo "=========================================="
echo "||  Client[${name}] Uninstall end.         ||"
echo "=========================================="
echo ""


打包所需文件


将脚本中文件操作相关的文件打包并压缩,之后将压缩包与脚本进行合并生成最终的bin安装包。

本例中所需打包的文件均已放在CFCA_UKEY_Client文件夹下,如下图所示:

linux下bin安装包制作教程_第1张图片

在终端窗口运行指令:

tar -czvf CFCA_UKEY_Client.tgz CFCA_UKEY_Client/ 

CFCA_UKEY_Client文件夹进行压缩生成压缩包CFCA_UKEY_Client.tgz



生成bin安装包

在终端窗口运行指令:
cat CFCA_UKEY.sh CFCA_UKEY_Client.tgz > CFCA_UKEY.bin 
将脚本与压缩包合并生成bin安装包。



   
    生成的bin安装包可以通过vi编辑器查看,可看到前半部分即为shell脚本,后半部分的乱码即为压缩包。脚本中语句sed -n -e '1,/^exit 0$/!p' $0 > ${tmpfile_name} 2>/dev/null即为实现分离出压缩包的功能。ps:随便拿到一个bin安装包,我们也可通过sed语句实现分离出其中的shell脚本以及压缩包:

从bin安装包中分离出shell脚本的指令:sed -n -e '1,/^exit 0$/p' bin安装包路径> 解压路径 2>/dev/null
从bin安装包中分离出压缩包的指令:sed -n -e '1,/^exit 0$/!p' bin安装包路径> 解压路径 2>/dev/null        (与上条指令的区别在于添加了sed中的取反符!)


运行bin安装包

在终端窗口运行指令:
chmod 755 CFCA_UKEY.bin
给生成的bin安装包添加可执行权限,之后运行指令:
./CFCA_UKEY.bin
运行bin安装包,运行结果如下:
linux下bin安装包制作教程_第2张图片 

PS:shell脚本借鉴


可以编写复杂的shell脚本实现更加复杂的安装,比较复杂的shell脚本借鉴如下:

#!/bin/bash

if [ -f "/opt/uninstall.sh" ];then
	echo "VVVVVVVV  卸载旧版本软件  VVVVVVVV"
	/opt/uninstall.sh
	echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
	echo ""
fi
	
tmpfile_name=/tmp/icbc.tar.gz

echo -e "\t 准备安装软件..."
sed -n -e '1,/^exit 0$/!p' $0 > ${tmpfile_name} 2>/dev/null
tar xzf ${tmpfile_name} -C /opt
rm -rf ${tmpfile_name}
cd /opt

MountDisk()
{
        diskname=`fdisk -l | grep 'GB' | awk '$3 -ge 100' | awk '{print substr($2,1,8)}'`
  
        if [ -z "$diskname" ];then
                echo "没有可挂载的硬盘"
                exit 0
        fi

        local filename="/etc/rc.d/rc.local"

        tmpnum=`sed -n -e '/mount/=' ${filename}`
        if [ -z "$tmpnum" ];then
                echo "echo \"Starting Mount '$diskname',Please waiting...\" > /dev/tty1" >>$filename
                echo "mount '$diskname' /opt/icbc/logs" >> $filename

#       AllowPort 60000
        fi

        echo 'y' | mkfs.ext4 $diskname
        mount $diskname /opt/icbc/logs
}

#usage: AllowPort port
AllowPort()
{
	#防火墙允许通过
	echo "开启端口号:" $1
	/sbin/iptables -I INPUT -p tcp --dport $1 -j ACCEPT
	/etc/init.d/iptables save
	/etc/init.d/iptables restart
}

ConfigNetwork()
{
        local filename=/etc/sysconfig/network-scripts/ifcfg-eth0
        tmpnum=`sed -n -e '/NM_CONTROLLED=yes/=' ${filename}`
        if [ -n "$tmpnum" ];then
                sed -i "/NM_CONTROLLED=yes/d" ${filename}
        fi

        tmpnum=`sed -n -e '/PEERDNS=yes/=' ${filename}`
        if [ -n "$tmpnum" ];then
                sed -i "/PEERDNS=yes/d" ${filename}
        fi

        tmpnum=`sed -n -e '/NM_CONTROLLED/=' ${filename}`
        if [ -z "$tmpnum" ];then
       		echo "NM_CONTROLLED=no" >> ${filename} 
	fi

        tmpnum=`sed -n -e '/PEERDNS/=' ${filename}`
        if [ -z "$tmpnum" ];then
       		echo "PEERDNS=no" >> ${filename} 
	fi
}
		
InstallJRE()
{
	local filename=/etc/profile
	tmpnum=`sed -n -e '/JAVA_HOME/=' ${filename}`
	if [ -z "$tmpnum" ];then
		#sed -i "/JAVA_HOME/d" ${filename}
		
		echo "export JAVA_HOME=/opt/jre" >> ${filename}
		echo "export CLASS_PATH=\$JAVA_HOME/lib" >> ${filename}
		echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> ${filename}
	fi
	
	source /etc/profile
}

InstallTomcat()
{
	local filename="/etc/rc.d/rc.local"
	
	if [ ! -f "/etc/rc.d/init.d/tomcat" ];then
#		echo "rm -ef /opt/tomcat/logs/*" >> $filename
#		echo "rm -ef /opt/tomcat/work/*" >> $filename
		
		cp /opt/tomcat/bin/tomcat /etc/rc.d/init.d
		chkconfig --add tomcat
		
		AllowPort 60002
	fi
	
	tmpnum=`ps -ef | grep tomcat | grep -v grep`
	if [ -z "$tmpnum" ];then
		#启动服务
		service tomcat start
	fi
}

InstallSbox()
{
	local filename="/etc/rc.d/rc.local"
	
	tmpnum=`sed -n -e '/sbxmanager/=' ${filename}`
	if [ -z "$tmpnum" ];then
		echo "echo \"Starting Sbox Service,Please waiting...\" > /dev/tty1" >> $filename
		echo "/opt/sbox/bin/sbxmanager.sh start &" >> $filename
		
#		AllowPort 60000
	fi
	
	tmpnum=`ps -ef | grep managerd | grep -v grep`
	if [ -z "$tmpnum" ];then
		#启动服务
		/opt/sbox/bin/sbxmanager.sh start
	fi
}

InstallInitd()
{
        local filename="/etc/rc.d/rc.local"

        tmpnum=`sed -n -e '/initd.jar/=' ${filename}`
        if [ -z "$tmpnum" ];then
                echo "echo \"Starting Proxy Service,Please waiting...\" > /dev/tty1" >> $filename
                echo "/opt/jre/bin/java -Dfile.encoding=utf8 -jar /opt/icbc/initd/initd.jar &" >> $filename
        fi
}

InstallJkgrd()
{
	local filename="/etc/rc.d/rc.local"
	
	tmpnum=`sed -n -e '/jkmgrd/=' ${filename}`
	if [ -z "$tmpnum" ];then
		echo "echo \"Starting jkmgrd Service,Please waiting...\" > /dev/tty1" >> $filename
		echo "jkmgrd p=60001 l=2" >> $filename
		
		jkmgrd p=60001 l=2
		
		AllowPort 60001
	fi
}

StopFirewall()
{
	local filename="/etc/rc.d/rc.local"
	tmpnum=`sed -n -e '/iptables/=' ${filename}`
	if [ -z "$tmpnum" ];then
		echo "echo \"Stopping firewall service, please waiting...\" > /dev/tty1" >> $filename
		echo "service iptables stop" >> $filename
	fi
	service iptables stop
}

InstallJob()
{
	local filename="/var/spool/cron/root"
	
	tmpnum=`sed -n -e '/submitLog/=' ${filename}`
	if [ -z "$tmpnum" ];then
		echo "0 0 * * * submitLog_Day.sh" >> $filename
		echo "0 0 * * 1 submitLog_Week.sh" >> $filename
		echo "0 0 1 * * submitLog_Month.sh" >> $filename
	fi
	tmpnum=`sed -n -e '/minisvs_rotate/=' ${filename}`
        if [ -z "$tmpnum" ];then
                echo "*/1 * * * * minisvs_rotate.sh" >> $filename
        fi
	tmpnum=`sed -n -e '/clean_log/=' ${filename}`
        if [ -z "$tmpnum" ];then
		echo "0 0 * * * clean_log.sh" >> $filename
        fi
}

InstallNtpdate()
{
	local filename="/etc/rc.d/rc.local"
	tmpnum=`sed -n -e '/ntpdate/=' ${filename}`
	if [ -z "$tmpnum" ];then
		echo "echo \"time update, please waiting...\" > /dev/tty1" >> $filename
		echo "ntpdate cn.ntp.org.cn" >> $filename
		echo "hwclock -w" >> $filename
		
		ntpdate cn.ntp.org.cn
	fi
}

StopSSH()
{
	local filename="/etc/rc.d/rc.local"
	
	tmpnum=`sed -n -e '/killall sshd/=' ${filename}`
	if [ -z "$tmpnum" ];then
		echo "echo \"Close SSH Service,Please waiting...\" > /dev/tty1" >> $filename
		echo "killall sshd" >> $filename
	fi
	
	tmpnum=`ps -ef | grep sshd | grep -v grep`
	if [ -n "$tmpnum" ];then
		echo "SSH serivce will close after 10 second"
		sleep 10
		killall sshd
	fi
}

####---- main ----####

echo -e "\t 挂载硬盘..."
MountDisk

echo -e "\t 配置网络..."
ConfigNetwork

echo -e "\t 修改启动列表默认时间"
sed -i  's/timeout=5/timeout=0/' /boot/grub/menu.lst

echo -e "\t 安装库文件和工具..."
cp bin/ftp /usr/bin
cp bin/ntpdate /usr/bin
cp bin/jkmgrd /usr/bin
cp bin/submitLog_*.sh /usr/bin
cp bin/minisvs_rotate.sh /usr/bin
rm -rf bin

echo -e "\t 安装NTP时间同步..."
InstallNtpdate

echo -e "\t 安装Mysql..."
cd mysql && ./install.sh

echo -e "\t 安装JRE..."
InstallJRE

echo -e "\t 安装Tomcat..."
InstallTomcat

echo -e "\t 安装Sbox..."
InstallSbox

echo -e "\t 安装Jkmgrd..."
InstallJkgrd

echo -e "\t 安装Initd..."
InstallInitd

#echo -e "\t 关闭防火墙..."
#StopFirewall

echo -e "\t 开启日志上传计划..."
InstallJob

echo -e "\t 关闭SSH服务..."
StopSSH

exit 0





你可能感兴趣的:(linux,shell)