1. 程式人生 > >一鍵部署最新的Git(Centos7.3)

一鍵部署最新的Git(Centos7.3)

一、手動部署步驟:

步驟1、安裝編譯git時需要的包

# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
# yum install  gcc perl-ExtUtils-MakeMaker

步驟2、刪除已有的git

# yum remove git

步驟3、下載git原始碼,我自己下載的是2.0.0版本的下載地址:http://pan.baidu.com/s/1qXFnOxI

# cd /usr/src
# wget https://www.kernel.org/pub/software/scm/git/git-2.0.5.tar.gz
# tar xzf git-2.0.5.tar.gz

步驟4、編譯安裝

# cd git-2.0.5
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install
# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
# source /etc/bashrc

步驟5、檢查一下版本號

# git --version

二、一鍵部署指令碼

#!/bin/sh
# @author ling

# 定義顯示顏色
RED='\e[1;91m'
GREEN='\e[1;92m'
WITE='\e[1;97m'
NC='\e[0m'

OPERATION_PATH=/opt
REMOTE_URL=https://mirrors.edge.kernel.org/pub/software/scm/git/
GIT_UNPACKAGE_NAME=git-2.19.1.tar.gz

# 校驗url是否有效
function check_invalid_url()
{
	echo "Check invalid url!"
	if [ ! -n "$1" ];then
		echo -e $RED "Please input url!" $NC
	else
		wget --spider -q  --tries=1 -T 5 $1
		return $?
	fi
}

# 安裝Git的依賴包
function install_dependent_packages()
{
	echo "Install dependent packages!"
	yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker && echo -e $GREEN "Install dependent packages success!" $NC
}

# 解除安裝舊版本的Git
function remove_old_git()
{
	echo "Remove old git!"
	git version
	if [ $? -eq 0 ];then
		yum -y remove git && echo -e $GREEN "Remove old git success!" $NC
	else
		echo -e $WITE "There has no git!" $NC
	fi

}

# 下載Git安裝包
function download_git_package()
{	
	echo "Download git package!"
	NUM_1=$(curl $REMOTE_URL | grep -o -E "git-[0-9]+" | grep -o -E "[0-9]+" | sort -un | sed -n '$p')
	NUM_2=$(curl $REMOTE_URL | grep -o -E "git-$NUM_1\.+[0-9]+\." | grep -o -E "\.[0-9]+\." | grep -o -E "[0-9]+" | sort -un | sed -n '$p')
	NUM_3=$(curl $REMOTE_URL | grep -o -E "git-$NUM_1\.$NUM_2\.[0-9]+\.tar\.gz" | grep -o -E "\.[0-9]\.tar\.gz" | grep -o -E "[0-9]+" | sort -un | sed -n '$p')
	GIT_UNPACKAGE_NAME=git-$NUM_1\.$NUM_2\.$NUM_3\.tar.gz
	wget -P $OPERATION_PATH $REMOTE_URL$GIT_UNPACKAGE_NAME && echo -e $GREEN "Download git package success!" $NC
}

# 編譯安裝Git
function install_git_package()
{
	echo "Install git package!"
	cd $OPERATION_PATH
	GIT_PACKAGE_NAME=$(echo $GIT_UNPACKAGE_NAME | grep -o -E "git-[0-9]+\.[0-9]+\.[0-9]+")
	tar -xzvf $GIT_UNPACKAGE_NAME && rm -rf $GIT_UNPACKAGE_NAME
	cd $GIT_PACKAGE_NAME && make prefix=/usr/local/git all && make prefix=/usr/local/git install && 
	echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc && source /etc/bashrc && echo -e $GREEN "Install git package success!" $NC
}

function main()
{
	echo "Start install git, it may take a moment!"
	# 安裝Git的依賴包
	install_dependent_packages
	# 解除安裝舊版本的Git
	remove_old_git
	check_invalid_url $REMOTE_URL
	if [ $? -eq 0 ];then
		download_git_package
	else
		cp $GIT_UNPACKAGE_NAME $OPERATION_PATH
	fi
	install_git_package
}

main

三、一鍵部署指令碼使用說明

1.下載一鍵部署指令碼
百度雲盤連結:https://pan.baidu.com/s/1RZNBn0UprOOmy9PKDb0bdw
提取碼:n4e0
2.解壓一鍵部署包,進入包下,使用命令:source start.sh執行部署指令碼;
3.使用命令:git --version校驗git是否安裝成功

四、FAQ

Q:使用if命令,判斷上一條命令執行退出的狀態是否為0出現如下圖錯誤提示:

在這裡插入圖片描述

R:[ ]表示條件測試。注意這裡的空格很重要。要注意在'['後面和']'前面都必須要有空格
S:在'['後面和']'前面新增空客
Q:shell函式return的值使用echo打印出來發現為空
S:shell函式return的值是賦值給$?
R:在呼叫shell函式後,直接使用$?獲取函式的返回值.
Q:shell 在指令碼中執行了source /etc/profile 不生效
S:sh filename 重新建立一個子shell,在子shell中執行腳本里面的語句,該子shell繼承父shell的環境變數,但子shell新建的、改變的變數不會被帶回父shell,除非使用export。 
R:執行source filename 或者 .filename即可解決問題。