shell example


#!/bin/sh
rm -f 111
svn log|grep lines|cut -d'|' -f1|while read rev
do
rev=${rev:1:999}
svn log -r $rev -v >> 111
done
-------------------------------------------------------

static void cli_helper(struct cli_def *cli);

【shell example】exec
1,$s/; /\r{\r}\r/g

static void cli_helper(struct cli_def *cli)
{
}

#!/bin/sh
prog_root=/opt/app
conf_root=/opt/app/config

usage()
{
echo "usage: $0"
exit 1
}

# kill pid in pid file and wait for it to exit
# max wait time is 3 seconds
# $1 is pid file
wait_pid()
{
local pidf=$1
if [ -f $pidf ]; then
local pid=`cat $pidf`
local waits=0
while [ -d /proc/$pid ] && [ $waits -lt 3 ]; do
sleep 1
kill -9 $pid 2>/dev/null
if [ ! -d /proc/$pid ]; then
break
fi
let waits=$waits+1
done
fi
}

if [ $# -ne 2 ]; then
usage
fi
prog="$1"
req="$2"

conf=`echo $prog|awk '{print(tolower($0)); }'`
req=`echo $req|awk '{print(tolower($0)); }'`
prog1=`echo $conf|awk '{print(substr($0, 1, 1)); }'`
prog2=`echo $conf|awk '{print(substr($0, 2)); }'`
prog1=`echo $prog1|awk '{print(toupper($0)); }'`

if [ "$conf" != "AAA" ] && [ "$conf" != "BBB" ]; then
usage
fi

prog=$prog_root/$prog1$prog2
log=$prog_root/$conf.log
pid_file=$prog_root/.$conf.pid
conf=$conf_root/$conf.conf

if [ "$req" != "start" ] && [ "$req" != "stop" ] && [ "$req" != "restart" ] && [ "$req" != "status" ]; then
usage
fi

start()
{
if [ ! -f $conf ]; then
echo "$conf not found"
exit 1
fi

start-stop-daemon -S -q -b -x "$prog" -p "$pid_file" -m -- -c "$conf" -l "$log"
exit 0
}

stop()
{
if test -f $pid_file; then
start-stop-daemon -K -q -o -p "$pid_file"
fi

wait_pid $pid_file

rm -f $pid_file
}

restart()
{
stop
start
}

status()
{
status=`ps -ef|grep $prog|grep -v grep|cut -d' ' -f0,1,2,3`
echo $status
}

case "$req" in
start)
start
; ;
stop)
stop
; ;
restart)
restart
; ;
status)
status
; ;
esac

ret=$?

exit $ret


条件表达式("CONDITIONAL EXPRESSIONS")
条件表达式用于 [[ 复合命令以及内建命令 test 和 [ 中,用来测试文件属性,进行字符串和算术比较。表达式使用下面的单目或二进制操作构造。如果某操作的任何 file 参数的形式是
/dev/fd/n,那么将检查文件描述符 n。如果某操作的 file 参数是 /dev/stdin, /dev/stdout 或者 /dev/stderr 之一,将分别检查文件描述符 0,1 和 2。

-a file
如果 file 存在则为真。
-b file
如果 file 存在且为块设备则为真。
-c file
如果 file 存在且为字符设备则为真。
-d file
如果 file 存在且是一个目录则为真。
-e file
如果 file 存在则为真。
-f file
如果 file 存在且为普通文件则为真。
-g file
如果 file 存在且是设置组ID的 (sgid) 则为真。
-h file
如果 file 存在且为符号链接则为真。
-k file
如果 file 存在且设置了 ‘‘sticky’’ 位 (粘滞位) 则为真。
-p file
如果 file 存在且是一个命名管道 (FIFO) 则为真。
-r file
如果 file 存在且可读则为真。
-s file
如果 file 存在且大小大于零则为真。
-t fd如果文件描述符 fd 是打开的且对应一个终端则为真。
-u file
如果 file 存在且是设置用户ID的 (suid) 则为真。
-w file
如果 file 存在且可写则为真。
-x file
如果 file 存在且可执行则为真。
-O file
如果 file 存在且为有效用户ID所拥有则为真。
-G file
如果 file 存在且为有效组ID所拥有则为真。
-L file
如果 file 存在且为符号链接则为真。
-S file
如果 file 存在且为套接字则为真。
-N file
如果 file 存在且上次读取后被修改过则为真。
file1 -nt file2
如果 file1 比 file2 要新 (根据修改日期),或者如果 file1 存在而 file2 不存在,则为真。
file1 -ot file2
如果 file1 比 file2 更旧,或者如果 file1 不存在而 file2 存在,则为真。
file1 -ef file2
如果 file1 和 file2 指的是相同的设备和 inode 号则为真。
-o optname
如果启用了 shell 选项 optname 则为真。参见下面对内建命令 set 的 -o 选项的描述中的选项列表。
-z string
如果 string 的长度为 0 则为真。
-n string
string 如果 string 的长度非 0 则为真。
string1 == string2
如果字符串相等则为真。= 可以用于使用 == 的场合来兼容 POSIX 规范。
string1 != string2
如果字符串不相等则为真。
string1 < string2
如果 string1 在当前语言环境的字典顺序中排在 string2 之前则为真。
string1 > string2
如果 string1 在当前语言环境的字典顺序中排在 string2 之后则为真。
arg1 OP arg2
OP 是 -eq, -ne, -lt, -le, -gt, 或 -ge 之一。这些算术二进制操作返回真,如果 arg1 与 arg2 分别是相等,不等,小于,小于或等于,大于,大于或等于关系 。Arg1和
arg2 可以是正/负整数。


    推荐阅读