---
title: "一个自动化打包上传的简单shell工具"
author: "Willam2004"
date: "2011-11-02T20:17:46Z"
tags: []
categories: ["操作系统"]
slug: "shell"
draft: false
lastmod: "2011-11-02T20:19:52Z"
id: 1231846
url: "https://willam2004.iteye.com/blog/1231846"
---

开发完成之后，需要将本地的环境进行打包，并部署到测试环境，一般需要经过这几个步骤， 1.mvn install 2.执行make-rpm.sh脚本 3.执行scp将本地的rpm包拷贝到测试服务器中。每次的敲代码也比较人肉，所以写了一个shell脚本，将这几部串起来。因为考虑到有些时候，我们其中一步失败，而不想重头在来，所以脚本我支持通过1，2，3的数字进行选择，具体看代码吧。

 

 

 

```
#!/bin/bash
#install

command=$1
username=$2
host=$3



function info(){
	echo 'use ./deploy.sh command [username] [host]'
	echo 'command:'
	echo '0  -- exec all action,contains:install makeRpm upload'
	echo '1  -- exec install'
	echo '2  -- exec makeRpm'
	echo '3  -- exec upload,the username and host only affect this action.'
}

function all(){
	install 
	makeRpm
	upload
}

function install(){
	echo 'mvn install'
	mvn clean install -Denv=release -Dmaven.test.skip=true >>/dev/null
}

function makeRpm(){
	echo 'make-rpm,please make u rpm version is 4.4.x'
	./make-rpm.sh>>/dev/null
}

function upload(){
	echo 'upload the rpm to server'
	if [ -z $username ];then
		username='xxx'
	fi
	if [ -z $host ];then
		host='xx.xx.xx.xx'
	fi
	scp ./target/rpm/RPMS/noarch/*.rpm $username@$host:/home/$username/ >>/dev/null
}

#start execute
info
echo 'deploy start'
if [ -z $command -o $command = 0 ];then  #这里面-o是或的关系，-a是and关系
		all
	elif [ $command = 1 ];then
		install
	elif [ $command = 2 ];then
		makeRpm	
	elif [ $command = 3 ];then
		upload	
fi

echo 'deploy success!'
```

这个脚本的不好之处，就是在上传的时候因为采用了scp的方式，需要手动输入密码，后续考虑使用expert的方式，让密码自动输入，这样就可以让脚本自动执行，让人休息了!