提交到git仓库脚本
#!/bin/bash
:<<BLOCK
# 初始化仓库
cd /root/halo
git init
touch README.md
git add .
git commit -m "first commit"
git remote add origin https://gitee.com/wdbj/halo2.0.git
git push -u origin "master"
# 执行权限
chmod 777 ./gitpub.sh
# 执行脚本提交到git
./gitpub.sh -u 用户名 -p 密码
BLOCK
usage() {
echo "参数: ${0} [-u|--user] [-p|--password]" 1>&2
exit 1
}
while [[ $# -gt 0 ]];do
key=${1}
case ${key} in
-u|--user)
var_u=${2}
shift 2
;;
-p|--password)
var_p=${2}
shift 2
;;
*)
usage
shift
;;
esac
done
if [ "$var_u" = "" ]; then
echo "用户名 不能为空"
exit 1
fi
if [ "$var_p" = "" ]; then
echo "密码 不能为空"
exit 1
fi
# echo "用户名 $var_u 密码$var_p"
# exit 1
#安装expect
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
yum install -y expect >/dev/null
echo "expect安装成功"
fi
time1=$(date "+%Y-%m-%d %H:%M:%S") #时间
time2=$time1
user=$var_u #用户名
password=$var_p #密码
var1=`
/usr/bin/expect <<-EOF
set timeout 1000
spawn git add .
spawn git commit -m "$time2"
spawn git push -u origin master
expect {
"Username" { send "$user\r" }
}
expect {
"Password" { send "$password\r" }
}
expect eof
EOF
`
var0="-------------$time1\r\n"
var2="\r\n-------------$time1\r\n"
var3=$var0$var1$var2
echo -e $var3 >> ./gitpub.txt
echo -e $var3
保存到 `/root/halo/gitpub.sh`