LibreOffice是一款免費、開源的辦公套件,多語言支持,是The Document Foundation(TDF)的一個項目。該項目於2010年開始,其最初的源碼來自OpenOffice,第一個版本於Jan 25, 2011釋出。LibreOffice包含多個功能強大的應用,如對文字(word)、電子數據表(spreadsheets)、幻燈片(slideshows)、圖表(diagrams)、製圖(drawings)、數據庫(databases)、數學公式(mathematical formulae)等的處理。

本文介紹如何使用Shell Script安裝/更新LibreOffice,包含版本比較、舊版本卸載、SHA-256校驗、安裝/更新等功能。

Introduction

LibreOffice相關信息

Release Note

LibreOffice的版本釋出信息見Release Notes。以下爲截至01 Mar, 2017時的版本信息

Type Release Date Version No.
Fresh 2017-02-01 5.3.0
Still 2017-01-26 5.2.5

Release Version Check

Approximate Version

LibreOffice最新釋出版本檢測(通過curl命令抓取下載頁的HTML源碼實現)

# Both
curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="(Fresh|Still)"/s@<[^>]*>@@gp'

# Fresh Only
curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="Fresh"/s@<[^>]*>@@gp'
# curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="Fresh"/s@<[^>]*>@@gp' | awk '{print $2}'

# Still Only
curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="Still"/s@<[^>]*>@@gp'
# curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="Still"/s@<[^>]*>@@gp' | awk '{print $2}'

輸出內容格式

LibreOffice 5.3.0 (2017-02-01) - Fresh Branch LibreOffice 5.2.5 (2017-01-26) - Still Branch

使用如下命令可自動生成Markdown表格

curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="(Fresh|Still)"/s@<[^>]*>@@gp' | awk 'BEGIN{printf "Type|Release Date|Version No.\n---|---|---\n"}{version=$2;date=gensub(/\((.*)\)/,"\\1","g",$3);type=$(NF-1);printf("%s|%s|%s\n",type,date,version)}'

輸出內容格式

Type|Release Date|Version No.
---|---|---
Fresh|2017-02-01|5.3.0
Still|2017-01-26|5.2.5

Markdown渲染效果

Type Release Date Version No.
Fresh 2017-02-01 5.3.0
Still 2017-01-26 5.2.5

但是 Release Notes頁面的版本與真實的釋出版本之間有細微的區別。在Release頁面,穩定版爲5.2.5,但在Old Archive中,穩定版爲5.2.5.1。而安裝該穩定版後獲取到的版本號也是5.2.5.1。故應以Old Archive列表中的版本號爲準。

Precise Version

以從Release Notes頁面的近似版本爲依據,從Old Archive列表中獲取精確的版本號(逆序,只取一個)。

