linux shell 实用小实例

  1. 判断当前用户是否为root管理员,如果是,则执行tar对/etc/目录执行备份.

    #!/bin/bash
    # IF current user is root then backup the /etc
    if [ "$(id -u)" -eq 0 ];then
      tar -czf /root/etc.tar.gz /etc &>/dev/null
    else
      echo ‘Please switch user on root.‘
      exit 9
    fi

  2. 要求用户输入密码,判断密码是否正确.

    #!/bin/bash
    # Read password and test.
    read -p "Enter a password:" password
    if [ "$password" == "pass" ];then
      echo "OK."
    else
      echo "Your password is Wrong."
      exit 9
    fi

  3. 九九乘法表

    #!/bin/bash
    # 九九乘法表
    for ((i=1;i<=9;i++))
    do
     for ((j=1;j<=i;j++))
      do
       echo -n "$j*$i=$[$i*$j] "
       #printf "%-8s" $j*$i=$(($i*$j))
      done
      echo
    done

  4. 批量发送邮件

    #!/bin/bash
    #批量发送邮件
    DOMAIN=sharelinux.com
    for MAIL_U in tom allen whistle
    do
        mail -s "Log" $MAIL_U@${DOMAIN} </var/log/message
    done

  5. 打印网卡配置文件信息

    #!/bin/bash
    #Read /etc/sysconfig/network-scripts/ifcfg-eth0 and print out.

    LINE=/etc/sysconfig/network-scripts/ifcfg-eth0
    while read -r line
    do
    echo $line
    done < $LINE

  6. 备份MySQL数据库

    #!/bin/bash
    # This scripts is backup mysql database.
    DATE=$(date +%Y-%m-%d)
    DES=/usr/local/src/DB_MySQL
    MYSQL_U="root"                  #mysql用户名
    MYSQL_P=""                      #mysql密码,需要修改密码
    MYSQL_H="127.0.0.1"             #mysql服务器IP  

    if [ ! -d $DES ];then
      mkdir -p "$DES"
    fi

    #获取数据库名称列表
    DBS=$(mysql -u${MYSQL_U} -h${MYSQL_H} -Bse ‘show databases‘)
    #通过循环执行备份所有的MySQL数据库
    for database in $DBS
    do
      if [ ! "${database}" == "information_schema" ];then
         mysqldump -u$MYSQL_U -h$MYSQL_H ${database} |bzip2> "${DES}/${database}_${DATE}_mysql.gz"
         #mysqldump -u$MYSQL_U -P$MYSQL_P -h$MYSQL_H $database|bzip2> "${DES}/${database}_${DATE}_mysql.gz"
         md5sum ${DES}/${database}_${DATE}_mysql.gz>>${DES}/ReadMe.txt
      else
        :
      fi
    done


本文出自 “心静梵音” 博客,请务必保留此出处http://mastters.blog.51cto.com/6516495/1532939

linux shell 实用小实例,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。