zoukankan      html  css  js  c++  java
  • sed 命令及shell的if语句的运用实例

    if [ -f /etc/syslog.conf ];     #判断文件是否存在
    then
    systest=`sed -n  '/^auth.info/p' /etc/syslog.conf`;    #匹配/etc/syslog.conf文件中是否有auto.info字符串,并返回字符串,systest变量接收。
    if [ -n "$systest" ];                                               #判断字符串是否存在
    then
    sed -e 's/^auth.info.*/auth.info        \/var\/adm\/authlog/g' /etc/syslog.conf > config.tmp && mv config.tmp /etc/syslog.conf;  #存在则替换
    else
    awk '{print $0} END{print "auth.info        /var/adm/authlog"}' /etc/syslog.conf > config.tmp && mv config.tmp /etc/syslog.conf; #不存在则在最后一行添加,solaris系统下不支持sed -i时使用。

    sed -i '$a auth.infos    /var/adm/authlogs' /etc/syslog.conf;      #不存在则在最后一行添加。 redhat,suse
    fi;                                                                              #第一个内层判断结束
    syserr=`sed -n  '/^*.err;auth.info/p' /etc/syslog.conf`;
    if [ -n "$syserr" ];
    then
    sed -e 's/^*.err;auth.info.*/*.err;auth.info        @${loghost}/g' /etc/syslog.conf > config.tmp && mv config.tmp /etc/syslog.conf;
    else
    awk '{print $0} END{print "*.err;auth.info        @${loghost}"}' /etc/syslog.conf > config.tmp && mv config.tmp /etc/syslog.conf;
    fi;                                                                         #第二个内层判断结束
    fi;                                                                      
    if [ -f /var/adm/authlog ];
    then
    echo "/var/adm/authlog exist.";
    else
    touch /var/adm/authlog;                                #创建文件
    chown root:system /var/adm/authlog;             #赋权
    chmod 600 /var/adm/authlog;                         #赋权
    echo "create file /var/adm/authlog.";
    fi;";

  • 相关阅读:
    Transformation
    Yet Another Number Sequence
    Table
    K个联通块
    Acyclic Organic Compounds
    Sandy and Nuts
    tetrahedron
    BZOJ4750 密码安全
    实现商品秒杀 时间倒计时
    实现qq登录
  • 原文地址:https://www.cnblogs.com/resie/p/4796967.html
Copyright © 2011-2022 走看看