#!/bin/bash
pa=abc.com
ja=/javaweb
tt=/usr/local/tomcat
#gets the parameter
[ $# -ne 1 ] && echo "please enter an instance name (for example: ./tomcatadd test.wltest.com)" && exit 1
itn=`echo $1`
#add new instance information
add () {
if [ -d $ja/$itn ];then
echo "warning:there are already exists $ja/$itn,please check"
anii
elif [ -d $tt/$itn ];then
echo "warning:there are already exists $tt/$itn,please check"
else
tbci
fi
}
anii () {
read -p "enter the instance name you want to create:" itn1
if [ -d $ja/$itn1 ];then
echo "warning:there are already exists $ja/$itn1,please check"
elif [ -d $tt/$itn1 ];then
echo "warning:there are already exists $tt/$itn1,please check"
echo "the instance name is $itn1"
read -p "is ok,please enter y or n:" reada
if [ $reada == y ];then
itn=`echo $itn1`
tbci
else
anii
fi
#testing basic configuration information
tbci () {
/bin/echo "" > /tmp/tomcatadd.txt
f=`find /usr/local/tomcat/ -name "server.xml"`
for i in $f
do
n=`sed -n "/port=/p" $i | awk '{print $2}'| grep -v "protocol" | grep -v "8443"| awk -f "=" '{print $2}'|awk -f '"' '{print $2}'`
echo $n >> /tmp/tomcatadd.txt
done
/bin/netstat -lnpt | grep java |awk '{print $4}'|awk -f: '{print $nf}' >> /tmp/tomcatadd.txt
[ ! -d $ja ] && echo "warning:not found $ja,please check " && exit 1
[ ! -d $tt ] && echo "warning:not found $tt,please check " && exit 1
[ ! -d $tt/conf/ ] && echo "warning:not found $tt/conf/,please check " && exit 1
[ ! -d $tt/webapps/ ] && echo "warning:not found $tt/webapps/,please check " && exit 1
[ ! -d $tt/temp/ ] && echo "warning:not found $tt/temp/,please check " && exit 1
[ ! -d $tt/work/ ] && echo "warning:not found $tt/work/,please check " && exit 1
[ ! -d $tt/logs/ ] && echo "warning:not found $tt/logs/,please check " && exit 1
aniiun
#select the administrator account
aniiun () {
unx=`echo $itn | awk -f "." '{print $1}'`
if [ "$(/bin/grep "^$unx" /etc/passwd)" == "" ];then
un=`echo $unx`
ran1
aniiunf
aniiunf () {
unf=`echo $(( random % 9+1 ))`
unx1=`echo $unx$unf`
if [ "$(/bin/grep "^$unx1" /etc/passwd)" == "" ];then
un=`echo $unx1`
#randomly generated port numbers
ran1 () {
ranu1=`echo $(( random % 918+8081 ))`
if [ "$(/bin/find /tmp/tomcatadd.txt | /usr/bin/xargs /bin/grep $ranu1)" == "" ];then
echo $ranu1 >> /tmp/tomcatadd.txt
num1=`echo $ranu1`
ran2
ran1
ran2 () {
ranu2=`echo $(( random % 918+8081 ))`
if [ "$(/bin/find /tmp/tomcatadd.txt | /usr/bin/xargs /bin/grep $ranu2)" == "" ];then
echo $ranu2 >> /tmp/tomcatadd.txt
num2=`echo $ranu2`
ran3
ran3 () {
ranu3=`echo $(( random % 918+8081 ))`
if [ "$(/bin/find /tmp/tomcatadd.txt | /usr/bin/xargs /bin/grep $ranu3)" == "" ];then
num3=`echo $ranu3`
aias
#add an instance, and make the appropriate settings
aias () {
mkdir -p $ja/$itn/root
echo -e "#!/bin/bash\nexport catalina_base=$tt/$itn\nsh "\$catalina_home"/bin/startup.sh" >$ja/$itn/start.sh
echo -e "#!/bin/bash\nexport catalina_base=$tt/$itn\nsh "\$catalina_home"/bin/shutdown.sh" >$ja/$itn/stop.sh
chmod 755 $ja/$itn/start.sh
chmod 755 $ja/$itn/stop.sh
mkdir $tt/$itn
cp -r $tt/conf/ $tt/$itn
cp -r $tt/webapps/ $tt/$itn
cp -r $tt/temp/ $tt/$itn
cp -r $tt/work/ $tt/$itn
cp -r $tt/logs/ $tt/$itn
sed -i -e s/webapps/"\/javaweb\/$itn"/ $tt/$itn/conf/server.xml
#modify the port number
sed -i s/8005/$num1/ $tt/$itn/conf/server.xml
sed -i s/8080/$num2/ $tt/$itn/conf/server.xml
sed -i s/8009/$num3/ $tt/$itn/conf/server.xml
#add an account
useradd $un
echo $pa | passwd --stdin $un &>/dev/null
chown -r $un $ja/$itn
chown -r $un $tt/$itn
#information confirmation
echo -e "\n"
echo "make sure the information is correct"
sed -n '22p' $tt/$itn/conf/server.xml
sed -n '79p' $tt/$itn/conf/server.xml
sed -n '105p' $tt/$itn/conf/server.xml
sed -n '137,138p' $tt/$itn/conf/server.xml
#information output
echo -e "你好,\n$itn服务器如下:\nip:192.168.1.1\n管理员账号:$un\n密码:$pa\n\n账号权限仅限于以下目录:\ntomcat实例路径:$tt/$itn \n程序存放目录:$ja/$itn\n程序本地直接测试端口号:$num2(不允许私自更改)\n域名生效后,程序外网访问:$itn \n\n另外提示下: \n$ja/$itn目录下的 \nstart.sh为启动应用程序 \nstop.sh为关闭应用程序 \n谢谢!"
#the main program
add