zoukankan      html  css  js  c++  java
  • shell之脚本片断

    16.

    以下是平台信息
    CentOS Linux release 7.1.1503 (Core)
    Linux mysql-dev1 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
    
       25  curl -O http://memcached.org/files/memcached-1.4.25.tar.gz
       26  curl -O http://mirrors.cnnic.cn/apache/zookeeper/stable/zookeeper-3.4.8.tar.gz
       27  curl -O http://www.apache.org/dyn/closer.cgi?path=/activemq/5.9.1/apache-activemq-5.9.1-bin.tar.gz 官网提供的这个链接无效
       28  curl -O http://archive.apache.org/dist/activemq/5.9.1/apache-activemq-5.9.1-bin.tar.gz
       31  curl -O https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
    
    
    
    1.免密码登录,就这两步
    ssh-keygen -t rsa -P ''
    scp .ssh/id_rsa.pub 192.168.1.242:/root/.ssh/authorized_keys
    
    [root@manage ~]# cat manager.sh
    while echo "please input host which you want connect: "
    do select host in 240 241 242 238-db 247-db 248-db
       do
       break
       done
            case $host in
             "240") ssh 192.168.1.240;;
             "241") ssh 192.168.1.241;;
             "242") ssh 192.168.1.242;;
          "238-db") ssh 192.168.1.238;;
          "247-db") ssh 192.168.1.247;;
          "248-db") ssh 192.168.1.248;;
               *)  break;;
            esac
    done
    
    
    2.安装jdk与tomcat
    mkdir packages
    cd packages/
    curl -O ftp://192.168.1.100/03%BF%AA%B7%A2%B9%A4%BE%DF/Java/jdk/jdk-7u79-linux-x64.rpm
    curl -O ftp://192.168.1.100/03%BF%AA%B7%A2%B9%A4%BE%DF/Tomcat/apache-tomcat-8.0.30.tar.gz
    
    rpm -ivh jdk-7u79-linux-x64.rpm
    cat <<EOF > /etc/profile.d/java.sh
    JAVA_HOME=/usr/java/jdk1.7.0_79
    CLASSPATH=$CLASSPATH:$JAVA_HOME/lib
    PATH=$JAVA_HOME/bin:$PATH
    export JAVA_HOME PATH CLASSPATH
    EOF
    source /etc/profile
    
    tar zxvf apache-tomcat-8.0.30.tar.gz
    ./apache-tomcat-8.0.30/bin/startup.sh
    
    3.安装mysql
    
    #hostnamectl set-hostname mysql-test-cn
    #systemctl stop firewalld
    #cd /root/packages/mysql
    #yum -y install perl net-tools nfs-utils
    #systemctl start rpcbind
    #rpm -e --nodeps mariadb-libs
    #mount 192.168.1.250:/nfs/html /mnt
    #cd /mnt
    #rpm -ivh mysql-commercial-client-5.7.10-1.1.el7.x86_64.rpm mysql-commercial-common-5.7.10-1.1.el7.x86_64.rpm mysql-commercial-libs-5.7.10-1.1.el7.x86_64.rpm mysql-commercial-server-5.7.10-1.1.el7.x86_64.rpm
    #systemctl start mysqld
    pass=$(grep "temporary password" /var/log/mysqld.log |awk  -F ': ' '{print $2}')
    mysql --connect-expired-password -p"$pass" <<EOF
    SET PASSWORD FOR 'root'@'localhost'= "123456";
    grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
    EOF
    #cd
    #umount /mnt
    echo $PWD
    
    4.安装memcached
    
    memcached 1.4.15
    libevent 2.0.21-stable
    
    yum install memcached
    vi /etc/sysconfig/memcached
    grep mem /etc/passwd
    systemctl status memcached
    systemctl start memcached
    systemctl enable memcached
    systemctl status memcached
    memcached-tool  127.0.0.1:11211 stats
    
    5.安装zookeeper
    tar zxvf zookeeper-3.4.8.tar.gz
    cd zookeeper-3.4.8
    cp conf/zoo_sample.cfg conf/zoo.cfg
    mkdir /home/zookeeper
    vi conf/zoo.cfg
    ./bin/zkServer.sh start/stop
    ss -lnp|grep zoo
    ss -lnp|grep 2181
    ./bin/zkCli.sh -server 127.0.0.1:2181
    
    6.安装activemq
    tar zxvf apache-activemq-5.9.1-bin.tar.gz
    cd packages/apache-activemq-5.9.1
    ./bin/activemq start
    ss -lnp|grep 8161
    ss -lnp|grep 11211
    ss -lnp|grep java

    15.

    [Tue Jan 19 11:23:47 1101 /dev/pts/0 192.168.2.250 ~/sh]#bash legs
    please enter name of an animal: ii
    the ii has an unknown number of legs.
    [Tue Jan 19 11:24:28 1101 /dev/pts/0 192.168.2.250 ~/sh]#bash legs
    please enter name of an animal: dog
    the dog has four legs.
    [Tue Jan 19 11:25:20 1102 /dev/pts/0 192.168.2.250 ~/sh]#cat legs
    echo
    -n "please enter name of an animal: " read animal echo -n "the $animal has " case $animal in horse|dog|cat)echo -n "four";; man|kangaroo)echo -n "two";; *)echo -n "an unknown number of";; esac echo " legs."

    14.删除程序产生的日志,只保留一个当天日期的文件,并且在每天20点时清空此文件,感觉有点重复,要么只保留一个文件,要么清空都可以达到减少占用磁盘的目的。

    [Thu Mar 26 10:01:40 1071 /dev/pts/1 192.168.2.109 /usr/local/ps/log]#cat /var/prtg/scripts/pslog
    #find /usr/local/ps/log  -ctime +0|xargs rm -rf
    #>/usr/local/ps/log/debug*
    a=debug`date +%Y%m%d`.log
    for i in `ls *.log`
    do
    if [ "$i" != "$a" ]
     then
     rm -rf $i
     else
     >$i
    fi
    done

    13.

    黑白棋盘
    其实是个双循环,
    外层执行第一层循环时
    内层共执行8次,并判断是单或双,
    是单的话,total是双,tmp余数是0,输出背景为灰的两个空格
    是双的话,total是单,tmp余数是1,输出背景为黑的两个空格
    外层第一层最后一个echo ""是换行
    然后执行第二层外循环
    [Wed Mar 25|15:40:23 ~ 1081 /bin/bash ]# cat black #!/bin/bash for((i=1;i<=8;i++)) do for((j=1;j<=8;j++)) do total=$(($i+$j)) tmp=$(($total%2)) if [ $tmp -eq 0 ] then echo -e -n "33[47m 33[0m" else echo -e -n "33[40m 33[0m" fi done echo "" done

    [Wed Mar 25|15:40:46 ~ 1082 /bin/bash ]# cat .bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi #PS1='[u@h W]$ ' export PS1='[d| w ! $SHELL ]$ '

    12.

    因为有好多的safe进程,所以把每一个列出来并杀死
    [root@aster3 ~]# cat a
    a=`ps -ef|grep safe|grep -v grep|awk '{print $2}'`
    for i in $a
     do
     kill -9 $i
     done
    
    清空每一个日志文件
    [root@84-monitor monitorlog]# cat a
    for i in `ls *.log`
    do
    #echo $i
    > $i
    done

    11.

    日常工作中,处理数据难免会遇到遍历,for循环可能是我们用的比较多的了。本节就来探讨下for语句嵌套循环的性能,猜想下面两个语句的性能。
    外层循环越少效率越高
    [root@250-shiyan sh]# cat inlinefor
    for (( i=1; i<100; i++ ))
    do
     for (( c=1; c<1000; c++ ))
      do
      echo "$c-----inline"
      done
    echo "$i-----outline"
    done
    [root@250-shiyan sh]# cat outlinefor
    for (( i=1; i<1000; i++ ))
    do
     for (( c=1; c<100; c++ ))
      do
      echo "$c-----inline"
      done
    echo "$i-----outline"
    done
    [root@250-shiyan sh]# time (bash inlinefor) |tail -3
    998-----inline
    999-----inline
    99-----outline
    
    real    0m1.754s
    user    0m1.433s
    sys     0m0.319s
    [root@250-shiyan sh]# time (bash outlinefor) |tail -3
    98-----inline
    99-----inline
    999-----outline
    
    real    0m1.731s
    user    0m1.392s
    sys     0m0.338s
    
    [root@250-shiyan sh]# time (bash inlinefor) |tail -3
    9998-----inline
    9999-----inline
    999-----outline
    
    real    3m8.818s
    user    2m25.615s
    sys     0m43.021s
    [root@250-shiyan sh]# time (bash outlinefor) |tail -3
    998-----inline
    999-----inline
    9999-----outline
    
    real    3m5.233s
    user    2m21.791s
    sys     0m43.278s

    10.脚本执行方式及子进程关系

    ###4种方式:全路径/path/to/conns|bash conns|source conns|. conns
    ###要有执行权限产生子shell
    [root@250-shiyan prog]# cat conns #!/bin/bash ip_conns=`ssh $1 "netstat -ant| grep EST | wc -l"` echo $ip_conns [root@250-shiyan prog]# chmod u+x conns [root@250-shiyan prog]# cp conns /usr/bin [root@250-shiyan prog]# conns 192.168.2.109
    [root@250-shiyan prog]# ./conns
    root@192.168.2.109's password:
    851
    ###无需执行权限,产生子shell
    [root@250-shiyan prog]# bash conns
    root@192.168.2.109's password:
    855
    [root@84-monitor ~]# cat a
    a=192.168.2.109
    ip_conns=`ssh $a "netstat -ant| grep EST | wc -l"`
    echo $ip_conns
    ###source命令与点命令是在当前shell中执行,并不产生子shell,也不需要脚本有执行权限。
    [root@84-monitor ~]# source a
    860
    [root@84-monitor ~]# . a
    860

    9.

    shell 判断字符串中是否含有指定字符
    new=sdd
    ps=s
    echo "$new" |grep -q "$ps"
    if [ $? -eq 0 ]
    then
    echo "yes"
    else
    echo "no"
    fi
     
    判断某年某天是星期几?
    year=1980
    end_year=2010
    day_2=10/22
    week_2=Fri
     
    while [ $year -lt $end_year ]
    do
    new=`date -d "$day_2 CST $year"`
    echo "$new"
    echo "$new" |grep -q "$week_2"
    if [ $? -eq 0 ]
    then      
          #  year=`expr $year + 1`
    echo "this year $year include $week_2"
        year=`expr $year + 1`
    else
        #echo "this year $year include $week_2"
        year=`expr $year + 1`
    fi  
    done

    [root@250-shiyan prog]# expr length "$HOME"
    5
    [root@250-shiyan prog]# echo "$HOME"|wc -c
    6
    [root@250-shiyan prog]# echo “$HOME”|awk '{print length($0)}'
    7
    判断字符串为空的方法有三种:
    if [ "$str" = "" ] if [ x"$str" = x ] if [ -z "$str" ] (-n 为非空) 注意:都要用双引号,否则有些命令会报错,养成好习惯吧!

    8.

    [root@250-shiyan frag]# bash here.sh
    USAGE: xtitlebar [-h] "string_for_titelbar"
    OPTIONS: -h help text
    EXAMPLE: xtitlebar "cvs"
    [root@250-shiyan frag]# cat here.sh
    help()
    {
    cat <<HELP
    USAGE: xtitlebar [-h] "string_for_titelbar"
    OPTIONS: -h help text
    EXAMPLE: xtitlebar "cvs"
    HELP
    exit 0
    }
    help

    7.还有点问题,2015/5/25的时候才搞明白下面的问题所在。才理解了下面的这段脚本含义。

    [root@250-shiyan frag]# bash while2.sh
    enter y/n :
    y
    ===============================================
    | unix script test |
    | 1 --- num 1 |
    | 2 --- num 2 |
    | 3 --- num 3 |
    | 4 --- num 4 |
    ===============================================
    enter y/n :
    n
    Press <return> to proceed or type q to quit:
    ef
    Press <return> to proceed or type q to quit:
    ef
    Press <return> to proceed or type q to quit:
    q
    [root@250-shiyan frag]# cat while2.sh
    #!/bin/bash
    banner()
    {
    cat <<echo1
    ===============================================
    | unix script test |
    | 1 --- num 1 |
    | 2 --- num 2 |
    | 3 --- num 3 |
    | 4 --- num 4 |
    ===============================================
    echo1
    }
    
    getyn()
    {
    while echo "enter y/n :"
    do
    read yn
    case $yn in
    [Yy]) return 0 ;;
    yes) return 0 ;;
    YES) return 0 ;;
    [Nn]) return 1 ;;
    no) return 1;;
    NO) return 1;;
    * ) echo "only accept Y,y,N,n,YES,yes,NO,no";;
    esac
    done
    }
    
    pause()
    {
    while echo "Press <return> to proceed or type q to quit:"
    do
    read cmd
    case $cmd in
    ####break跳出while语句
    [qQ]) exit 1;;
    ####引号中间没有任何字符,表示是return
    "") break;;
    ####continue继续下一循环
    *) continue;;
    esac
    done
    }
    
    ####pause or getyn to test
    while getyn    
    do
    banner
    done
    pause

    6.

    [root@250-shiyan frag]# bash while.sh
    input num:
    4
    input is 4
    new num is 5
    new num is 6
    new num is 7
    new num is 8
    new num is 9
    new num is 10
    [root@250-shiyan frag]# cat while.sh
    #!/bin/bash
    echo "input num:"
    read num
    echo "input is $num"
    while test $num -lt 10
    do
    num=$(($num+1))
    echo "new num is $num"
    sleep 2
    done

    5.

    [root@localhost script]# cat >if-1
    #!/bin/bash
    echo -e "are you ok(y/n or maybe)? c"
    read answer
    if [[ $answer == [yY]* || $answer = maybe ]]
    then echo "glad to hear it"
    fi
    [root@localhost script]# bash if-1
    are you ok(y/n or maybe)? dk
    [root@localhost script]# bash if-1
    are you ok(y/n or maybe)? y
    glad to hear it
    [root@localhost script]# bash if-1
    are you ok(y/n or maybe)? Y
    glad to hear it
    [root@localhost script]# bash if-1
    are you ok(y/n or maybe)? maybe
    glad to hear it

    4.||表示只要其中之一个,满足即可,&&表示两个,以至n个条件都要满足才可以。

    ####也可以一试
    if [[ "$s" -gt "0" ||  "$r" -gt "0" ]]

    [root@localhost script]# cat >if-2 #!/bin/bash echo -e "how old are you? c" read age if (( age < 0 || age > 120 )) then echo "you are so old" ####两个同时满足,即上限与下限 elif (( age >=0 && age <= 12 )) then echo "you are child" elif (( age >=13 && age <=19 )) then echo "you are 13-19 years old" elif (( age >=20 && age <=29 )) then echo "you are 20-29 years old" elif (( age >=30 && age <=39 )) then echo "you are 30-39 years old" else echo "you are above 40" fi [root@localhost script]# bash if-2 how old are you? 30 you are 30-39 years old [root@localhost script]# bash if-2 how old are you? 28 you are 20-29 years old [root@localhost script]# bash if-2 how old are you? 400 you are so old [root@localhost script]# bash if-2 how old are you? 0 you are child

    3.检查根分区,循环做两件事,a.输出当前值,b.如果大于10%,则输出信息

    [root@250-shiyan sh]# cat check-root.sh
    #!/bin/bash
    while sleep 5
      do
        for i in `df -h |sed -n '//$/p'|awk '{print $5}'|sed 's/\%//g'`
             do echo $i
             if [ $i -ge 10 ]
             then
             echo " more than 10$ linux of disk space."
             fi
             done
      done
    
    [root@250-shiyan sh]# bash check-root.sh
    11
     more than 10$ linux of disk space.
    11
     more than 10$ linux of disk space.

    2.自编有while循环,有函数,有if,还有脚本参数,执行时  ./while w1或./while w2

    [root@250-shiyan sh]# cat while
    #!/bin/bash
    w1 () {
    min=1
    max=100
    while [ $min -le $max ]
    do
    echo $min
    min=`expr $min + 1`
    done
    }
    
    w2 () {
    i=1
    while(($i<100))
    do
    if(($i%4==0))
    then
    echo $i
    fi
    i=$(($i+1))
    done
    }
    
    if [ $@ = w1 ]
    then
    w1
    else
    w2
    fi

    1.自编让其以后台进程形式存在,不用crontab去定期执行

    [root@250-shiyan sh]# cat eth
    #!/bin/bash
    while [ 1 -gt 0 ]
    do
    eth=`ifconfig eth0|grep "TX bytes"|gawk '{print $6}'|cut -d ":" -f2`
    echo $eth >> /root/sh/jj
    sleep 2
    done
    
    [root@250-shiyan sh]# bash eth &
    [root@250-shiyan sh]# tail -f jj
  • 相关阅读:
    .net程序调用检测和性能分析工具——DotTrace
    HR系统邮件审批功能总结
    添加AD验证(域身份验证)到现有网站
    【事务】:数据库事务隔离级别、脏读、幻读、不可重复读
    【TensorFlow】:解决TensorFlow的ImportError: DLL load failed: 动态链接库(DLL)初始化例程失败
    【Anaconda】:科学计算的Python发行版
    【Junit4】:要点随笔
    【ElasticSearch】:elasticsearch.yml配置
    【ElasticSearch】:Windows下ElasticSearch+版本安装head
    ArrayList、Vector、HashMap、HashTable、HashSet的默认初始容量、加载因子、扩容增量
  • 原文地址:https://www.cnblogs.com/createyuan/p/3778241.html
Copyright © 2011-2022 走看看