#!/usr/local/env sh
#author : kj
#time : 2013-12-12 21:00
#function : done the function of sys update
#0 - offline ; 1 - online ; 2 - update ;3 - update_succeed ; 4 - update_failed
joseph_avserver=`md5sum /opt/joseph/av_server/av_server | awk '{print$1}'`
joseph_avserver_tmp=`md5sum /opt/joseph/av_server_tmp/av_server | awk '{print$1}'`
joseph_ipnc_sys_staus=`sed -n 's/joseph_ipnc_run_status=\(.*\)$/\1/p' /opt/joseph/av_server/config/ipnc.conf`
echo "$joseph_avserver"
echo "$joseph_avserver_tmp"
echo "$joseph_ipnc_sys_staus"
#joseph_sys_defend > /dev/null &
if [ "$joseph_avserver" == "$joseph_avserver_tmp" ]
then
echo "sys update succeed !"
#rm -rf /opt/joseph/av_server_tmp/av_server
cp /opt/joseph/av_server/av_server /tmp/bin/
sync
cd /tmp/bin/
./av_server &
sleep 1s
cd /opt/joseph/av_server/
./ipnc_send &
else
if [ "$joseph_avserver_tmp" == "" ]
then
echo "joseph_avserver_tmp is NULL !"
#rm -rf /opt/joseph/av_server_tmp/av_server
cp /opt/joseph/av_server/av_server /tmp/bin/
sync
cd /tmp/bin/
./av_server &
sleep 1s
cd /opt/joseph/av_server/
./ipnc_send &
else
if [ "$joseph_avserver_tmp" != "" ] && [ "$joseph_ipnc_sys_staus" == "2" ] || [ "$joseph_ipnc_sys_staus" == "4" ] || [ "$joseph_ipnc_sys_staus" == "1" ]
then
echo "joseph_ipnc_sys_staus is 2 or 4 or 1 !"
cp /opt/joseph/av_server/av_server /tmp/bin/
sync
cd /tmp/bin/
./av_server &
sleep 1s
cd /opt/joseph/av_server/
./ipnc_send &
else
if [ "$joseph_ipnc_sys_staus" == "3" ]
then
echo "joseph_ipnc_sys_staus is 3 !"
cp /opt/joseph/av_server_tmp/av_server /opt/joseph/av_server/
sync
cp /opt/joseph/av_server/av_server /tmp/bin/
sync
cd /tmp/bin/
./av_server &
sleep 1s
cd /opt/joseph/av_server/
./ipnc_send &
fi
fi
fi
fi
和C语言类似,在Shell中用if
、then
、elif
、else
、fi
这几条命令实现分支控制。这种流程控制语句本质上也是由若干条Shell命令组成的,例如先前讲过的
if [ -f ~/.bashrc ]; then . ~/.bashrc fi
其实是三条命令,if [ -f ~/.bashrc ]
是第一条,then . ~/.bashrc
是第二条,fi
是第三条。如果两条命令写在同一行则需要用;号隔开,一行只写一条命令就不需要写;号了,另外,then
后面有换行,但这条命令没写完,Shell会自动续行,把下一行接在then
后面当作一条命令处理。和[
命令一样,要注意命令和各参数之间必须用空格隔开。if
命令的参数组成一条子命令,如果该子命令的Exit Status为0(表示真),则执行then
后面的子命令,如果Exit Status非0(表示假),则执行elif
、else
或者fi
后面的子命令。if
后面的子命令通常是测试命令,但也可以是其它命令。Shell脚本没有{}括号,所以用fi
表示if
语句块的结束。见下例:
#! /bin/sh if [ -f /bin/bash ] then echo "/bin/bash is a file" else echo "/bin/bash is NOT a file" fi if :; then echo "always true"; fi
:
是一个特殊的命令,称为空命令,该命令不做任何事,但Exit Status总是真。此外,也可以执行/bin/true
或/bin/false
得到真或假的Exit Status。再看一个例子:
#! /bin/sh echo "Is it morning? Please answer yes or no." read YES_OR_NO if [ "$YES_OR_NO" = "yes" ]; then echo "Good morning!" elif [ "$YES_OR_NO" = "no" ]; then echo "Good afternoon!" else echo "Sorry, $YES_OR_NO not recognized. Enter yes or no." exit 1 fi exit 0
上例中的read
命令的作用是等待用户输入一行字符串,将该字符串存到一个Shell变量中。
此外,Shell还提供了&&和||语法,和C语言类似,具有Short-circuit特性,很多Shell脚本喜欢写成这样:
test "$(whoami)" != 'root' && (echo you are using a non-privileged account; exit 1)
&&相当于“if...then...”,而||相当于“if not...then...”。&&和||用于连接两个命令,而上面讲的-a
和-o
仅用于在测试表达式中连接两个测试条件,要注意它们的区别,例如,
test "$VAR" -gt 1 -a "$VAR" -lt 3
和以下写法是等价的
test "$VAR" -gt 1 && test "$VAR" -lt 3