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源碼實現)

1
2
3
4
5
6
7
8
9
10
# 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表格

1
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)}'

輸出內容格式

1
2
3
4
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列表中獲取精確的版本號(逆序,只取一個)。

1
2
3
4
5
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>).*@\[email protected]' | sed -n 's@/@@g;$s@<[^>]*>@@gp'
unset proximateVersionCheck

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

Installed Version Check

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

1
2
3
4
5
6
7
8
9
10
# 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

實例演示

1
2
3
[email protected]:~> 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
[email protected]:~>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 瀏覽器地址欄中生成的地址
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
2
3
4
5
6
7
8
# 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拼湊需要的真實下載鏈接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# !/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>).*@\[email protected]' | 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命令,執行如下命令進行安裝

1
2
3
4
5
6
7
8
9
10
# 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

使用如下命令進行解壓

1
2
3
sudo tar xf LibreOffice.tar.gz
sudo tar xf LibreOffice_help.tar.gz
sudo tar xf LibreOffice_lang.tar.gz

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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,如

1
/opt/libreoffice5.2

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

1
2
3
4
5
6
7
8
9
10
11
# 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

版本檢測命令

1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/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>).*@\[email protected]' | 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: (.*)@\[email protected]')
# 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
    • 初稿完成