mulegame 发表于 2014-03-18 03:22

inotify while...do循环里面不可以在嵌套吗?

安装完inotify之后,想实现三个目标:

1。监视upload文件夹,如果有文件,就解压缩到extractfiles目录
2。监视extractfiles目录,找出新的文件,然后将文件打包成 1.rar,然后移动到wwwroot目录
3。监视wwwroot目录,如果有文件,就推送到其他节点

没想到刚开始就卡住了...#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

src=/otp/wwwroot/upload/
des=/otp/wwwroot/
# 从服务器(客户端)
ip=192.168.1.3

# 解压缩指定文件到指定目录(覆盖方式)
function ExtractFiles()
{
      unrar x -o+ -y /otp/wwwroot/upload/update.rar /otp/wwwroot/extractfiles/
}

# 验证已上传压缩文件是否完整
function CheckFiles()
{
      unrar t update.rar

if [ $? -eq 0 ]; then
      echo "CheckFiles:update.rar OK"
      ExtractFiles
else
      echo "CheckFiles:update.rar Error!!!"
fi
}



/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} | while read file
do
        for i in $ip
                do
                # 文件解压缩
                CheckFiles
                # 同步操作
                done
done

mulegame 发表于 2014-03-18 03:30

占楼编辑:

另外,本人刚开始学shell,花了2个晚上的时间写了一个脚本。望各位大神略微指点一二。放出一键安装脚本
我已经尽量用中文标注了,但是我那蹩脚的English实在拿不出手,有些来自Google翻译:lol

脚本名称: inotify+rsync+unrar
最后编辑时间: 2014/03/18   -Mulegame
备注:bbs.chinaunix.net#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

cur_dir=$(pwd)
# 验证当前用户是否为 root 权限
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install."
    exit 1
fi

yum -y update

# 安装相关依赖
for packages in patch make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap rsync;
do yum -y install $packages; done

# 检查系统内核版本
function Checksystemversion()
{
      echo "Check System Version = 2.6.32-431.5.1.el6.x86_64 ...."
      R=$(uname -r)
      if [[ $R = "2.6.32-431.5.1.el6.x86_64" ]]; then
                echo "Check System Version Successful."
      else
                echo "Check System Version Error.The End Of Installation Process!!!"
                exit 1
      fi
}

# 检查系统内核是否支持inotify
function Checksysteminotify()
{
      echo "Check System Support inotify ...."
      R=$(ls -l /proc/sys/fs/inotify)
      if [ $? -eq 0 ]; then
                echo "Check System Support inotify successful."
      else
                echo "Check System Support inotify Error. Please Check System Kernel Support inotify!!!"
                exit 1
      fi
}

# 安装 inotify
function Installinotify()
{
        cd $cur_dir
       
        # inotify Install Start

        if [ -s inotify-tools-3.13.tar.gz ]; then
          echo "inotify-tools-3.13.tar.gz "
        else
          echo "Error: inotify-tools-3.13.tar.gz not found!!!download now......"
          wget -c http://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
        fi

      # Expansion inotify-tools-3.13.tar.gz
      tar zxvf inotify-tools-3.13.tar.gz
      cd inotify-tools-3.13

      # configure &&make && make install
      ./configure
      make && make install

      # Check inotify Install
      #inotifywait -h
      #if [ $? -eq 0 ]; then
         #       echo "inotify Install is successful."
      #else
         #       echo "Error: inotify Install failed!"
        #fi


        if [ -s /usr/local/bin/inotifywait ] && [ -s /usr/local/bin/inotifywatch ]; then
          echo "inotify: inotify Install is successful."
          else
          echo "Error: inotify Install failed!"
        fi
}

# 安装 unrar
function Installunrar()
{
        cd $cur_dir
      # unrar Install Start
      echo "#### Download rarlinux-x64-5.0.1.tar.gz ->From http://www.rarlab.com/rar/rarlinux-x64-5.0.1.tar.gz ###"
      wget http://www.rarlab.com/rar/rarlinux-x64-5.0.1.tar.gz

      # Expansion rarlinux-x64-5.0.1.tar.gz
      tar zxvf rarlinux-x64-5.0.1.tar.gz
      cd rar

      # configure &&make && make install
      make && make install

      # Check unrar Install
      unrar
      if [ $? -eq 0 ]; then
                echo "unrar Install is successful."
      else
                echo "unrar Install feiled!"
        fi
}



Checksystemversion 2>&1 | tee -a /root/test_install.log
Checksysteminotify 2>&1 | tee -a /root/test_install.log
Installinotify 2>&1 | tee -a /root/test_install.log
Installunrar 2>&1 | tee -a /root/test_install.log
页: [1]
查看完整版本: inotify while...do循环里面不可以在嵌套吗?