Linux安裝包run、bin製作

原文鏈接:https://blog.csdn.net/tony_328427685/article/details/86505010

製作run安裝包

run程序安裝包實質上是一個安裝腳本加要安裝的程序,如下圖所示:
在這裏插入圖片描述

這樣整個run安裝包結構就一目瞭然了,實際上因爲實際需要結構多少有點變動但這個無關緊要,只需要明白原理就行了。

安裝腳本:helloworld.sh如下:
在這裏插入圖片描述

#!/bin/bash
lines=15
DEFAULT_DIR=/opt
read -p "Please enter the installation directory: " INSTALL_DIR
if [ ! -z "$INSTALL_DIR" ]; then
   DEFAULT_DIR=$INSTALL_DIR
fi
if [ ! -d "$DEFAULT_DIR" ];then
   mkdir -p $DEFAULT_DIR  
fi
tail -n +$lines "$0" > /tmp/helloworld.tar.gz
tar -xzf /tmp/helloworld.tar.gz -C $DEFAULT_DIR
rm -rf /tmp/helloworld.tar.gz
exit 0

lines=15 #這個值是指這個腳本的行數加1,這個腳本共有14行
tail -n +$lines “$0” > /tmp/helloworld.tar.gz # 00表示腳本本身,這個命令用來把從lines開始的內容寫入一個/tmp目錄的helloworld.tar.gz文件裏
tar -xzf /tmp/helloworld.tar.gz -C $ DEFAULT_DIR #解壓到指定目錄
注意:第15行(空行)不能缺少,不然解壓會有問題
安裝程序壓縮包:helloworld.tar.gz
進入程序helloworld父目錄,執行以下命令:
tar -czf helloworld.tar.gz helloworld
壓縮包名稱:helloworld.tar.gz
程序文件夾:helloworld
然後使用cat命令連接安裝腳本helloworld.sh和helloworld.tar.gz
cat helloworld.sh helloworld.tar.gz > helloworld.run
可以通過vi命令查看helloworld.run:
vi helloworld.run
在這裏插入圖片描述
前面14行是helloworld.sh,從第15行開始的亂碼就是源程序

運行helloworld.run時,運行到第14行的exit 0腳本就退出了,所以不會去運行第14行以下的二進制數據(即 helloworld.tar.gz文件),而我們用了tail巧妙地把第14行以下的數據重新生成了一個helloworld.tar.gz文件。再執行安裝。

run安裝包製作較小的程序包是很好的選擇,但是它也有缺點,做邏輯比較複雜的安裝包,寫的安裝腳本將會很麻煩,因此此時還是用其他的安裝包更好。

製作bin安裝包

bin安裝包結構和run包類似,是一個安裝腳本加要安裝的程序,如下圖所示:
在這裏插入圖片描述
安裝腳本:helloworld.sh如下:
在這裏插入圖片描述

#!/bin/bash
install_dir=/opt/helloworld
read -p "Please enter the installation directory:" install_dir
echo "The installation directory is:$install_dir"
mkdir -p $install_dir
sed -n -e "1,/^exit 0$/!p" "$0" > /tmp/helloworld.tar.gz 2>/dev/null
tar -xzf /tmp/helloworld.tar.gz -C ${install_dir}
rm -rf /tmp/helloworld.tar.gz
exit 0

最主要的是下面這句,是將二進制文件從.bin文件裏分離出來
ed -n -e “1,/^exit 0$/!p” “$0” > /tmp/ helloworld.tar.gz 2>/dev/null #意思爲打印除從第一行到所在exit 0的行的所有行到/tmp/helloworld.tar.gz,如果過程中有錯誤則輸出到/dev/null
注意:第14行(空行)不能缺少,不然解壓會有問題
安裝程序壓縮包:helloworld.tar.gz
進入程序helloworld父目錄,執行以下命令:
tar -czf helloworld.tar.gz helloworld
壓縮包名稱:helloworld.tar.gz
程序文件夾:helloworld
然後使用cat命令連接安裝腳本helloworld.sh和helloworld.tar.gz
cat helloworld.sh helloworld.tar.gz > helloworld.bin
可以通過vi命令查看helloworld.bin:
vi helloworld.bin
在這裏插入圖片描述

總結

上面兩個例子,其實不管是bin也好run也好,其實Linux下一切皆文件,而且是不管什麼文件,都是一樣的看法。
所以這些後綴沒有什麼意義。上面兩個不同的地方是分離,一個是用了tail命令,一個是用了sed來實現。
總之,這只是一種思路,不管用什麼辦法,只要能合起來然後又分開就行。

來源:https://blog.csdn.net/tony_328427685/article/details/86505010

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章