8月 03

bash 产生随机数

  使用bash产生n位的随机数。测试中为产生8位。

#!/bin/bash
# -------------------------------
# Filename:    test.sh
# Revision:    1.0
# Date:        2013-08-01
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Description: 
# -------------------------------

function randnum()
{
    di=(0 1 2 3 4 5 6 7 8 9 \
        a b c d e f g h i j k l m n o p q r s t u v w x y z \
        A B C D E F G H I G K L M N O P Q R S T U V W X Y Z \
        ~ ! ^ _)
    for(( i=0; i<$1; i++))
    do
        num=$num`echo -n ${di[$RANDOM % ${#di[*]}]}`
    done
    echo $num
}

get=$(randnum 8)
echo $get

bash产生n位随机数

8月 02

linux远程通过ssh启停tomcat

  在linux上远程通过ssh启停tomcat,因为存在环境变量加载问题,所以写一个shell脚本,放在tomcat的bin目录下。CentOS测试通过。需要使用bash。因为脚本中包含脚本名字,在查找时要过滤掉自己,所以如果需要修改脚本名字,在脚本内也要相应的修改。

#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    tomcatcomm.sh
# Revision:    1.0
# Date:        2013-7-29
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Description: 
# -------------------------------------------------------------------------------
# Source function library.
. /etc/init.d/functions
source /etc/profile

#### shell env
path=$(dirname $0)
if [ ${path:0:1} == . ]; then
   path=${path/./$(pwd)}
fi
my_name="tomcatcomm.sh"

###### delete tomcat`s work dir and startup server
del_start()
{
    /bin/rm -rf ${1}work/*
}

######main()
case "$1" in
  start)
       get_pid_count=`/bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|wc -l`
       if [ ${get_pid_count} -gt 0 ] ; then
          /bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|awk ' ''{print $2}'|xargs kill -9
          sleep 1
          get_pid_count=`/bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|wc -l`
           if [ ${get_pid_count} -gt 0 ] ; then
              echo "Error"
           else
              del_start ${path}
              ${path}/startup.sh
           fi
        else
            del_start ${path}
            ${path}/startup.sh
        fi
        ;;
  stop)
        ${path}/shutdown.sh
        del_start ${path}
        sleep 1
        ;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

tomcat远程启停脚本