• 扫盲


    pgrep tail | xargs kill -9    #xargs:通过缓冲方式并以前面命令行的输出作为参数

    kill -9 `pgrep tail`

    find /root -name 11.sh -exec rm {} ;

    cat 22.sh   1>/dev/null   标准输出 :只输出错误信息。

    cat 22.sh   2>/dev/null   #标准错误输出 :只输出正确结果。

    log=/tmp/`date "+%Y-%m-%d_%H_%M"`.top

    echo "11" >> $log    #生成日志

    top -p 1 -b -d 1 -n 1|awk 'BEGIN{print strftime("%Y-%m-%d %H:%M:%S");}'

    awk 'BEGIN{print strftime("%Y %m %d %H:%M:%S", 1324)}'

    awk 'BEGIN{print strftime("%Y-%m-%d %H:%M:%S");}' >> asas.log
    top -p 1 -b -d 1 -n 1 >> asas.log

    ctrl + z  后台运行

    jobs 查看后台运行的进程

    fg 将后台运行的进程调到前台。

    返回值为字符,会报错:

    return "my name is bixiaopeng"

    Shell自定义函数总结.sh: line 6: return: my: numeric argument required

    1.变量通过" "引号引起来

    #!/bin/sh
    para1=
    if [ ! -n "$para1" ]; then
      echo "IS NULL"
    else
      echo "NOT NULL"
    fi
    

      

    4.使用""判断
     
    #!/bin/sh 
    dmin=
    if [ "$dmin" = "" ]
    then
      echo "dmin is not set!"
    else  
      echo "dmin is set !"
    fi
    

      

    #自动挂载Swap
    autoSwap()
    {
    	swap=`free |grep Swap|awk '{print $2}'`
    	if [ $swap -gt 1 ];then
            echo "Swap total sizse: $swap";
    		return;
    	fi
    	if [ ! -d /www ];then
    		mkdir /www
    	fi
    	swapFile='/www/swap'
    	dd if=/dev/zero of=$swapFile bs=1M count=1025
    	mkswap -f $swapFile
        swapon $swapFile
        echo "$swapFile    swap    swap    defaults    0 0" >> /etc/fstab
    	swap=`free |grep Swap|awk '{print $2}'`
    	if [ $swap -gt 1 ];then
            echo "Swap total sizse: $swap";
    		return;
    	fi
    	
    	sed -i "//www/swap/d" /etc/fstab
    	rm -f $swapFile
    }
    autoSwap
    
    #判断kernel-headers组件是否安装
    rpm -qa | grep kernel-headers > kernel-headers.pl
    kernelStatus=`cat kernel-headers.pl`
    #判断华为云
    huaweiLogin=`cat /etc/motd |grep 4000-955-988`
    huaweiSys=`cat /etc/redhat-release | grep ' 7.'`
    if [ "$kernelStatus" = "" ]; then
    	if [ "$huaweiLogin" != "" ] && [ "$huaweiSys" != "" ]; then
    		wget $download_Url/src/kernel-headers-3.10.0-514.el7.x86_64.rpm
    		rpm -ivh kernel-headers-3.10.0-514.el7.x86_64.rpm
    		rm -f kernel-headers-3.10.0-514.el7.x86_64.rpm
    	else
    		yum install kernel-headers -y
    	fi
    fi
    rm -f kernel-headers.pl
    
    
    

      

    安装ntp,并同步时间

    yum install ntp -y
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    #尝试同步时间(从bt.cn)
    echo 'Synchronizing system time...'
    v1=$(curl http://www.bt.cn/api/index/get_time)
    date -s "$(date -d @$v1 +"%Y-%m-%d %H:%M:%S")"
    
    #尝试同步国际时间(从ntp服务器)
    ntpdate 0.asia.pool.ntp.org
    

      

    startTime=`date +%s`
    setenforce 0
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    for pace in python-devel python-imaging zip unzip openssl openssl-devel gcc libxml2 libxml2-devel libxslt* zlib zlib-devel libjpeg-devel libpng-devel libwebp libwebp-devel freetype freetype-devel lsof pcre pcre-devel vixie-cron crontabs icu libicu-devel c-ares;
    do
    	yum -y install ${pace}; 
    done
    
    if [ -f "/usr/bin/dnf" ]; then
    	dnf install -y redhat-rpm-config
    fi
    yum install python-devel -y
    
    py26=$(python -V 2>&1|grep '2.6.')
    if [ "$py26" != "" ];then
    	if [ ! -f /etc/yum.repos.d/epel.repo ];then
    		wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    	fi
        if [ ! -f /usr/bin/python3 ];then
    		yum install python34 python34-devel -y
    		if [ ! -f /usr/bin/python3 ];then
    			echo "python3.4 install error!"
    			exit 0;
    		fi
    		isSed=$(cat /usr/bin/yum|grep /usr/bin/python2.6)
    		if [ "$isSed" == "" ];then
    			sed -i "s#/usr/bin/python#/usr/bin/python2.6#" /usr/bin/yum
    		fi
    		#rm -f /usr/bin/python2
    		mv -f /usr/bin/python /usr/bin/python2_backup
    		ln -sf /usr/bin/python3 /usr/bin/python
    	fi
    	if [ ! -f /usr/bin/pip3 ];then
    		wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
    		python3 get-pip.py
    		mv -f /usr/bin/pip /usr/bin/pip_backup 
    		ln -sf /usr/bin/pip3.4 /usr/bin/pip
    	fi
    fi
    

      

    awk

    https://www.cnblogs.com/ginvip/p/6352157.html

     

    打印字段分割符

    awk -F ":" '{print FS}' /etc/passwd
    :
    :
    

    打印字段数量

    awk -F ":" '{print NF}' /etc/passwd
    7
    7
    

    打印记录的行数

    awk '{print NR}' /etc/passwd
    1
    2
    3
    

      加上说明进行打印  ;   Tab键

    [root@localhost ~]# awk -F":" '{ print "username: " $1 "		uid:" $3 }' /etc/passwd
    username: root		uid:0
    username: bin		uid:1
    

      

    awk -F ":" '{if(NR>=20 && NR<=30) print $1}' /etc/passwd
    tss
    postfix
    

      

     从该文件中过滤出'Poe'字符串与33794712,最后输出的结果为:Poe 33794712

    [root@localhost ~]# cat test.txt 
    I am Poe,my qq is 33794712
    
    [root@localhost ~]# awk -F '[ ,]+' '{print $3" "$7}' test.txt
    Poe 33794712
    

      

    awk 'BEGIN{info="this is a test";split(info,tA," ");print length(tA);for(k in tA){print k,tA[k];}}'
    4    #length(tA) tA数组的长度
    4 test    #下标为4的值
    1 this
    2 is
    3 a
    #分割 info,动态创建数组 tA,awk for …in 循环,是一个无序的循环。 并不是从数组下标

      

    ++s[$NF]   
    #y[x]定义一个数组s,
    下标的名称设置为最后一列($NF),
    ++ 以最后一列($NF)为标准计数,遇到一个加一,最后将统计出的数字赋值给[键或者说是下标名称,这里是最后一列的名称]
    #总结:以最后一列分类统计。键为最后一列的名称,值为最后一列出现的次数。
     netstat -an|awk '/^tcp/{++s[$NF]}END{for(a in s)print a,s[a]}'
    

      

     
    [root@localhost nginx]# awk 'BEGIN{citie[1]="beijin"} {print citie[1]}' /etc/hosts
    beijin
    beijin
    beijin
    
    [root@localhost nginx]# awk 'BEGIN{citie[1]="beijin"} END{print citie[1]}' /etc/hosts
    beijin
    

      

    [root@localhost nginx]# awk 'BEGIN{citie[1]="beijin"} {print "a"} END{print citie[1]}' /etc/hosts
    a
    a
    a
    beijin
    

      

     
  • 相关阅读:
    第二十八节:EFCore6.x版本新功能总结
    C#开发常用技巧
    awk去掉最后几个字母
    AB中主从表,子表删除时出现缺少Where错误的解决方法
    ABQuery的查询语句关联了其它表,增加记录时,提示关联表无法更新的解决方法
    cxGrid动态设置单元格对齐方式
    4.0后续增加功能和修正Bug
    Windows的Proifier报错Cannot configure Windows network.Try to run SysSettings tool manually
    GO orm 框架GORM学习
    js根据某属性对json数组分类
  • 原文地址:https://www.cnblogs.com/linuxws/p/11048725.html
Copyright © 2020-2023  润新知