Skip to content

Commit bde246d

Browse files
committed
update
1 parent d13e0f3 commit bde246d

30 files changed

Lines changed: 746 additions & 311 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<h1 align="center">linux-tutorial</h1>
1212

13+
> 📚 **linux-tutorial** 是一个 Linux 教程。
14+
>
1315
> 🔁 项目同步维护在 [github](https://github.com/dunwu/linux-tutorial) | [gitee](https://gitee.com/turnon/linux-tutorial)
1416
>
1517
> 📖 [电子书](https://dunwu.github.io/linux-tutorial/) | [电子书(国内)](http://turnon.gitee.io/linux-tutorial/)

codes/linux/lib/env.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
# ------------------------------------------------------------------------------ 颜色状态
4+
5+
# Regular Color
6+
C_BLACK="\033[0;30m"
7+
C_RED="\033[0;31m"
8+
C_GREEN="\033[0;32m"
9+
C_YELLOW="\033[0;33m"
10+
C_BLUE="\033[0;34m"
11+
C_MAGENTA="\033[0;35m"
12+
C_CYAN="\033[0;36m"
13+
C_WHITE="\033[0;37m"
14+
15+
# Bold Color
16+
C_B_BLACK="\033[1;30m"
17+
C_B_RED="\033[1;31m"
18+
C_B_GREEN="\033[1;32m"
19+
C_B_YELLOW="\033[1;33m"
20+
C_B_BLUE="\033[1;34m"
21+
C_B_MAGENTA="\033[1;35m"
22+
C_B_CYAN="\033[1;36m"
23+
C_B_WHITE="\033[1;37m"
24+
25+
# Underline Color
26+
C_U_BLACK="\033[4;30m"
27+
C_U_RED="\033[4;31m"
28+
C_U_GREEN="\033[4;32m"
29+
C_U_YELLOW="\033[4;33m"
30+
C_U_BLUE="\033[4;34m"
31+
C_U_MAGENTA="\033[4;35m"
32+
C_U_CYAN="\033[4;36m"
33+
C_U_WHITE="\033[4;37m"
34+
35+
# Background Color
36+
C_BG_BLACK="\033[40m"
37+
C_BG_RED="\033[41m"
38+
C_BG_GREEN="\033[42m"
39+
C_BG_YELLOW="\033[43m"
40+
C_BG_BLUE="\033[44m"
41+
C_BG_MAGENTA="\033[45m"
42+
C_BG_CYAN="\033[46m"
43+
C_BG_WHITE="\033[47m"
44+
45+
# Reset Color
46+
C_RESET="$(tput sgr0)"
47+
48+
# ------------------------------------------------------------------------------ 常用状态值
49+
50+
YES=0
51+
NO=1
52+
SUCCEED=0
53+
FAILED=1

codes/linux/lib/file.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env bash
2+
3+
# 装载其它库
4+
ROOT=`dirname ${BASH_SOURCE[0]}`
5+
source ${ROOT}/env.sh
6+
7+
# ------------------------------------------------------------------------------ 文件操作函数
8+
9+
# 文件是否存在
10+
isFileExists() {
11+
if [[ -e $1 ]]; then
12+
return ${YES}
13+
else
14+
return ${NO}
15+
fi
16+
}
17+
18+
isFile() {
19+
if [[ -f $1 ]]; then
20+
return ${YES}
21+
else
22+
return ${NO}
23+
fi
24+
}
25+
26+
isDirectory() {
27+
if [[ -d $1 ]]; then
28+
return ${YES}
29+
else
30+
return ${NO}
31+
fi
32+
}
33+
34+
isFileReadable() {
35+
if [[ -r $1 ]]; then
36+
return ${YES}
37+
else
38+
return ${NO}
39+
fi
40+
}
41+
42+
isFileWritable() {
43+
if [[ -w $1 ]]; then
44+
return ${YES}
45+
else
46+
return ${NO}
47+
fi
48+
}
49+
50+
isFileExecutable() {
51+
if [[ -x $1 ]]; then
52+
return ${YES}
53+
else
54+
return ${NO}
55+
fi
56+
}
57+
58+
# 检查文件夹是否存在,不存在则创建
59+
createFolderIfNotExist() {
60+
if [ ! -d "$1" ]; then
61+
mkdir -p "$1"
62+
fi
63+
}
64+
65+
# 重建目录,如果目录已存在,则删除后重建;如果不存在,直接新建
66+
recreateDir() {
67+
if [[ ! $1 ]]; then
68+
printf "${C_B_RED}<<<< Please input dir path.${C_RESET}\n"
69+
return ${FAILED}
70+
fi
71+
72+
rm -rf $1
73+
mkdir -p $1
74+
75+
isDirectory $1
76+
if [[ "$?" != "${SUCCEED}" ]]; then
77+
printf "${C_B_RED}<<<< create $1 failed.${C_RESET}\n"
78+
return ${FAILED}
79+
fi
80+
81+
return ${SUCCEED}
82+
}

codes/linux/lib/git.sh

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env bash
2+
3+
# 装载其它库
4+
ROOT=`dirname ${BASH_SOURCE[0]}`
5+
source ${ROOT}/env.sh
6+
7+
# ------------------------------------------------------------------------------ git 操作函数
8+
9+
# 检查指定的路径是不是一个 git 项目
10+
checkGit() {
11+
local source=$1
12+
if [[ -d "${source}" ]]; then
13+
cd ${source} || return ${NO}
14+
# (1)删除git状态零时文件
15+
if [[ -f "gitstatus.tmp" ]]; then
16+
rm -rf gitstatus.tmp
17+
fi
18+
19+
# (2)判断是否存在 .git 目录
20+
if [[ -d "./.git" ]]; then
21+
# (3)判断git是否可用
22+
git status &> gitstatus.tmp
23+
local gitStatus=false
24+
grep -iwq 'not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true
25+
rm -rf gitstatus.tmp
26+
if [[ ${gitStatus} == true ]]; then
27+
return ${YES}
28+
else
29+
return ${NO}
30+
fi
31+
fi
32+
33+
return ${NO}
34+
fi
35+
36+
printf "${C_B_B_YELLOW}${source} is invalid dir.${C_RESET}\n"
37+
return ${NO}
38+
}
39+
40+
# clone 或 fetch 操作
41+
# 如果本地代码目录已经是 git 仓库,执行 pull;若不是,则执行 clone
42+
# 依次传入 Git 仓库、Git 项目组、Git 项目名、分支、本地代码目录
43+
cloneOrPullGit() {
44+
45+
local repository=$1
46+
local group=$2
47+
local project=$3
48+
local branch=$4
49+
local root=$5
50+
51+
if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then
52+
printf "${C_B_YELLOW}>>>> Please input root, group, project, branch.${C_RESET}\n"
53+
return ${FAILED}
54+
fi
55+
56+
if [[ ! -d "${root}" ]]; then
57+
printf "${C_B_YELLOW}>>>> ${root} is not directory.${C_RESET}\n"
58+
return ${FAILED}
59+
fi
60+
61+
local source=${root}/${group}/${project}
62+
printf "${C_B_CYAN}>>>> project directory is ${source}.${C_RESET}\n"
63+
printf "${C_B_CYAN}>>>> git url is ${repository}:${group}/${project}.git.${C_RESET}\n"
64+
mkdir -p ${root}/${group}
65+
66+
checkGit ${source}
67+
if [[ "${YES}" == "$?" ]]; then
68+
# 如果 ${source} 是 git 项目,执行 pull 操作
69+
cd ${source} || return ${FAILED}
70+
71+
git checkout -f ${branch}
72+
if [[ "${SUCCEED}" != "$?" ]]; then
73+
printf "${C_B_RED}<<<< git checkout ${branch} failed.${C_RESET}\n"
74+
return ${FAILED}
75+
fi
76+
printf "${C_B_GREEN}>>>> git checkout ${branch} succeed.${C_RESET}\n"
77+
78+
git reset --hard
79+
if [[ "${SUCCEED}" != "$?" ]]; then
80+
printf "${C_B_RED}<<<< git reset --hard failed.${C_RESET}\n"
81+
return ${FAILED}
82+
fi
83+
printf "${C_B_GREEN}>>>> git reset --hard succeed.${C_RESET}\n"
84+
85+
git pull
86+
if [[ "${SUCCEED}" != "$?" ]]; then
87+
printf "${C_B_RED}<<<< git pull failed.${C_RESET}\n"
88+
return ${FAILED}
89+
fi
90+
printf "${C_B_GREEN}>>>> git pull succeed.${C_RESET}\n"
91+
else
92+
# 如果 ${source} 不是 git 项目,执行 clone 操作
93+
94+
git clone "${repository}:${group}/${project}.git" ${source}
95+
if [[ "${SUCCEED}" != "$?" ]]; then
96+
printf "${C_B_RED}<<<< git clone ${project} failed.${C_RESET}\n"
97+
return ${FAILED}
98+
fi
99+
printf "${C_B_GREEN}>>>> git clone ${project} succeed.${C_RESET}\n"
100+
101+
cd ${source} || return ${FAILED}
102+
103+
git checkout -f ${branch}
104+
if [[ "${SUCCEED}" != "$?" ]]; then
105+
printf "${C_B_RED}<<<< git checkout ${branch} failed.${C_RESET}\n"
106+
return ${FAILED}
107+
fi
108+
printf "${C_B_GREEN}>>>> git checkout ${branch} succeed.${C_RESET}\n"
109+
fi
110+
111+
return ${SUCCEED}
112+
}

codes/linux/lib/nodejs.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env bash
2+
3+
# 装载其它库
4+
ROOT=`dirname ${BASH_SOURCE[0]}`
5+
source ${ROOT}/file.sh
6+
7+
# ------------------------------------------------------------------------------ nodejs 操作函数
8+
9+
# install Node Version Manager(nvm)
10+
installNvm() {
11+
local nvmVersion=0.35.2
12+
if [[ $1 ]]; then
13+
local nvmVersion=$1
14+
fi
15+
16+
recreateDir "~/.nvm"
17+
curl -o- https://raw.githubusercontent.com/creationix/nvm/v${nvmVersion}/install.sh | bash
18+
source ~/.nvm/nvm.sh
19+
if [[ "$?" != "${YES}" ]]; then
20+
return ${FAILED}
21+
fi
22+
23+
# Check
24+
nvm version
25+
if [[ "$?" != "${YES}" ]]; then
26+
return ${FAILED}
27+
fi
28+
return ${SUCCEED}
29+
}
30+
31+
# Check nodejs version
32+
checkNodejsVersion() {
33+
if [[ ! $1 ]]; then
34+
printf "${C_B_RED}<<<< please specified expect nodejs version.${C_RESET}\n"
35+
return ${FAILED}
36+
fi
37+
38+
local expectVersion=$1
39+
40+
source /root/.bashrc
41+
local nodeVersion=$(nvm version)
42+
if [[ "$?" != "${YES}" ]]; then
43+
printf "${C_B_YELLOW}>>>> nvm not installed.${C_RESET}\n"
44+
45+
local nvmVersion=v0.35.2
46+
installNvm "${nvmVersion}"
47+
if [[ "$?" != "${SUCCEED}" ]]; then
48+
return ${FAILED}
49+
fi
50+
nodeVersion=$(nvm version)
51+
fi
52+
53+
if [[ "${nodeVersion}" != "v${expectVersion}" ]]; then
54+
printf "${C_B_YELLOW}>>>> current nodejs version is ${nodeVersion}, not ${expectVersion}.${C_RESET}\n"
55+
nvm install ${expectVersion}
56+
nvm use ${expectVersion}
57+
fi
58+
59+
return ${SUCCEED}
60+
}
61+
62+
# build nodejs project
63+
buildNodejsProject() {
64+
if [[ ! $1 ]]; then
65+
printf "${C_B_RED}<<<< please input nodejs project path.${C_RESET}\n"
66+
return ${FAILED}
67+
fi
68+
69+
if [[ ! $2 ]]; then
70+
printf "${C_B_RED}<<<< please input nodejs version.${C_RESET}\n"
71+
return ${FAILED}
72+
fi
73+
74+
isDirectory $1
75+
if [[ "$?" != "${YES}" ]]; then
76+
printf "${C_B_RED}<<<< $1 is not valid path.${C_RESET}\n"
77+
return ${FAILED}
78+
fi
79+
80+
local project=$1
81+
local nodeVersion=$2
82+
printf "${C_B_BLUE}>>>> build nodejs project $1 begin.${C_RESET}\n"
83+
cd ${project} || (printf "${C_B_RED}<<<< ${project} is not exists.${C_RESET}\n" && exit 1)
84+
85+
checkNodejsVersion ${nodeVersion}
86+
87+
npm install
88+
if [[ "$?" != "${YES}" ]]; then
89+
printf "${C_B_RED}<<<< update dependencies failed.${C_RESET}\n"
90+
return ${FAILED}
91+
else
92+
printf "${C_B_GREEN}>>>> update dependencies succeed.${C_RESET}\n"
93+
fi
94+
95+
npm run build
96+
if [[ "$?" != "${YES}" ]]; then
97+
printf "${C_B_RED}<<<< build failed.${C_RESET}\n"
98+
return ${FAILED}
99+
else
100+
printf "${C_B_GREEN}<<<< build succeed.${C_RESET}\n"
101+
fi
102+
return ${SUCCEED}
103+
}
104+
105+
# package nodejs artifact dir (default is dist)
106+
packageDist() {
107+
zip -o -r -q ${branch}.zip *
108+
}

0 commit comments

Comments
 (0)