标记:开始学习while循环
查看本地的8080端口的应用是否已启用:
curl -s -o index.jsp -m 2 localhost:8080/shop/index.jsp
#!/bin/bash
N=0
while [ $N -lt 5 ]; do
let N++
echo $N
done
#循环读取文件的一行,然后输出 read命令读取到数据,状态是0,条件为真,读取不到数据则条件为假
cat ./catalina.out | while read LINE ; do
echo $LINE
done
#另一种方法
while read LINE2 ; do
echo $LINE2
done <./catalina.out
#永真条件的三种表示
while :; do
echo "yes"
done
while true ; do
echo "yes"
done
while [ 1 -eq 1 ] ; do
echo "yes"
done
#跳出当前循环和终止循环
continue break
N2=0
while true; do
let N2++
if [ $N2 -eq 5 ]; then
break
fi
echo $N2
done
#continue
N3=0
while [ $N3 -lt 5 ]; do
let N3++
if [ $N3 -eq 3 ]; then
continue
fi
echo $N3
done
case的学习实践:启动tomcat脚本 ./tomcat.sh 8080 start
#!/bin/bash
# function :scripts is for tomcat stop/start 8080-8089
TOMCAT_BASH_DIR=/u02
function usage()
{
echo "tomcat.sh {port|all} {start|stop}"
exit 1
}
case $2 in
start)
if [ $1 == all ]; then
for i in $(seq 8080 8089)
do
[ -f $TOMCAT_BASH_DIR/tomcat$i/bin/shutdown.sh ] && $TOMCAT_BASH_DIR/tomcat$i/bin/shutdown.sh >/dev/null 2>&1
[ -f $TOMCAT_BASH_DIR/tomcat$i/bin/startup.sh ] && $TOMCAT_BASH_DIR/tomcat$i/bin/startup.sh
done
else
[ -f $TOMCAT_BASH_DIR/tomcat$1/bin/shutdown.sh ] && $TOMCAT_BASH_DIR/tomcat$1/bin/shutdown.sh >/dev/null 2>&1
[ -f $TOMCAT_BASH_DIR/tomcat$1/bin/startup.sh ] && $TOMCAT_BASH_DIR/tomcat$1/bin/startup.sh
fi
;;
stop)
echo "stop!"
if [ $1 == all ] ; then
for i in $(seq 8080 8089)
do
[ -f $TOMCAT_BASH_DIR/tomcat$i/bin/shutdown.sh ] && $TOMCAT_BASH_DIR/tomcat$i/bin/shutdown.sh >/dev/null 2>&1 && echo "stop $i"
done
else
[ -f $TOMCAT_BASH_DIR/tomcat$1/bin/shutdown.sh ] && $TOMCAT_BASH_DIR/tomcat$1/bin/shutdown.sh >/dev/null 2>&1 && echo "stop $1"
fi
;;
*)
usage
;;
esac
exit 0
select和unitl语句的语法:
select variable in list
do
循环体,可以用break终止
done
主要作用:用于创建菜单,按数字顺序排列list,用户输入保存在变量REPLY中
#!/bin/bash
PS3="提示语:"
select menu in start stop restart quit
do
# $REPLY 存储的是所选菜单的第几项
# case $REPLY in
case $menu in
start)
echo "the tomcat while be start"
;;
stop)
echo "the tomcat while be stop"
;;
restart)
echo "the tomcat while be restart"
;;
quit)
break
;;
*)
echo "no the option"
esac
done
PS3="提示语:"
select menu in start stop restart quit
do
# $REPLY 存储的是所选菜单的第几项 如输入 1 则执行start
case $REPLY in
# case $menu in
1)
echo "the tomcat while be start"
;;
2)
echo "the tomcat while be stop"
;;
3)
echo "the tomcat while be restart"
;;
4)
break
;;
*)
echo "no the option"
esac
done
unitl 循环条件 ; do
循环体
done
使用:循环条件为真时,一直执行循环体;为假时,结束循环(和while相反)
检测8080端口,一旦发现就杀掉8080的进程
#!/bin/bash
until ps -ef |grep 8080|grep -v grep &> /dev/null; do
sleep 0.5
done
kill -9 $(ps -ef|grep -ef 8080|grep -v grep|awk '{print $2}')
until pgrep -u tomcat &> /dev/null; do
sleep 0.5
done
pkill -9 -u tomcat
第五章:函数和数组
函数的定义:function fun(){
函数体
}
或:fun(){
函数体
}
函数的调用:函数名 fun
#!/bin/bash
fun(){
echo "this is a function"
}
fun
#返回值只能是0-255 的数字
func() {
VAR=$((1+1))
return $VAR
echo "This is a function."
}
func
echo $?
#递归调用
test() {
echo $1
sleep 1
test hello
}
test
#递归使用时,一定要有终止条件 深水炸弹
bomb() {
bomb|bomb&
};
bomb
数组:数组元素间使用空格作为分割符
格式: array=(元素 1 元素 2 元素 3 ...)
array=(a b c)
array=($(command))
array[下标]=元素
#!/bin/bash
arr=(1 2 3 4)
#获取数组长度 字符串的长度 ${#var}
echo ${#array[*]}
#获取数组所有元素
echo ${array[*]}
#获取元素下标:
echo ${!a[@]}
#获取对应下标的元素
echo ${array[2]}
#添加元素
arr[4]=e
#添加多个元素,数组和数组可以直接加,构成一个新数组
arr+=(f g h)
#删除元素
unset arr[0]
#删除数组
unset array
#遍历数组,依次启动tomcat
PORTS=(8080 8081 8082 8083)
for port in ${PORTS[*]} ; do
./starttomcat $port
done
for (( i=0;i<${#PORTS[*]} ; i++)) ; do
echo ${PORTS[$i]}
done