Shell脚本-自动化部署WEB

时间:2022-08-27 21:33:45

导言:最近在阿里云服务器上搭建了个个人的网站,对于这个网站采用了Django框架开发,搭建服务器请查看:http://blog.csdn.net/temanm/article/details/49255769,对于WEB的更新采用了Shell脚本,每次更新只需要更新SVN代码,运行下Shell脚本,代码就上线了,大赞Shell

1.直接主题看Shell脚本

#!/bin/sh

rm -rf MyProject/

svn co svn://120.24.239.214/repo0/MyProject

cp /root/file/django_wsgi.py /root/MyProject/Yangtai/
cp /root/file/uwsgi_socket.xml /root/MyProject/Yangtai/

pid=`lsof -i:9090 | awk '{print $2}'`
echo $pid
for i in $pid
do
kill -9 $i
done

cd MyProject/Yangtai/

uwsgi -x uwsgi_socket.xml

nginx -s reload

2.Shell的一些基础知识
建议不会Shell可以去看看这份文档:http://c.biancheng.net/cpp/shell/
定义变量: variableName=”value”
使用变量: your_name=”mozhiyan” echo $your_name

删除变量: unset variable_name
变量类型:局部变量、环境变量、shell变量
Shell特殊变量:Shell 0, #, , @, ?, $和命令行参数
Shell替换:Shell变量替换,命令替换,转义字符
\\ 反斜杠
\a 警报,响铃
\b 退格(删除键)
\f 换页(FF),将当前位置移到下页开头
\n 换行
\r 回车
\t 水平制表符(tab键)
\v 垂直制表符
Shell运算符:

    #!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi

Shell注释:和python一样 #号
Shell字符串:

    your_name='qinjx'
str="Hello, I know your are \"$your_name\"! \n"

Shell数组:

    #!/bin/sh
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"

Shell echo命令:

    name="OK"
echo "$name It is a test"

shell printf命令:格式化输出语句

$ printf "%d %s\n" 1 "abc"

Shell if else语句:

    #!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi

Shell case esac语句:

    echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
;;
3) echo 'You select 3'
;;
4) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac

Shell for循环:

    for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done

Shell while循环:

    COUNTER=0
while [ $COUNTER -lt 5 ]
do
COUNTER='expr $COUNTER+1'
echo $COUNTER
done

Shell until循环:

    #!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

Shell break和continue命令:

    #!/bin/bash
while :
do
echo -n "Input a number between 1 to 5: "
read aNum
case $aNum in
1|2|3|4|5) echo "Your number is $aNum!"
;;
*) echo "You do not select a number between 1 to 5, game is over!"
break
;;
esac
done

Shell函数:

    #!/bin/bash
# Define your function here
Hello () {

echo "Url is http://see.xidian.edu.cn/cpp/shell/"
}
# Invoke your function
Hello

Shell函数参数:

    #!/bin/bash
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # 参数个数
echo "The string of the parameters is $* !" # 传递给函数的所有参数
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

PS:本人服务器网站正在备案审核中,阿里云备案还是轻松很多,备案完成后,敬请期待:www.xuyangting.com,现在可以域名访问:120.24.239.214

阳台测试: 239547991(群号)

本人博客:http://xuyangting.sinaapp.com/