免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3950 | 回复: 1
打印 上一主题 下一主题

[其他] inotify while...do循环里面不可以在嵌套吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-03-18 03:22 |只看该作者 |倒序浏览
安装完inotify之后,想实现三个目标:

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

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

  4. src=/otp/wwwroot/upload/
  5. des=/otp/wwwroot/
  6. # 从服务器(客户端)
  7. ip=192.168.1.3

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

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

  17. if [ $? -eq 0 ]; then
  18.         echo "CheckFiles:update.rar OK"
  19.         ExtractFiles
  20. else
  21.         echo "CheckFiles:update.rar Error!!!"
  22. fi
  23. }



  24. /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} | while read file
  25. do
  26.         for i in $ip
  27.                 do
  28.                 # 文件解压缩
  29.                 CheckFiles
  30.                 # 同步操作
  31.                 done
  32. done
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-03-18 03:30 |只看该作者
占楼编辑:

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

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

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

  10. yum -y update

  11. # 安装相关依赖
  12. 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;
  13. do yum -y install $packages; done

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

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

  38. # 安装 inotify
  39. function Installinotify()
  40. {
  41.         cd $cur_dir
  42.        
  43.         # inotify Install Start

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

  50.         # Expansion inotify-tools-3.13.tar.gz
  51.         tar zxvf inotify-tools-3.13.tar.gz
  52.         cd inotify-tools-3.13

  53.         # configure &&  make && make install
  54.         ./configure
  55.         make && make install

  56.         # Check inotify Install
  57.         #inotifywait -h
  58.         #if [ $? -eq 0 ]; then
  59.          #       echo "inotify Install is successful."
  60.         #else
  61.          #       echo "Error: inotify Install failed!"
  62.         #fi


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

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

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

  79.         # configure &&  make && make install
  80.         make && make install

  81.         # Check unrar Install
  82.         unrar
  83.         if [ $? -eq 0 ]; then
  84.                 echo "unrar Install is successful."
  85.         else
  86.                 echo "unrar Install feiled!"
  87.         fi
  88. }



  89. Checksystemversion 2>&1 | tee -a /root/test_install.log
  90. Checksysteminotify 2>&1 | tee -a /root/test_install.log
  91. Installinotify 2>&1 | tee -a /root/test_install.log
  92. Installunrar 2>&1 | tee -a /root/test_install.log
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP