Skip to content

Commit 15c0ae6

Browse files
authored
Merge pull request #104 from 1328032567/update-bash-script
Fix install.sh bug and Add new Language selection feature
2 parents 6a81785 + 5405ce9 commit 15c0ae6

File tree

2 files changed

+203
-31
lines changed

2 files changed

+203
-31
lines changed

tool/install.sh

100644100755
Lines changed: 143 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,109 +13,221 @@
1313
# ---------------------------------------------------------------
1414

1515
install_dir=""
16+
version="pre"
1617
path_to_executable=""
1718
default_install_path="/usr/local/bin"
19+
noroot_default_install_path="$HOME/.local/bin"
1820
binary_name="chsrc"
19-
version=""
21+
temp_install_dir="" # 用于存储临时安装目录
22+
helpflag=0
23+
lan="zh"
2024

25+
# 输出相关信息
2126
info() {
2227
echo "[INFO] $*"
2328
}
2429

30+
# 输出错误到stdout和stderr
2531
error() {
2632
echo -e "[ERROR] $*" >&2
2733
exit 1
2834
}
2935

36+
# 显示 chsrc 安装程序的帮助信息,包括使用说明和可用选项
3037
help() {
31-
echo "chsrc-installer: Install chsrc on any Unix-like OS and any architect "
32-
echo
33-
echo "使用: install.sh [options]"
34-
echo "选项:"
35-
echo "-h 打印此帮助信息"
36-
echo "-d <destination> 指定安装目录,默认为 /usr/local/bin;若已安装,则覆盖旧版本"
37-
echo "-v <version> 指定chsrc版本"
38-
echo
38+
if [ "$lan" = "zh" ]; then
39+
echo "chsrc-installer: 在任何类Unix操作系统上安装 chsrc"
40+
echo
41+
echo "使用: install.sh [选项]"
42+
echo "选项:"
43+
echo " -h 打印此帮助信息"
44+
echo " -d <destination> 指定安装目录,默认为 /usr/local/bin;若已安装,则覆盖旧版本"
45+
echo " -v <version> 指定 chsrc 版本"
46+
echo " -l <language> 指定脚本语言,支持 zh 和 en "
47+
echo
48+
else
49+
echo "chsrc-installer: Install chsrc on any Unix-like OS"
50+
echo
51+
echo "Usage: install.sh [options]"
52+
echo "Options:"
53+
echo " -h Print this help information"
54+
echo " -d <destination> Specify installation directory, default is /usr/local/bin; will overwrite if already installed"
55+
echo " -v <version> Specify chsrc version"
56+
echo " -l <language> Specify script language, supports zh and en"
57+
echo
58+
fi
3959
}
4060

41-
61+
# 确定下载路径
4262
set_install_path() {
4363
if [ -n "$install_dir" ]; then
4464
# 扩展 ~ 符号
4565
install_dir="${install_dir/#\~/$HOME}"
66+
67+
# 检查路径是否存在,如果不存在则创建该路径
68+
if [ ! -d "$install_dir" ]; then
69+
# 多种语言输出
70+
if [ "$lan" = "zh" ]; then
71+
echo "目录 $install_dir 不存在,正在创建..."
72+
else
73+
echo "Directory $install_dir does not exist. Creating..."
74+
fi
75+
# 多语言输出
76+
if ! mkdir -p "$install_dir"; then
77+
if [ "$lan" = "zh" ]; then
78+
echo "创建目录失败,请重试"
79+
else
80+
echo "Failed to create directory, please try again"
81+
fi
82+
exit 1
83+
fi
84+
85+
temp_install_dir="$install_dir" # 记录临时安装目录
86+
fi
87+
4688
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
47-
info "$binary_name 已安装,更新路径: ${existing_path}"
89+
90+
if [ "$lan" = "zh"]; then
91+
info "$binary_name 已安装,更新路径: ${existing_path}"
92+
else
93+
info "$binary_name is already installed, updating path: ${existing_path}"
94+
fi
95+
4896
install_dir=$(dirname "$existing_path")
4997
else
5098
# 检查默认路径
5199
if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then
52100
install_dir="$default_install_path"
101+
else if [ -d "$noroot_default_install_path" ] && [ -w "$noroot_default_install_path" ]; then
102+
install_dir="$noroot_default_install_path"
53103
else
54-
error "默认下载路径 /usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它路径安装"
104+
if [ "$lan" = "zh"]; then
105+
error "默认下载路径 /usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它路径安装"
106+
else
107+
error "Default download path /usr/local/bin is not writable. Please run the script with sudo; or specify another path using the -d option."
108+
fi
109+
55110
fi
56111
fi
57112
}
58113

59-
114+
# 从Gitee仓库安装 指定架构,操作系统,版本 的chsrc二进制文件
60115
install() {
61116
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
62117

63118
case "$arch" in
64119
x86_64) arch="x64" ;;
65-
aarch64) arch="aarch64" ;;
120+
aarch64|arm64) arch="aarch64" ;;
66121
riscv64) arch="riscv64" ;;
67122
armv7*) arch="armv7" ;;
68-
*) error "不支持的架构: ${arch}" ;;
123+
*)
124+
if [ "$lan" = "zh" ]; then
125+
error "不支持的架构: ${arch}"
126+
else
127+
error "Unsupported architecture: ${arch}"
128+
fi
129+
;;
69130
esac
70131

71132
platform="$(uname -s | awk '{print tolower($0)}')"
72133

