04.17 Shell函数数组

第一章 统计字符串长度的方法

1. 统计字符串长度的方法

char=oldboy
echo ${#char}    *****
echo $char |awk '{print length}'
echo $char |wc -L
expr length $char
[root@nginx02_8 script]# vim char.sh
#!/bin/bash
. /etc/init.d/functions

for char in I am oldboy teacher I like linux
do
    if [ ${#char} -lt 4 ]
    then
        echo $char
    fi
done

2. 打印选择菜单,按照选择一键安装不同的Web服务。
打印选择菜单,按照选择一键安装不同的Web服务。
1.[install lamp]
2.[install lnmp]
3.[exit]

#!/bin/bash
. /etc/init.d/functions

while true;do clear
cat <<END
    1.[install lamp]
    2.[install nginx]
    3.[exit]
END
    read -p "please input the number of you want" num
    case "$num" in
        1)
            action "[install lamp]..." /bin/true
            sleep 2
            ;;
        2)
            action "[install nginx]..." /bin/true
            sleep 2
            ;;
        3)
            exit
    esac
done

3. 监控网站是否可以打开

#!/bin/bash
. /etc/init.d/functions

url=(http://www.baidu.com
http://oldboy.blog.51cto.com
http://blog.oldboyedu.com
http://10.0.0.7)

check(){
    wget -q -o /dev/null --spider --timeout=5 --tries=2 "$1"
    if [[ $? -eq 0 ]]; then
        action "$1" /bin/true
    else
        action "$1" /bin/false
    fi
}

main(){
    while true; do
        for i in ${url[*]}; do
            check "$i"
            sleep 3
        done
    done
}
clear
main

第二章 企业Shell运维实战案例

  1. 使用for循环在/oldboy目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串oldboy
#!/bin/bash
. /etc/init.d/functions

[ -d /oldboy ] && cd /oldboy || mkdir -p /oldboy && cd /oldboy
for i in `echo oldboy{00..10}`; do
        word=`uuidgen|md5sum |tr "0-9" "a-z"|cut -c 1-10`
        touch ${word}_$i.html
        if [ $? -eq 0 ]; then
                action "${word}_$i.html" /bin/true
        fi
done
  1. 将上次文件名中的oldboy字符串全部改成oldgirl(最好用for循环实现),并且将扩展名html全部改成大写。
#!/bin/bash
. /etc/init.d/functions
cd /oldboy
new_File=_oldgirl.HTML


for i in `ls /oldboy`; do
        name=$(echo $i | awk -F "_" '{print $1}')
        mv  $i ${name}${new_File}
done
  1. 批量创建10个系统账号oldboy01-oldboy10并设置密码(密码为随机数,要求字符和数字的混合)
#!/bin/bash
. /etc/init.d/functions

for i in `echo oldboy{01..10}`; do
    oldboy_Password=`uuidgen|sed 's#-##g'|cut -c 1-10`
    useradd $i && echo ${oldboy_Password} |passwd --stdin $i &>/dev/null
    if [[ $? -eq 0 ]]; then
        action "$i 用户创建" /bin/true
    fi
    echo "用户名:$i  用户密码:${oldboy_Password}" >>/tmp/password
done
  1. 写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?
#!/bin/bash
. /etc/init.d/functions

ip="10.0.0."
check="ping -w 2 -c 2"
for i in `seq 254`; do
    $check $ip$i &>/dev/null
    if [[ $? -eq 0 ]]; then
        action "$ip$i" /bin/true
    else
        action "$ip$i" /bin/false
    fi
done

  1. 写一个Shell脚本解决DOS攻击生产案例。
    请根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100(读者根据实际情况设定),即调用防火墙命令封掉对应的IP。防火墙命令为:iptables -I INPUT -s IP地址 -j DROP。
#!/bin/bash
. /etc/init.d/functions

#file=$1 定义一个变量接收命令行传参,参数为日志文件类型
while true; do
    awk '{print $1}' $1|grep -v "^$"|sort|uniq -c >/tmp/tmp.log
    #分析传入的日志文件,并在排序去重后追加到一个临时文件里。
    while read line; do
        ip=$(echo $line|awk '{print $2}')  #获取文件中的每一行的第二列ip列
        count=$(echo $line|awk '{print $1}')  #获取文件中的每一行的第一列
        if [ $count -gt 500 ] && [ $(iptables -nL |grep "$ip"|wc -l -lt 1) ]; then
            iptables -I INPUT -s $ip -j DROP
        fi
    done </tmp/tmp.log
    sleep 3600  #日志的分隔或过滤分析得按分钟执行
done
  1. MySQL启动脚本
[root@db02 scripts]# cat /etc/init.d/oldgirl
#!/bin/bash
# chkconfig: 2345 64 36
# description: MySQL startup
#Author:oldboy
#Blog:http://oldboy.blog.51cto.com 

#Time:2017-07-07 09:24:34
#Name:mysqld.sh 

#Version:V1.0
#Description:This is a test script.
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
Port=3306
User="root"
Bindir="/application/mysql/bin"
Datadir="/application/mysql/data"
mysqld_pid_file_path="/application/mysql/`hostname`.pid"
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH
return_value=0


# Lock directory.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"

log_success_msg(){ 
    echo " SUCCESS! $@"
}   
log_failure_msg(){     
    echo " ERROR! $@"
}  



case "$1" in
    start)            
        # Start daemon
        echo "Starting MySQL"
        if test -x $Bindir/mysqld_safe
        then
            $Bindir/mysqld_safe --datadir="$Datadir" --pid-file="$mysqld_pid_file_path"  >/dev/null &
            return_value=$?
            sleep 2

            # Make lock for CentOS
            if test -w "$lockdir"
            then
                touch "$lock_file_path"
            fi
            exit $return_value
        else
            log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
        fi
        ;;
    stop)
        if test -s "$mysqld_pid_file_path"
        then
            mysqld_pid=`cat "$mysqld_pid_file_path"`

            if (kill -0 $mysqld_pid 2>/dev/null)
            then
                echo "Shutting down MySQL"
                kill $mysqld_pid
                return_value=$?
                sleep 2
            else
                log_failure_msg "MySQL server process #$mysqld_pid is not running!"
                rm -f "$mysqld_pid_file_path"
            fi
            # Delete lock for CentOS
            if test -f "$lock_file_path"
            then
                rm -f "$lock_file_path"
            fi
            exit $return_value
        else
            log_failure_msg "MySQL server PID file could not be found!"
        fi
        ;;
    restart)
        if $0 stop; then
            $0 start
        else
            log_failure_msg "Failed to stop running server, so refusing to try to start."
            exit 1
        fi
        ;;

    *)
        echo "Usage: $0  {start|stop|restart}"
        exit 1
        ;;
esac
exit $return_value
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值