admin 管理员组

文章数量: 1184232


2024年1月22日发(作者:js怎么把数组进行大小排序)

利用shell脚本制作可执行安装程序

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

如在linux 下制作二进制 .bin 文件的制做方法:就是使用cat 命令将执行脚本和打包文件同时放到一个.bin的文件里。这样安装的时候只要使用一个包,直接执行该包即可安装完毕,简单方便。

下例就是一名为脚本,它实现以下功能:

 自动创建用户及用户组

 自动检查网络环境并安装环境包所需要的支撑环境

 显示安装进度

 自动检查并配置防火墙

 自动安装环境包

 自动增加自启动脚本

1. bin文件制作步骤

a) 打包文件

tar cvf

b) 编写脚本

内容见《脚本内容》节

c) 可执行安装程序制作方法

# cat

>

这样就生成的安装文件,该文件是由shell脚本和二进制合成的。前半部分是脚本后半部分是二进制文件,用strings等 二进制查看命令可以看到。

2. 安装

chmod 755

./

3. 脚本内容

#/bin/bash

PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

curdir=`pwd`

installdir=/myapp

myappuser=myapp

env_name=myapp-env-1.0-linux-x64-installer

sum=0

echo "----------------------------------------------------------------------------"

echo "Welcome to the myapp-env-1.0 for linux-x64 Stack Setup Wizard."

echo "----------------------------------------------------------------------------"

echo " "

echo "Installation folder : $installdir "

echo "----------------------------------------------------------------------------"

echo ""

echo " "

#检查网络环境

checknetwork ()

{

netstat=$(ping -c3 |grep transmitted |awk '{print $4}')

}

#检查网络状态、判断支撑软件是否安装,若没有安装则自动安装。

checkenv ()

{

rpm -qa > /tmp/

for soft in $softlist

do

grep "^$soft" /tmp/ > /dev/null

if [ $? -ne 0 ] ; then

echo "$soft is not installed"

if((netstat==0)) ; then

echo "ping -c3 ,failed . Please check network"

exit

else

echo "install $soft ...... "

yum -y install $soft | tee -a

fi

done

}

else

echo "$soft was installed"

fi

rm /tmp/

#检查防火墙配置,并自动配置防火墙策略

Config_Firewall ()

{

echo "checking and configuring the firewall rule ....."

echo " "

for port in $myappport

do

iptables -L -n |grep :$port

if [ $? -ne 0 ] ; then

if [ "$port" == "8080" ] ; then

/sbin/iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport $port -j

ACCEPT

else

/sbin/iptables -I INPUT -p tcp --dport $port -j ACCEPT

fi

echo "$port is not in iptables, the Port: $port is opened"

else

echo "$port is exists"

fi

done

}

#增加用户、密码和用户组、并修改安装目录的属主

adduser()

{

echo "----------------------------------------------------------------------------"

echo " creating UserGroup: $myappuser and username: $myappuser and default

passwd: myapp4308 "

#若用户已经存在则不增加

grep "$myappuser:" /etc/group &> /dev/null || groupadd $myappuser

grep "$myappuser:" /etc/passwd &> /dev/null || useradd $myappuser -d $installdir -g

$myappuser

#用户密码

echo "myapp4308" | passwd --stdin "$myappuser"

#修改目录属主

chown $myappuser $installdir

chgrp $myappuser $installdir

}

#支撑软件列表

softlist="

gcc

openssl

unixODBC

unixODBC-devel

ncurses-devel

gtk2

xmlto

dialog

"

#应用端口列表

#mysqlport=3306

#wwwport=8080

#mqport=5673

#mqweb=5567

myappport="

8080

5567

5673

3306

"

checknetwork

checkenv $softlist

echo "----------------------------------------------------------------------------"

Config_Firewall $myappport

iptables-save > /etc/sysconfig/iptables

echo "----------------------------------------------------------------------------"

if [ ! -d "$installdir" ] ; then

echo " creating folder $installdir"

mkdir $installdir

else

echo "Warning: cannot create directory $installdir exists. "

echo "----------------------------------------------------------------------------"

exit

fi

adduser

echo "----------------------------------------------------------------------------"

echo "Extract the "

echo "----------------------------------------------------------------------------"

#将二进制文件从.bin文件里分离出来

sed -n -e '1,/^exit 0$/!p' $0 > "${installdir}/$env_" 2>/dev/null

if [ $? -eq 0 ];then

echo "Install ......"

cd $installdir

# su $myappuser -c "gunzip < $env_ | tar xvvf -"

# for x in "`tar -tvvzf $env_`"; do

# one=`echo "$x" | awk '{print $3}'`;

# done

# for x in $one; do

# sum=`expr $sum + $x`;

# done

# s=$sum

s=650028382

#以myappuser用户执行解压并显示进度

su $myappuser -c "gunzip < $env_ | tar xvvf -" | awk -v s=$s '{ a=a+$3;

b=(a/s)*100;printf "%dn",b}' | dialog --gauge "Install ..." 10 70 0

# rm $env_

cp $installdir/env/scripts/bash_profile $installdir/.bash_profile

echo "----------------------------------------------------------------------------"

echo "Setup has finished installing myapp-env-1.0 for linux-x64 Stack on your computer."

echo " "

echo "usage: $installdir/env/ help"

echo " $installdir/env/ (start|stop|restart|status)"

echo " $installdir/env/ (start|stop|restart|status) mysql"

echo " $installdir/env/ (start|stop|restart|status) apache"

echo " $installdir/env/ (start|stop|restart|status) tomcat"

echo " $installdir/env/ (start|stop|restart|status) rabbitmq"

echo "help - this screen"

echo "start - start the service(s)"

echo "stop - stop the service(s)"

echo "restart - restart or start the service(s)"

echo "status - show the status of the service(s)"

else

echo "$env_ not found"

exit

fi

echo "----------------------------------------------------------------------------"

echo "Are you want to auto start the service on rebooting the unix machine. [Y/N] or [y/n] ? "

read ans

#增加服务和自启动

if [ "$ans" = "Y" -o "$ans" = "y" ]; then

cp $installdir/env/scripts/myapp-env /etc/init.d/.

chmod 755 /etc/init.d/myapp-env

rm /etc/rc3.d/S96myapp-env

ln -s /etc/init.d/myapp-env /etc/rc3.d/S96myapp-env

#Register Sonar at boot time :

sudo chkconfig --add myapp-env

fi

echo "----------------------------------------------------------------------------"

echo " "

echo "Do you want to start [Y/N] or [y/n] ? "

read ans

if [ "$ans" = "Y" -o "$ans" = "y" ]; then

su $myappuser -c "$installdir/env/ start"

su $myappuser -c "$installdir/env/ status"

echo " "

echo "----------------------------------------------------------------------------"

echo "Once started, you can open your browser and access the following URL:"

echo "127.0.0.1:8080"

echo "If you enabled the Tomcat Manager Web Application tool, you can access the

following URL "

echo "(you will be prompted to enter the user and password set during the installation):"

echo "127.0.0.1:8080/manager/html"

fi

exit 0


本文标签: 文件 脚本 安装 自动 执行