Bash脚本复制最新文件并将输出发送到电子邮件ID

时间:2013-08-02 15:05:38

标签: linux bash shell

要求:

  1. 检查当前日期/ a / b / DROP和/ a / b / ARCHIVE目录中是否存在* .idx文件的脚本。(DONE)

  2. 如果/ a / b / DROP缺少* .idx文件,则从/ a / b / ARCHIVE复制* .idx文件以获取当前日期。(一天只有1个文件)

  3. 需要将脚本的输出发送到几个邮件ID。

    #!/bin/bash
    # Checking DROP ZIP file for current date
    #***************************************
    filestr=`find /a/b/DROP -name "*.idx" -mtime -1 -print`
          if [ "$filestr" = "" ]; then
            echo "DROP ZIP File missing for `date +%D`"
          else
            echo "DROP ZIP file exists for `date +%D`"
          fi
    #Checking ARCHIVE IDX file for current date
    #******************************************
    filestr=`find /a/b/ARCHIVE -name "*.idx" -mtime -1 -print`
      if [ "$filestr" = "" ]; then
        echo "ARCHIVE IDX File missing for `date +%D`"
      else
        echo "ARCHIVE IDX file exists for `date +%D`"
      fi
    
  4. 有人可以帮助我实现第二个第三要求吗?

1 个答案:

答案 0 :(得分:1)

#!/bin/bash
# req.sh
filestr=`find /a/b/DROP -name "*.idx" -mtime -1 -print`
if [ "$filestr" = "" ]; then
  missing="1"
  echo "DROP ZIP File missing for `date +%D`"
else
  echo "DROP ZIP file exists for `date +%D`"
fi
filestr=`find /a/b/ARCHIVE -name "*.idx" -mtime -1 -print`
if [ "$filestr" = "" ]; then
  echo "ARCHIVE IDX File missing for `date +%D`"
elif [ "$missing" = "1" ];then
  echo "Copying from ARCHIVE `date +%D`"
  cp $filestr /a/b/DROP
else
  echo "ARCHIVE IDX file exists for `date +%D`"
fi

运行脚本
sh req.sh |/usr/sbin/sendmail -r sender@domain.tld recipient1@domain.tld