一些shell脚本

最近在搞部署项目的功能,写了不少shell脚本,总结一下

1. sftp上传支持多条命令,用awk分隔文件路径获取文件名称($NF表示最后一列)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash                                                                                                                           
function check() {
if [ -z $1 ]; then
echo "please give the filename"
return
fi
if [ ! -f $1 ]; then
echo "file not exists, exit..."
return
fi
echo "ls" > command.txt
echo "cd bj6-c-grb-screen-admin01/史国富" >> command.txt
echo "put $1" >> command.txt
filename=`echo $1 | awk -F/ '{print $NF}'`
echo $filename
echo "ls $filename" >> command.txt
sftp -P 2222 shiguofu@120.92.118.51 < command.txt
echo "Done"
}


check $1

2. 部署项目,未增加测试环境标识(sed修改),手动修改文件

环境变量传入: 由于sudo 会重新初始化环境变量,因此,可以在脚本中传入执行的变量PATH等

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
WALLE_NODE=10.13.88.152
WALLE_USER=root
WALLE_PASSWD=1qaz@WSX
zip_file_name=wps_eb_admin_`date +%Y%m%d%H%M%S`.tar
remote_dest_dir=/tmp
TEST_NODE=10.226.50.22

function copy_to_walle()
{
echo "copy to $WALLE_NODE"
cd ..
echo "tar service files..."
tar -cf /tmp/$zip_file_name ./*
echo "Done. tar file -> /tmp/$zip_file_name"
sshpass -p "$WALLE_PASSWD" scp /tmp/$zip_file_name $WALLE_USER@$WALLE_NODE:$remote_dest_dir
echo "copy to remote Done."
sshpass -p "$WALLE_PASSWD" ssh $WALLE_USER@$WALLE_NODE "
rm -rf /tmp/wps_eb_tmp
mkdir -p /tmp/wps_eb_tmp
tar -xf /tmp/$zip_file_name -C /tmp/wps_eb_tmp
"
}

function deploy()
{
if [ -z $1 ]; then
echo "please give the ip address as the first param"
return
fi
USER=root
echo "deploy to test: $1"
echo "tar service files..."
tar -cf /tmp/$zip_file_name ./*
echo "Done. tar file -> /tmp/$zip_file_name"
if [ ! -z $2 ]; then
USER=$2
fi
if [ ! -z $3 ]; then
sshpass -p "$3" scp /tmp/$zip_file_name $USER@$1:$remote_dest_dir
sshpass -p "$3" ssh $USER@$1 << eeooff
sudo -i
tar -xf $remote_dest_dir/$zip_file_name -C /tmp/ ./release/*;
cd /tmp/release
PATH=$PATH:/usr/local/bin && ./project_init.sh $remote_dest_dir/$zip_file_name
eeooff
else
scp /tmp/$zip_file_name $USER@$1:$remote_dest_dir
echo "Done copy to remote host..."
ssh $USER@$1 << eeooff
sudo -i
tar -xf $remote_dest_dir/$zip_file_name -C /tmp/ ./release/*;
cd /tmp/release
. /etc/profile && ./project_init.sh $remote_dest_dir/$zip_file_name
eeooff
fi
rm /tmp/$zip_file_name
# ./project_init.sh $remote_dest_dir/$zip_file_name
}


function deploy_test()
{
cd ..
sed -i 's/production/development/g' pm2.json
deploy 10.226.50.22 root 123456
sed -i 's/development/production/g' pm2.json
}

echo $1
if [ -z $1 ]; then
deploy_test
elif [ "$1" == "walle" ]; then
copy_to_walle
else
cd ..
deploy $1 $2 $3
fi

3. 初始化服务环境,安装必要的包

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
#!/bin/bash                                                                                                                                                                    
SOFT_DIR=/data/soft

function install_webp(){
cd $SOFT_DIR
wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.0.3-linux-x86-64.tar.gz
tar -xf libwebp-1.0.3-linux-x86-64.tar.gz
cd libwebp-1.0.3-linux-x86-64/bin
if [ -f /usr/bin/cwebp ]; then
mv /usr/bin/cwebp /usr/bin/cweb.bak
fi
cp cwebp /usr/bin/
}

function install_node(){
cd $SOFT_DIR
wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-x64.tar.xz
tar xvf node-v10.13.0-linux-x64.tar.xz
echo "export PATH=$PATH:$SOFT_DIR/node-v10.13.0-linux-x64/bin" >> /etc/profile
echo "export NODE_PATH=/data/soft/node-v10.13.0-linux-x64/lib/node_modules" >> /etc/profile
source /etc/profile
npm install pm2 -g
echo "10.13.0.29 wpsgit.xxx.net" >> /etc/hosts
echo "10.13.0.98 cdnshow.xxx.kingsoft.net" >> /etc/hosts
echo "120.92.115.93 suc.xxx.kingsoft.net" >> /etc/hosts
}
# mongodb 下载地址:https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/

if [ ! -d $SOFT_DIR ];then
rm -rf $SOFT_DIR
mkdir -p $SOFT_DIR
fi
which cwebp # 采用which判断是否存在,获取返回值
code=`echo $?`
echo $code
if [ $code != 0 ]; then
install_webp
fi
which npm
code=`echo $?`
if [ $code != 0 ]; then
install_node
fi

4. 服务初始化脚本

服务需要sudo 执行, 增加了source 初始化环境的头

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
#!/bin/bash
source /etc/profile

function init()
{
if [ ! -z $1 ] && [ -d $1 ]; then
final_dir=$1;
else
echo "$1 did not exists... exit"
return
fi
echo "install package..."
cd $1
if [ ! -z $2 ]; then
sed -i "s/production/$2/g" pm2.json
fi
source /etc/profile
npm config set @wps:registry http://120.92.93.69:4873
npm install --no-save --production
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install canvas
echo "install Done."
echo "stop service sxxx-eb.."
sh -c 'source /etc/profile && pm2 delete wps_eb-admin' &>/tmp/pm2.log
echo "stopped"
sleep 1
echo "start service xxx-eb"
sh -c 'source /etc/profile && pm2 start pm2.json' &>/tmp/pm2.log
echo "start Done."
}


function deploy_by_tar()
{
release_dir=/data/www
dest_dir=/data/release/xxx_ebook_admin/`date +%Y%m%d-%H%M%S`
final_xxx_ebook_dir=$release_dir/xxx_ebook_admin
if [ ! -z $1 ] && [ -f $1 ]; then
echo "deploy with $1..."
zip_file_name=$1
else
echo "$1 did not exists... exit"
return
fi
mkdir -p $release_dir
mkdir -p $dest_dir
echo "untar package to $dest_dir"
`tar -xf $zip_file_name -C $dest_dir`
echo "untar Done."
echo "ln -sfn $dest_dir $final_xxx_ebook_dir"
ln -sfn $dest_dir $final_xxx_ebook_dir
echo 'Done.'
init $final_wps_ebook_dir $2
rm $zip_file_name
}

./init_env.sh
deploy_by_tar $1 $2

shell知识点.md

1. shell函数参数传递不需要在参数列表,是通过$1, $2….这样获取,如

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

function deploy_one_host() # 函数定义,小括号不是必须的
{
cd wps_pic
zip_file_name=wps_pic_`date +%Y%m%d%H%M%S`.zip
zip "$zip_file_name" ./* -q
scp $zip_file_name root@$1:/tmp # 获取第一个参数
ssh root@$1 "sh exec.sh $zip_file_name;" # ssh 在远端执行命令,多个以分号分隔
}

deploy_one_host 10.229.26.143 # 传递一个参数,多个可在后面添加,以空格分隔

2. shell 命令行参数

1
2
3
#!/bin/bash

echo $1, $2 # 获取命令行参数,第一个第二个, 如果没有就为空

执行:

./exec.sh a b

3. if 判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function check()
{
if [ ! -d "$release_name" ]; then
echo "$release_name"" not exists, check the online dir"
return
fi
if [ -z "$1" ]; then
echo "must give the dir"
return
elif [ ! -d "$1" ]; then
echo "$1" "not exists please give the dir will online"
return
else
echo "check ok"
do_online $1
fi
}

-d 判读是不是一个目录,如果是为真

-f 判断是不是一个普通文件,是为真

-z 判断字符的长度是不是为0,为0则为真

==/!= 字符串相等/不等