zoukankan      html  css  js  c++  java
  • shell脚本编写实例

    实际案例

    1.判断接收参数个数大于1 

          [ $# -lt 1 ] && echo "至少需要一个参数" && { echo "我要退出了.... "; exit; } || echo "搞到参数"

    2.统计文件夹 和 文件数

         let etcd=`ls -l /etc| grep "^d"|wc -l`

         let etcf=`ls -l /etc| grep "^-"|wc -l`

         let sum=$[$etcd+$etcf]

    3.取某列的最大值

         number=`df | grep '^/dev' | tr -s " " " "|cut -d" " -f5|tr -s "%" " "|sort -n|tail -n1`

    4.短路条件判断

        [ $# -ne 2 ] && echo "need two args" && exit

    5.统计文件的空白行

        let b=`grep '^$' $2|wc -l`

    6.ping通远程主机 

        ping -c2 $1 &> /dev/null
        [ $? -eq 0 ] && echo "$1 可以被ping通" || echo "$1 不可以被ping通"

    7.一次性删除大批量文件

       ls |xargs rm -rf 当删除的文件数量过多,超过了所支持的参数数量上限时,可配合管道及xargs来删除。

    8.删除文件中不连续的重复行

        sort -k2n file | uniq > a.out

        当file中的重复行不再一起的时候,uniq没法删除所有的重复行。经过排序后,所有相同的行都在相邻,因此unqi可以正常删除重复行。

    9.分组倒序排序列表

       cat access_log | cut -d" " -f1|sort|uniq -c|sort -nr|head -n10

    10.把多条命令当成一条命令集合来执行

       id $1 &> /dev/null && echo "$1 user has added" || { useradd $1; echo "add user $1 success"; }

     

    11.查看文件夹和文件的大小  du    -sh

        [root@centos7 ~]# du -sh appserver/     appserver是目录
          5.0M          appserver/
       [root@centos7 ~]# du  -sh  f1                  f1是文件
          4.0K         f1

    12.查看端口占用情况

       1、lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000

         

          可以看到8000端口已经被轻量级文件系统转发服务lwfs占用

       2、netstat -tunlp |grep 端口号,用于查看指定的端口号的进程情况,如查看8000端口的情况,netstat -tunlp |grep 8000

       

       

    shell编程知识点

    1.(cmd1;exit;) 和 {cmd2;exit;}的区别

       ()会开启一个子线程,exit退出的是子进程,对执行线程没有影响

       { }不会开启子线程,exit退出的是执行进程本身.

     2.source .bashrc和bash .bashrc的区别

        source 表示.bashrc在当前shell进程中运行

        bash 会开启一个子shell进程来执行当前shell 脚本

    3.字符串匹配

          [ "$var"="ha" ] 精确判断$var字符串是否等于ha
          [[ "$var" ~="regx" ]]
          [ x"$var"="x" ] 判断$var字符串是否为空

    4.变量格式的保留

         echo $name 把所有的行压缩成了一行,不保留文件内容换行格式.
         echo "$name" 添加双引号保留原文件内容的换行格式.

    5.bash调试方式

         1.检查语法错误                          bash -n test.sh
         2.跟踪调试执行脚本                 bash -x test.sh

    6.bash的配置文件

           1.全局配置
                  1./etc/profile
                   2./etc/profile.d/*.sh
                   3./etc/bashrc
            2.个人配置
                   1.~/.bash_profile
                   2.~/.bashrc

    7.shell和其它语言的不同之处

         shell把空字符串和1相加运算会得到1,并不会提示错误.其它语言空类型是不能和整数进行运算的

    [root@centos7 ~]# a=""
    [root@centos7 ~]# b=a+1
    [root@centos7 ~]# echo b
    b
    [root@centos7 ~]# echo $b
    a+1
    [root@centos7 ~]# let b=a+1
    [root@centos7 ~]# echo $b
    1
    View Code
  • 相关阅读:
    1143 Lowest Common Ancestor (30)
    PAT 1135 Is It A Red-Black Tree
    PAT 1119 Pre- and Post-order Traversals
    1102 Invert a Binary Tree(25 分)
    PAT总结
    c++ getline的用法
    PAT 1049 Counting Ones (30)
    PAT 1022 Digital Library (30)
    java jar包
    NIO的理解
  • 原文地址:https://www.cnblogs.com/yxh168/p/8779174.html
Copyright © 2011-2022 走看看