© / Posted in Linux / September 21, 2009

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

——————————华丽的分割线——————————

 run程序安装包实质上是一个安装脚本加要安装的程序,如下图所示:

|-----------------|
|                       |
|     安装脚本      |
|                       |
|-----------------|
|                       |
|      程序            |
|                       |
|-----------------|
图: run安装包的结构

这样整个run安装包结构就一目了然了,实际上因为实际需要结构多少有点变动但这个无关紧要,只需要明白原理就行了。
制作run安装包以下举个实际的例子:
为了简单起见,要安装的程序就是helloworld程序,安装它的过程就是把它拷贝到/bin目录下。
$ ls
install.sh helloworld
$ cat install.sh
#!/bin/bash
cp helloworld /bin
$
现在有一个安装脚本了,名为install.sh,有一个要安装的程序helloworld.因为要安装的程序一般都是用.tar.bz2来做的。我们这儿也做一下:
$ tar jcvf helloworld.tar.bz2 helloworld
现在修改一个安装脚本install.sh
改为:
#!/bin/bash
lines=7      #这个值是指这个脚本的行数加1,这个脚本共有6行
tail +$lines $0 >/tmp/helloworld.tar.gz # $0表示脚本本身,这个命令用来把从$lines开始的内容写入一个/tmp目录的helloworld.tar.gz文件里。
tar jxvf /tmp/hellowrold.tar.gz
cp helloworld /bin
exit 0

然后使用cat命令连接安装脚本install.sh和helloworld.tar.bz2。
$ cat install.sh helloworld.tar.bz2 > myinstall.run
这样就得到了myinstall.run文件,它的结构如下:
|------------------| 第1行
|                         |
|    install.sh       |
|                         | 第6行
|------------------|
|                         | 第7行
|helloworld.tar.bz2 |
|                         |
|------------------| 结尾
图: myinstall.run安装包的结构

运行myinstall.run时,运行到第6行的exit 0脚本就退出了,所以不会去运行第7行以下的二进制数据(即helloworld.tar.bz2文件),而我们用了tail巧妙地把第7行以下的数据重新生成了一个helloworld.tar.gz文件。再执行安装。

run安装包制作较小的程序包是很好的选择,但是它也有缺点,做逻辑比较复杂的安装包,写的安装脚本将会很麻烦。因此此时还是用其他的安装包更好。

——————————华丽的分割线——————————

linux 下制作二进制 .bin  的文件
制做方法是使用cat 命令将执行脚本和打包文件同事放到一个.bin的文件里
这样安装的时候只要使用一个包,直接执行该包即可安装完毕,简单方便。
例:制作安装apache、mysql的安装脚本包
1.将源码包先打包
#tar zcvf packages.tar.gz httpd-2.0.63.tar.bz2 mysql-5.0.33.tar.gz

2.编写脚本如下:
# cat install.sh
#!/bin/bash
dir_tmp=/root/installapache
mkdir $dir_tmp
sed -n -e '1,/^exit 0$/!p' $0 > "${dir_tmp}/packages.tar.gz" 2>/dev/null
cd $dir_tmp
tar zxf packages.tar.gz
tar jxf httpd-2.0.63.tar.bz2
cd  httpd-2.0.63
./configure --prefix=/tmp/apache2
make
make install
cd $dir_tmp
tar zxf mysql-5.0.33.tar.gz
cd mysql-5.0.33
./configure --with-charset=gbk --with-extra-charsets=binary,latin1,gb2312 --localstatedir=/home/db --with-mysqld-ldflags=-all-static -enable-assembler --with-innodb --prefix=/tmp/mysql5
make
make install
exit 0

#cat install.sh packages.tar.gz >install.bin

这样就生成install.bin的安装文件,改文件是由shell脚本和二进制合成的。前半部分是脚本后半部分是二进制文件,用strings等二进制查看命令可以看到
最主要的是下面这句,是将二进制文件从.bin文件里分离出来
sed -n -e '1,/^exit 0$/!p' $0 > "${dir_tmp}/packages.tar.gz" 2>/dev/null

安装的时候直接执行
sh install.bin
安装这个方法可以将我们平时常使用的安装脚本化,然后打包。以后使用就方便了。

——————————华丽的分割线——————————
上面两个例子,其实不管是bin也好run也好,其实Linux下一切皆文件,而且是不管什么文件,都是一样的看法。所以这些后缀没有什么意义。上面两个不同的地方是分离,一个是用了tail命令,一个是用了sed来实现。总之,这只是一种思路,不管用什么办法,只要能合起来然后又分开就行。

另外,有人写出了一个脚本来实现这样的功能,感兴趣的可以自己看一下:

http://megastep.org/makeself/

本文有 11 篇评论 ↓↓

    1. 像这么高深的 俺一般都不会用 有时间好好研究一下

      1. @罗银有, 呃,这个不算高深啊~~呵呵,就是把两文件合在一起,然后又分开而已

    2. 文章对我来说好恐怖...完全不明白啊...

    3. 留个名,用到的时候来细读

    4. 有点复杂,目前我还搞不懂~

    5. 什么时候水平到了再看

    6. 白虎 白虎

      不知道他自己试没有,反正我试了,分出来是文本文件,无法解压的

    7. 白虎 白虎

      还有上面用的bz2,里面用的.tar.gz ,这样是不行的

      1. @白虎, 我用得好好的,文章最后也说了,这是一种思路而已

    8. 用的挺好的,Christian Louboutin 不错。


    9. welcome to our burberry outlet store ,there are many fashion and hight quality burberry bags wait for

      you!
      do you want to buy burberry bags for yourslf or your beat friends in sometimes?

      please come to our burberry outlet store.
      our burberry outlet store offer many new style burberry watches for you.do

      not miss the fashion burbery Burberry T-shirts.
      In burberry sunglasses stores it really is typically some sort of

      most effective style which precisely the top notch and several middle-class are typically in a region to afford
      Fouthy-six Putting on a new burberry watches for men and relish the

      summertime Does one prefers to invest in Burberry bags?


      as we all know the burberry brand is very famouse in the world.in burberry bags outlet store you

      will have a good time!
      there is new store,the name is burberry outlet !you are interested in new store,you will have a good

      time in there.
      If you use burberry bags ,you will feel very Summer feeling, so many girls put it

      down.
      evey women love the burberry shirts in life,the burberry can give you more

      confidence in sometimes.
      Jack bought the burberry sunglasses and the next day he came to the

      interview in the Burberry cheak shirt.


      Join forums that areparticularlymeant to talk about watches. If you are searching for burberry sale for women, then

      obviouslycheck out women's forum.
      One of the most popular British exports is the burberry outlet online .
      The particular burberry bags typically reveal the actual Burberry renowned examine layout

      which has been made popular by the garment range.
      The color of burberry ties are bright but with the characters of mature and steady
      the burberry shoes collection had occipied a large markey share and enjoys a massive

      fan following.
      I believe one's destiny of your burberry wallets long decrease jackets are

      often more large-scale progress.

    添加新评论 ↑↑