您的位置:首页 > 运维架构 > Shell

【shell实例】一键自动化pull->更新文件->commit->压缩打包->上传服务器

2017-08-24 20:58 337 查看
源码:upload_to_swver.sh

技术点汇总如下:

#从当前目录返回上级目录
$(dirname ${PWD})

#从当前目录返回上级的上级目录
$(dirname $(dirname ${PWD}))

#搜索当前目录下的指定文件或目录
package_dir=`ls | grep "$tmp_version_info"`

#取出文件的第一行内容赋值变量
current_verision_str=`sed -n 1p $board_version_info_file`

#剪切字符串以:分隔并输出第二个子字符串
current_version_info=`echo $current_verision_str | cut -d ':' -f 2`

#判断字符串是否在文件中
if grep -q "$current_verision_str" $release_history_txt ; then
echo "yes"
else
echo "no"
fi

#从文件中搜索出所包含的字符串,并进行剪切以@分隔的第二个
custom_name=`grep $customID $custom_info_file | cut -d '@' -f 2`

#剪切字符串,以_分隔并且取分隔出来的字段中第2个开始取第3到第9个连在一起的字符串
tmp_version_info=`echo $current_version_info | cut -d '_' -f 2,3-9`

#将分割线和源文件的内容插入到目标文件第二行开始的位置,line_cnt保证插入顺序与源文件一致,ni\代表从第n行的前面插入内容,双引号内的变量取值需要转义\$variable,sed -i直接修改文件内容
delimit_line="====================================="
line_cnt=2
echo $delimit_line | sed -i "${line_cnt}i\\$delimit_line" $release_history_txt
cat $board_version_info_file | while read line
do
line_cnt=$[ $line_cnt + 1 ]
echo $line | sed -i "${line_cnt}i\\$line" $release_history_txt
echo $line
done

#自动输入内容到需要选择输入"[Y|N]"的选项中,使其自动化运行(gerrit_push_py是python脚本文件)
echo "y" | $gerrit_push_py
echo "Y"

#判断wc -w命令统计到的字符串是否为1个word单词
if [ $(echo $custom_name | wc -w) -ne 1 ]; then
echo "some words..."
fi

#使用ssh协议进行服务器地址命令操作(注意空格与引号),sshpass -p指定登录地址的密码,test -d判断文件/目录是否存在,scp -r使用scp协议传输目录及目录中所有文件
if ! sshpass -p swver ssh swver@10.129.61.36 "test -d /home/swver/sw_versions/${target_path}" ; then
sshpass -p swver ssh swver@10.129.61.36 "mkdir -p /home/swver/sw_versions/${target_path}"
echo ">>>Create /home/swver/sw_versions/${target_path} , Start uploading:"
else
echo ">>>Directory is existed, Start uploading:"
fi
sshpass -p swver scp -r $package_file swver@10.129.61.36:"/home/swver/sw_versions/${target_path}/"

#判断字符串的包含关系,如下(左 =~ 右)表示判断左字符串是否包含右字符串
if [[ $main_board_path_name =~ "/" ]]; then
echo "man_board_path_name include '/'..."
else
echo "Don't include..."
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 自动化 实例
相关文章推荐