73134
case "$platform" in
74135
linux) platform="linux" ;;
75136
darwin) platform="macos" ;;
76-
*) error "不支持的平台: ${platform}" ;;
137+
*)
138+
if [ "$lan" = "zh" ]; then
139+
error "不支持的平台: ${platform}"
140+
else
141+
error "Unsupported platform: ${platform}"
142+
fi
143+
;;
77144
esac
78145

79-
if [ -n "$version" ]; then
80-
url="https://gitee.com/RubyMetric/chsrc/releases/download/v${version}/${binary_name}-${arch}-${platform}"
81-
else
82-
url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}"
83-
version="latest"
146+
if [[ ! "$version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then
147+
# version 不符合条件,报错
148+
if [ "$lan" = "zh" ]; then
149+
error "不支持的版本: ${version},版本号必须在 0.1.4 到 0.1.9 之间或为 'pre'"
150+
else
151+
error "Unsupported version: ${version}. Version number must be between 0.1.4 and 0.1.9 or 'pre'"
152+
fi
84153
fi
85154

86-
path_to_executable="${install_dir}/${binary_name}"
155+
url="https://gitee.com/RubyMetric/chsrc/releases/download/${version}/${binary_name}-${arch}-${platform}"
87156

88-
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台) 到 ${path_to_executable}"
157+
path_to_executable="${install_dir}/${binary_name}"
158+
159+
if [ "$lan" = "zh" ]; then
160+
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}"
161+
else
162+
info "Downloading ${binary_name} (${arch} architecture, ${platform} platform, version ${version}) to ${path_to_executable}"
163+
fi
89164

90-
# 下载文件并设置权限
91165
if curl -sL "$url" -o "$path_to_executable"; then
92166
chmod +x "$path_to_executable"
93-
info "🎉 安装完成,版本: $version,路径: $path_to_executable"
167+
168+
if [ "$lan" = "zh" ]; then
169+
info "🎉 安装完成,版本: $version,路径: $path_to_executable"
170+
else
171+
info "🎉 Installation completed, path: $path_to_executable"
172+
fi
173+
94174
else
95-
error "下载失败,请检查您的网络连接和代理设置: ${url}"
175+
if [ "$lan" = "zh" ]; then
176+
error "下载失败,请检查您的网络连接和代理设置: ${url}"
177+
else
178+
error "Download failed, please check your network connection and proxy settings: ${url}"
179+
fi
180+
181+
fi
182+
}
183+
184+
# 清理函数
185+
cleanup() {
186+
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then
187+
188+
if [ "$lan" = "zh" ]; then
189+
echo "清理创建的目录: $temp_install_dir"
190+
else
191+
echo "Cleaning up created directory: $temp_install_dir"
192+
fi
193+
rm -rf "$temp_install_dir"
96194
fi
97195
}
98196

197+
# 设置 trap 以捕获退出信号
198+
trap cleanup EXIT
99199

100-
# main
101-
while getopts ":hd:v:" option; do
200+
# 从命令行读取 安装路径与版本号
201+
while getopts ":hd:v:l:" option; do
102202
case $option in
103203
h)
104-
help
105-
exit 0
204+
helpflag=1
106205
;;
107206
d)
108207
install_dir=${OPTARG}
109208
;;
110209
v)
111210
version=${OPTARG}
112211
;;
212+
l)
213+
lan=${OPTARG}
214+
;;
113215
\?)
114216
echo "无效的命令行选项,请使用 -h 查看帮助"
115217
exit 1
116218
;;
117219
esac
118220
done
119221

222+
# 判断语言的类型,不符合直接退出
223+
if [[ "$lan" != "zh" && "$lan" != "en" ]]; then
224+
error "无效的语言选项: $lan。支持的选项是 zh 和 en"
225+
fi
226+
227+
if [ "$helpflag" -eq 1 ]; then
228+
help
229+
exit 0;
230+
fi
231+
120232
set_install_path
121-
install
233+
install

tool/readme.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
###########################################################################
2+
# Chsrc 相关工具
3+
# 脚本工具
4+
###########################################################################
5+
6+
************
7+
1. 概述
8+
************
9+
10+
此文件夹包含许多与安装 Chsrc 二进制文件相关的脚本工具,您可以使用这些工具指定目录路径和发布版本。
11+
12+
********
13+
2. 文件
14+
********
15+
16+
install.sh Bash 安装工具
17+
install.ps1 Powershell 安装工具。
18+
reademe.txt 此文件
19+
20+
*******************
21+
3. 安装
22+
*******************
23+
24+
步骤1:输入“sudo chmod u+x ./install.sh”以使脚本可执行。
25+
26+
步骤2:输入“./install.sh [-h] [-d <install_directory>] [-v <version>] [-l <language>]”指定安装路径,发布版本和脚本语言。
27+
28+
步骤3:如果出现错误,请检查您的网络连接并确保您可以访问Gitee。
29+
30+
31+
32+
#####################################################################
33+
# Chsrc realated tools
34+
# Scripts Tools
35+
# English Version
36+
#####################################################################
37+
38+
************
39+
1. Overview
40+
************
41+
42+
This folder contains many script tools realated to install Chsrc binary file,
43+
you can use the tools to specify directory path and release version.
44+
45+
********
46+
2. Files
47+
********
48+
49+
install.sh Bash install tool.
50+
reademe.txt This file
51+
52+
*******************
53+
3. Install
54+
*******************
55+
56+
Step 1: Type "sudo chmod u+x ./install.sh" to make scrpit executable.
57+
58+
Step 2: Type "./install.sh [-h] [-d <install_directory>] [-v <version>] [-l <language>]" to specify install path and release version also language.
59+
60+
Step 3: If an error occurs, please check your internet connection and ensure that you can access Gitee.

0 commit comments

Comments
 (0)