proximateVersionCheck=$(curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="Still"/s@<[^>]*>@@gp' | awk '{print $2}')

curl -fsSL https://downloadarchive.documentfoundation.org/libreoffice/old/ | sed -r -n '/'"$proximateVersionCheck"'/s@.*(<a .*</a>).*@\1@gp' | sed -n 's@/@@g;$s@<[^>]*>@@gp'

unset proximateVersionCheck

獲取到最新穩定版的精確版本號5.2.5.1

Installed Version Check

如果系統已經安裝有LibreOffice,可通過如下命令查看具體版本號

# for i in `ls /usr/bin/libreoffice*`;do if command -v $i &> /dev/null; then $i --version | awk '{print $2;exit}';else echo 'No LibreOffice Find';fi;break; done

for i in `ls /usr/bin/libreoffice*`;do
    if command -v $i &> /dev/null; then
        $i --version | awk '{print $2;exit}'
    else
        echo 'No LibreOffice Find'
    fi
    break
done

實例演示

flying@openSUSE:~> for i in `ls /usr/bin/libreoffice*`;do if command -v $i &> /dev/null; then $i --version | awk '{print $2;exit}';else echo 'No LibreOffice Find';fi;break; done
5.2.3.3
flying@openSUSE:~>

Get Help

以下爲LibreOffice的幫助頁面

  1. Documentation
  2. System Requirements 2.1. GNU/Linux
  3. Installation Instructions 3.1. GNU/Linux 3.1.1. Installing LibreOffice on Linux

System Requirements

LibreOffice在GNU/Linux系統下對軟硬件的要求見System Requirements

Hardware Prerequisites

硬件需求

Type Minimal Requirements Recommended
CPU Pentium-compatible PC Pentium III, Athlon or more-recent system
RAM 256Mb 512Mb
Disk 1.55Gb
Resolution X Server 1024x768
256 colors
higher resolution

Software Prerequisites

軟件需求

Software Minimal Version Explanation
Linux Kernel 2.6.18
glibc2 2.5
gtk 2.10.4
GNOME 2.16 gail 1.8.6, at-spi 1.7
another compatible GUI (such as KDE, among others)
Java Optional

Download

LibreOffice的下載地址爲Download Page,但官方 似乎 沒有提供GnuPG的簽名。

此處下載的是LibreOffice的rpmdeb包,其生成的下載鏈接與以下因素有關 * 操作系統CPU架構(x86_64x86)、 * LibreOffice版本號(5.2.55.3.0) * 包管理器 * deb: Debian/Ubuntu * rpm: RedHat/CentOS/Fedora/SUSE/OpenSUSE * 語言(en-USzh-TW)

此處以下載版本號爲5.25的正體中文版(zh-TW)爲例,操作系統選擇Linux x86_64 (deb)

以下鏈接獲取自Download Page

# 瀏覽器地址欄中生成的地址
https://www.libreoffice.org/download/download/?type=deb-x86_64&version=5.2.5&lang=zh-TW

# Download按鈕生成的地址爲
http://donate.libreoffice.org/home/dl/deb-x86_64/5.2.5/zh-TW/LibreOffice_5.2.5_Linux_x86-64_deb.tar.gz

# 真實下載地址
# 1- main package (不分語言,語言部分由單獨的安裝包langpack處理)
http://download.documentfoundation.org/libreoffice/stable/5.2.5/deb/x86_64/LibreOffice_5.2.5_Linux_x86-64_deb.tar.gz

# 2- help package
http://download.documentfoundation.org/libreoffice/stable/5.2.5/deb/x86_64/LibreOffice_5.2.5_Linux_x86-64_deb_helppack_zh-TW.tar.gz

# 3- language package (英文爲默認語言,無語言包)
http://download.documentfoundation.org/libreoffice/stable/5.2.5/deb/x86_64/LibreOffice_5.2.5_Linux_x86-64_deb_langpack_zh-TW.tar.gz

Download 按鈕的地址的HTTP狀態碼爲 200,命令wgetcurl無法自動重定向至真實的下載地址,故只能根據需要拼湊真實下載地址。

以下鏈接獲取自Old Archive,爲真實下載地址

# 1- main package (不分語言,語言部分由單獨的安裝包langpack處理)
https://downloadarchive.documentfoundation.org/libreoffice/old/5.2.5.1/deb/x86_64/LibreOffice_5.2.5.1_Linux_x86-64_deb.tar.gz

# 2- help package
https://downloadarchive.documentfoundation.org/libreoffice/old/5.2.5.1/deb/x86_64/LibreOffice_5.2.5.1_Linux_x86-64_deb_helppack_zh-TW.tar.gz

# 3- language package (英文爲默認語言,無語言包)
https://downloadarchive.documentfoundation.org/libreoffice/old/5.2.5.1/deb/x86_64/LibreOffice_5.2.5.1_Linux_x86-64_deb_langpack_zh-TW.tar.gz

分析 * 使用deb安裝包的是Debian/Ubuntu,安裝命令爲aptapt-getdpkg; * 使用rpm安裝包的是RedHat/CentOS/Fedora/SUSE/OpenSUSE,安裝命令爲rpmyumzypper; * 硬件架構由命令uname -m獲取; * 具體版本號由本文Release Version Check中的命令獲取; * 語言手動指定(en-USzh-TW)

通過如下Shell Script拼湊需要的真實下載鏈接

# !/bin/env bash

# package type
if command -v zypper &> /dev/null; then
    packageType='rpm'
elif command -v yum &> /dev/null; then
    packageType='rpm'
elif command -v rpm &> /dev/null; then
    packageType='rpm'
elif command -v apt-get &> /dev/null; then
    packageType='deb'
elif command -v dpkg &> /dev/null; then
    packageType='deb'
fi

# latest statble version
proximateVersionCheck=$(curl -fsSL https://www.libreoffice.org/download/release-notes | sed -r -n '/name="Still"/s@<[^>]*>@@gp' | awk '{print $2}')

preciseVersionCheck=$(curl -fsSL https://downloadarchive.documentfoundation.org/libreoffice/old/ | sed -r -n '/'"$proximateVersionCheck"'/s@.*(<a .*</a>).*@\1@gp' | sed -n 's@/@@g;$s@<[^>]*>@@gp')

# hardware arch
arch=$(uname -m)

# language check
lang='en-US'

### Approximate Version Link (use : comment)###
: ' #https://www.cyberciti.biz/faq/bash-comment-out-multiple-line-code/
mainPackLink='http://download.documentfoundation.org/libreoffice/stable/'"$proximateVersionCheck"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$proximateVersionCheck"'_Linux_'"${arch//_/-}"'_'"$packageType"'.tar.gz'

helpPackLink='http://download.documentfoundation.org/libreoffice/stable/'"$proximateVersionCheck"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$proximateVersionCheck"'_Linux_'"${arch//_/-}"'_'"$packageType"'_helppack_'"$lang"'.tar.gz'

if [[ "$lang" != 'en-US' ]]; then
    langPackLink='http://download.documentfoundation.org/libreoffice/stable/'"$proximateVersionCheck"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$proximateVersionCheck"'_Linux_'"${arch//_/-}"'_'"$packageType"'_langpack_'"$lang"'.tar.gz'
fi
'

### Precise Version Link ###
mainPackLink='https://downloadarchive.documentfoundation.org/libreoffice/old/'"$preciseVersionCheck"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$preciseVersionCheck"'_Linux_'"${arch//_/-}"'_'"$packageType"'.tar.gz'

helpPackLink='https://downloadarchive.documentfoundation.org/libreoffice/old/'"$preciseVersionCheck"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$preciseVersionCheck"'_Linux_'"${arch//_/-}"'_'"$packageType"'_helppack_'"$lang"'.tar.gz'

if [[ "$lang" != 'en-US' ]]; then
    langPackLink='https://downloadarchive.documentfoundation.org/libreoffice/old/'"$preciseVersionCheck"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$preciseVersionCheck"'_Linux_'"${arch//_/-}"'_'"$packageType"'_langpack_'"$lang"'.tar.gz'
fi


[[ -n "$mainPackLink" ]] && echo "$mainPackLink"
[[ -n "$helpPackLink" ]] && echo "$helpPackLink"
[[ -n "$langPackLink" ]] && echo "$langPackLink"

Decompressing

因壓縮包格式爲.tar.gz,若要解壓該格式文件。系統須安裝targzip命令,執行如下命令進行安裝

# RedHat/CentOS/Fedora
sudo yum install -y tar gzip

# Debian/Ubuntu
sudo apt-get update
sudo apt-get -qy install tar gzip

# SUSE/OpenSUSE
sudo zypper ref -f
sudo zypper in -ly tar gzip

此處假設下載後的文件名稱如下

Type Package Name
MainPack LibreOffice.tar.gz
HelpPack LibreOffice_help.tar.gz
LangPack LibreOffice_lang.tar.gz

使用如下命令進行解壓

sudo tar xf LibreOffice.tar.gz
sudo tar xf LibreOffice_help.tar.gz
sudo tar xf LibreOffice_lang.tar.gz

如果不指定-C選項,則解壓至當前目錄。安裝包在生成目錄的子目錄DEBSRPMS中,目錄結構如下

# tree -L 2
.
├── DEBS
│   ├── libobasis5.2-base_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-calc_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-core_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-draw_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-en-us_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-en-us-base_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-en-us-calc_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-en-us-math_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-en-us-res_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-en-us-writer_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-extension-beanshell-script-provider_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-extension-javascript-script-provider_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-extension-mediawiki-publisher_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-extension-nlpsolver_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-extension-pdf-import_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-extension-report-builder_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-filter-data_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-gnome-integration_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-graphicfilter_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-images_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-impress_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-kde-integration_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-librelogo_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-math_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-ogltrans_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-onlineupdate_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-ooofonts_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-ooolinguistic_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-postgresql-sdbc_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-python-script-provider_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-pyuno_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-writer_5.2.5.1-1_amd64.deb
│   ├── libobasis5.2-xsltfilter_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-base_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-calc_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-debian-menus_5.2.5-1_all.deb
│   ├── libreoffice5.2-dict-en_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-dict-es_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-dict-fr_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-draw_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-en-us_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-impress_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-math_5.2.5.1-1_amd64.deb
│   ├── libreoffice5.2-ure_5.2.5.1-1_amd64.deb
│   └── libreoffice5.2-writer_5.2.5.1-1_amd64.deb
└── readmes
    └── README_en-US

2 directories, 47 files

安裝時,需注意安裝包的真實路徑。

Installation

LibreOffice在GNU/Linux的詳細安裝說明,見官方文檔 Installing LibreOffice on Linux。本文支持RedHat/CentOS/Fedora、Debian/Ubuntu、SUSE/OpenSUSE等GNU/Linux。

LibreOffice默認安裝路徑爲/opt,如

/opt/libreoffice5.2

安裝之前需將舊版本的LibreOffice卸載,在解壓後的目錄中執行如下命令

# package type
if command -v zypper &> /dev/null; then
    sudo zypper rm -y libreoffice*
    sudo zypper in -ly ./RPMS/*.rpm
elif command -v yum &> /dev/null; then
    sudo yum erase -y libreoffice*
    sudo yum localinstall -y ./RPMS/*.rpm
elif command -v apt-get &> /dev/null; then
    sudo apt-get purge -y libreoffice*
    sudo dpkg -i ./DEBS/*.deb
fi

安裝完成後,在Applications–>Office中,可看到LibreOffice的相關應用程序。

item path
安裝路徑 /opt/libreoffice5.2
配置文件路徑 ~/.config/libreoffice
可執行命令路徑 /usr/bin/libreoffice5.2

版本檢測命令

# libreoffice5.2 --version
LibreOffice 5.2.5.1 0312e1a284a7d50ca85a365c316c7abbf20a4d22

Configuration

使用配置

Document Background Color

背景色更改,選擇Pale green,在任務欄依次點擊 Options–>LibreOffice–>Application Colors–>Document background–>Pale green

Shell Script

該Shell Script用於安裝/更新LibreOffice

#!/bin/env bash
#Official Site: https://www.libreoffice.org
#Target: Automatically Install & Update LibreOffice On GNU/Linux
#Writer: LempStacker
#Date: Mar 01, 2017 14:01~20:15 Asia/Shanghai

printf "Usage: Installing / Updating LibreOffice On GNU/Linux!\n"
# 配置文件路徑 ~/.config/libreoffice


########  0. Initialization Check  ########
c_red='\e[31;1m'    # font color
c_blue='\e[34m'       # font color
c_end='\e[0m'       # font color

[[ "$UID" -ne 0 ]] && printf "Sorry, $c_red%s$c_end Privilege Is Required For Execuring This Shell Script!\n" "root" && exit 1    # 1. Check Root or sudo Privilege, root uid is 0

#$USER exist && $SUDO_USER not exist, then use $USER
# [[ -n $USER && -z $SUDO_USER ]] && nowUser="$USER" || nowUser="$SUDO_USER"

# CentOS: iproute  Debian/OpenSUSE: iproute2
! ping -q -w 1 -c 1 $(ip route | awk 'match($1,/^default/){print $3}') &> /dev/null && printf "%s\n" "Error: No Internet Connection, Please Check It!" && exit 2    # 2. Check Internet Connection

if command -v curl &> /dev/null; then
    # downloadTool='curl -fSL# --retry 5 --retry-delay 1'   # curl -s URL -o /PATH/FILE
    downloadTool='curl -fsSL --retry 5 --retry-delay 1'   # curl -s URL -o /PATH/FILE
elif command -v wget &> /dev/null; then
    downloadTool='wget -qO-' # wget -q URL -O /PATH/FILE
else
    printf "Error: Can't Find Command $c_red%s$c_end or $c_red%s$c_end!\n" "curl" "wget"
    exit 3
fi      # 3. Download Tool (curl, wget) Check

if ! command -v sha256sum &> /dev/null && ! command -v openssl &> /dev/null; then
    printf "Error: Can't Find Command $c_red%s$c_end or $c_red%s$c_end!\n" "sha256sum" "openssl"
    exit 4
fi

! command -v tar &> /dev/null && printf "Error: Can't Find Command $c_red%s$c_end To Decompress .gz File!\n" "tar" && exit 5

# CentOS/Debian/OpenSUSE: gzip
! command -v gzip &> /dev/null && printf "Error: Can't Find Command $c_red%s$c_end To Decompress .gz File!\n" "gzip" && exit 5   # 5. Decompress Command Check gzip for .gz

# Custom function - sha256 verify
sha256VerifyFunc(){
    local filePath="$1"
    if [[ -f "$filePath" ]]; then
        if command -v sha256sum &> /dev/null; then
            result=$(sha256sum "$filePath" | awk '{print $1}')
        elif command -v openssl &> /dev/null; then
            result=$(openssl dgst -sha256 "$filePath" | awk '{print $NF}')
        fi  # End if
    else
        result=0    # file not exists
    fi  # End if
    unset filePath
    echo $result
}


########  1. Variables Setting  ########
officialSite='https://www.libreoffice.org/'   #LibreOffice官網
downloadPage='https://www.libreoffice.org/download/download/'   #下載頁面
downloadLink='http://download.documentfoundation.org'   #真實下載鏈接(proximateVersion)
oldArchivePage='https://downloadarchive.documentfoundation.org/libreoffice/old' #真實下載地址(preciseVersion)
releasePage='https://www.libreoffice.org/download/release-notes'    #版本釋出信息

saveDir='/tmp'  #軟件包存放路徑
flag=0  #判斷LibreOffice是否安裝,默認爲0 未安裝, 1 已安裝有

startTime=$(date +'%s')     #操作起始時間
########  2. Latest Stable Version Check  ########
proximateLatestVersion=$($downloadTool "$releasePage" | sed -r -n '/name="Still"/s@<[^>]*>@@gp' | awk '{print $2}')

# echo 'proximateVersion :'$proximateLatestVersion

preciseLatestVersion=$($downloadTool "$oldArchivePage" | sed -r -n '/'"$proximateLatestVersion"'/s@.*(<a .*</a>).*@\1@gp' | sed -n 's@/@@g;$s@<[^>]*>@@gp')

# echo 'preciseLatestVersion :'$preciseLatestVersion

latestVersion="$preciseLatestVersion"

[[ -z "$latestVersion" ]] && printf "$c_red%s$c_end, Fail To Detect Current Latest Stable Version. Please Try Later Again.!\n" "Error" && exit 21


########  3. Check Software Installed Or Not  ########
ls /usr/bin/libreoffice* &> /dev/null   # check binary program exists or not

if [[ $? -eq 0 ]]; then
    for i in `ls /usr/bin/libreoffice*`;do
        currentVersion=$($i --version | awk '{print $2;exit}')

        if [[ "$latestVersion" == "$currentVersion" ]]; then
            printf "There Has Been Existed The Latest Stable Version ($c_red%s$c_end) On Your System!\n" "$latestVersion" && exit 31
        else
            printf "Your Current Version ($c_red%s$c_end) Is Older Then The Latest Stable Version ($c_red%s$c_end)!\n" "$currentVersion" "$latestVersion"
            flag=1
        fi  # End if

        break
    done    # Endo for

else
    printf "You Haven't Installed LibreOffice, Latest Stable Version Is ($c_red%s$c_end)!\n" "$latestVersion"
fi  #End if


########  4. Download & Verify Latest Software  ########
# package type
if command -v zypper &> /dev/null; then
    packageType='rpm'
elif command -v yum &> /dev/null; then
    packageType='rpm'
elif command -v rpm &> /dev/null; then
    packageType='rpm'
elif command -v apt-get &> /dev/null; then
    packageType='deb'
elif command -v dpkg &> /dev/null; then
    packageType='deb'
fi

arch=$(uname -m)    # hardware arch
# lang='zh-TW'    # language choose
lang='en-US'    # language choose
[[ "$lang" == 'en-US' ]] && langFlag=0 || langFlag=1

# - Approximate Version Link (use : comment multiple line)
: ' #https://www.cyberciti.biz/faq/bash-comment-out-multiple-line-code/
mainPackLink='http://download.documentfoundation.org/libreoffice/stable/'"$proximateLatestVersion"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$proximateLatestVersion"'_Linux_'"${arch//_/-}"'_'"$packageType"'.tar.gz'

helpPackLink='http://download.documentfoundation.org/libreoffice/stable/'"$proximateLatestVersion"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$proximateLatestVersion"'_Linux_'"${arch//_/-}"'_'"$packageType"'_helppack_'"$lang"'.tar.gz'

if [[ "$lang" != 'en-US' ]]; then
    langPackLink='http://download.documentfoundation.org/libreoffice/stable/'"$proximateLatestVersion"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$proximateLatestVersion"'_Linux_'"${arch//_/-}"'_'"$packageType"'_langpack_'"$lang"'.tar.gz'
fi
'

# - Precise Version Link
mainPackLink='https://downloadarchive.documentfoundation.org/libreoffice/old/'"$latestVersion"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$latestVersion"'_Linux_'"${arch//_/-}"'_'"$packageType"'.tar.gz'

helpPackLink='https://downloadarchive.documentfoundation.org/libreoffice/old/'"$latestVersion"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$latestVersion"'_Linux_'"${arch//_/-}"'_'"$packageType"'_helppack_'"$lang"'.tar.gz'

[[ "$langFlag" -eq 1 ]] && langPackLink='https://downloadarchive.documentfoundation.org/libreoffice/old/'"$latestVersion"'/'"$packageType"'/'"$arch"'/LibreOffice_'"$latestVersion"'_Linux_'"${arch//_/-}"'_'"$packageType"'_langpack_'"$lang"'.tar.gz'

# - Temp Save Path
mainPackSaveName="$saveDir/LibreOffice_$latestVersion.$lang.tar.gz"
helpPackSaveName="$saveDir/LibreOffice_helppack_$latestVersion.$lang.tar.gz"
[[ "$langFlag" -eq 1 ]] && langPackSaveName="$saveDir/LibreOffice_langpack_$latestVersion.$lang.tar.gz"

[[ -f "$mainPackSaveName" ]] && rm -f "$mainPackSaveName"
[[ -f "$helpPackSaveName" ]] && rm -f "$helpPackSaveName"
[[ "$langFlag" -eq 1 ]] && [[ -f "$langPackSaveName" ]] && rm -f "$langPackSaveName"

printf "Begin To Download Latest Version $c_red%s$c_end, Just Be Patient!\n" "$latestVersion"

# Custom Function - Package Download & SHA-256 Verify
packDownloadFunc(){
    local packLink="$1"
    local packSaveName="$2"
    local num="$3"
    [[ -z "$num" ]] && num=0

    $downloadTool "$packLink" > "$packSaveName"

    if [[ -f "$packSaveName" ]]; then
        local PackSHA256=$(sha256VerifyFunc "$packSaveName")
        # echo "$PackSHA256"

        if [[ "$PackSHA256" == 0 ]]; then
            printf "Sorry, Package $c_blue%s$c_end Not Exists!\n" "${packSaveName}" && exit 4"$num"
        else
            local PackSHA256Origin=$($downloadTool "$packLink.mirrorlist" | sed -n -r '/SHA-256/s@<[^>]*>@@g;s@.*Hash: (.*)@\1@gp')
            # echo 'PackSHA256Origin :'$PackSHA256Origin
            if [[ "$PackSHA256" == "$PackSHA256Origin" ]]; then
                printf "Package $c_blue%s$c_end Approves SHA-256 Check!\n" "${packSaveName##*/}"
            else
                printf "Error, Package $c_blue%s$c_end SHA-256 Check Inconsistency! The Package May Not Be Integrated!\n" "${packSaveName##*/}" && exit 4"$num"
            fi  # End if

            unset PackSHA256Origin
        fi  # End if
        unset packLink
        unset packSaveName
        unset num
        unset PackSHA256

    fi  # End if
}

### - Docnload & Verify Controller (Very Important) - ###
packDownloadFunc "$mainPackLink" "$mainPackSaveName" 1
packDownloadFunc "$helpPackLink" "$helpPackSaveName" 2
[[ "$langFlag" -eq 1 ]] &&  packDownloadFunc "$langPackLink" "$langPackSaveName" 3


########  5. Decompression & Installation   ########
if command -v zypper &> /dev/null; then
    sudo zypper rm -y libreoffice* &> /dev/null
    installCommand='zypper in -ly'
elif command -v yum &> /dev/null; then
    sudo yum erase -y libreoffice* &> /dev/null
    installCommand='yum localinstall -y'
elif command -v apt-get &> /dev/null; then
    sudo apt-get purge -y libreoffice* &> /dev/null
    installCommand='dpkg -i'
fi

mainPackExtractPath="$saveDir/LibreOffice"
helpPackExtractPath="$saveDir/LibreOffice_helppack"
[[ "$langFlag" -eq 1 ]] &&  langPackExtractPath="$saveDir/LibreOffice_langpack"
# 不能放在同一個目錄中安裝,help的pack依賴mian中的pack

# Custom Function - Decompression & Extraction Files
decompressInstallationFileFunc(){
    local packSaveName="$1"
    local packExtractPath="$2"
    local packType="$3"

    if [[ -f "$packSaveName" ]]; then
        [[ -d "$packExtractPath" ]] && rm -rf "$packExtractPath"
        mkdir -p "$packExtractPath"
        tar xf "$packSaveName" -C "$packExtractPath" --strip-components=1
        local packPath="$packExtractPath"/"${packType^^}S"
        if [[ -d "$packPath" ]]; then
            $installCommand "$packPath"/*."$packType" 1> /dev/null
        fi
    fi
    unset packSaveName
    unset packExtractPath
    unset packPath
    unset packType
}

if [[ $flag -eq 1 ]]; then
    printf "Begin To $c_red%s$c_end LibreOffice!\n" "Update"
else
    printf "Begin To $c_red%s$c_end LibreOffice!\n" "Install"
fi

### - Decompression & Extraction & Installation Controller (Very Important) - ###
decompressInstallationFileFunc "$mainPackSaveName" "$mainPackExtractPath" "$packageType"
decompressInstallationFileFunc "$helpPackSaveName" "$helpPackExtractPath" "$packageType"
[[ "$langFlag" -eq 1 ]] && decompressInstallationFileFunc "$langPackSaveName" "$langPackExtractPath" "$packageType"

ls /usr/bin/libreoffice* &> /dev/null   # check binary program exists or not

if [[ $? -eq 0 ]]; then
    for i in `ls /usr/bin/libreoffice*`;do
        newInstallVersion=$($i --version | awk '{print $2;exit}')

        if [[ "$latestVersion" == "$newInstallVersion" ]]; then
            # delete temporary files
            [[ -f "$mainPackSaveName" ]] && rm -f "$mainPackSaveName"
            [[ -f "$helpPackSaveName" ]] && rm -f "$helpPackSaveName"
            [[ -d "$mainPackExtractPath" ]] && rm -rf "$mainPackExtractPath"
            [[ -d "$helpPackExtractPath" ]] && rm -rf "$helpPackExtractPath"
            if [[ "$langFlag" -eq 1 ]]; then
                [[ -f "$langPackSaveName" ]] && rm -f "$langPackSaveName"
                [[ -d "$langPackExtractPath" ]] && rm -rf "$langPackExtractPath"
            fi

            if [[ $flag -eq 1 ]]; then
                printf "LibreOffice Was Updated To Version $c_red%s$c_end Successfully!\n" "$latestVersion"
            else
                printf "Installing LibreOffice Version $c_red%s$c_end Successfully!\n" "$latestVersion"
            fi

        fi  # End if

        break
    done    # Endo for

else
    printf "Sorry, Fail To Install LibreOffice, Please Try Later Again\n"
fi  #End if


########  6.Operation Time Cost  ########
finishTime=$(date +'%s')        # 操作結束時間
totalTimeCost=$((finishTime-startTime))   # 操作耗時
printf "Total Time Cost Is $c_red%s$c_end Seconds!\n" "$totalTimeCost"


########  7.Unset Viriables & Remove Temp File  ########
unset c_red
unset c_blue
unset c_end
unset downloadTool
unset officialSite
unset downloadPage
unset downloadLink
unset oldArchivePage
unset releasePage
unset saveDir
unset flag
unset proximateLatestVersion
unset preciseLatestVersion
unset latestVersion
unset packageType
unset arch
unset lang
unset mainPackLink
unset helpPackLink
unset langPackLink
unset mainPackSaveName
unset helpPackSaveName
unset langPackSaveName
unset mainPackExtractPath
unset helpPackExtractPath
unset langPackExtractPath
unset installCommand
unset startTime
unset finishTime
unset totalTimeCost

# Script End

Usage Examples

Shell Script實例演示

  • Installing

Usage: Installing / Updating LibreOffice On GNU/Linux! You Haven’t Installed LibreOffice, Latest Stable Version Is (5.2.5.1)! Begin To Download Latest Version 5.2.5.1, Just Be Patient! Package LibreOffice_5.2.5.1.en-US.tar.gz Approves SHA-256 Check! Package LibreOffice_helppack_5.2.5.1.en-US.tar.gz Approves SHA-256 Check! Begin To Install LibreOffice! Installing LibreOffice Version 5.2.5.1 Successfully! Total Time Cost Is 396 Seconds!

  • Updating

Usage: Installing / Updating LibreOffice On GNU/Linux! Your Current Version (5.2.3.3) Is Older Then The Latest Stable Version (5.2.5.1)! Begin To Download Latest Version 5.2.5.1, Just Be Patient! Package LibreOffice_5.2.5.1.en-US.tar.gz Approves SHA-256 Check! Package LibreOffice_helppack_5.2.5.1.en-US.tar.gz Approves SHA-256 Check! Begin To Update LibreOffice! LibreOffice Was Updated To Version 5.2.5.1 Successfully! Total Time Cost Is 385 Seconds!

Change Logs

  • 2017.03.01 20:15 Wed Asia/Shanghai
    • 初稿完成