Chinaunix

标题: 一个很不错的ftp脚本,但是我不会改哦 [打印本页]

作者: liuxuexin_xxlhz    时间: 2004-12-27 17:32
标题: 一个很不错的ftp脚本,但是我不会改哦
  1. #!/bin/sh
  2. # ftpget.sh



  3. SPSC_SERVER=192.168.0.1
  4. FTP_USERNAME=11
  5. FTP_PASSWORD=11
  6. FILE_PRENAME=mobile

  7. Log ()
  8. {
  9.         echo [`date '+%Y%m%d %H:%M:%S'` $$] $*
  10. }

  11. if [ "-?" = "$1" ]
  12. then echo usage: ftpgetsample.sh [-d dest_path]
  13.         echo         dest_path: The local path in which files are stored, the recv path is default.
  14.         echo    A sample: ftpget.sh -d /chroot/gateway
  15. else
  16.         exec>>/chroot/gateway/log/recv`date '+%Y%m%d'`.log 2>&1
  17.         if [ "-d" = "$1" ]
  18.                 then DEST_PATH=$2
  19.                 else DEST_PATH=/chroot/gateway/
  20.         fi

  21.         cd $DEST_PATH
  22.         Log "Begin to get filelist..."
  23.        
  24.         # You can NOT use mget and mdelete commands of ftp to handle this issue,
  25.         # because when you begin to execute the mdelete command,
  26.         # it's possible that a new file have been written into the disk,
  27.         # just before the mdelete command start and after mget command had been finished.
  28.         # as a result, firstly, you should get the file list,
  29.         # secondly, get and delete each file in the list
  30.        
  31.         # Step 1: Get the file list from FTP server
  32.         ftp -i -n $SPSC_SERVER <<MACRO_END1
  33.           user $FTP_USERNAME $FTP_PASSWORD
  34.           bin
  35.           ls *$FILE_PRENAME.*  filelist
  36.           bye
  37. MACRO_END1

  38.         if [ ! -s filelist ];then
  39.            Log "No file received......"
  40.            rm -f filelist
  41.            exit
  42.         fi
  43.         # Step 2: Get and delete each file in circle.
  44.         # "cut -b56-" means get the filename from 56th bytes in each line in filelist file,
  45.         # you can adjust the number by yourself after read the filelist file.
  46.         # In our testing environment of Turbo Linux and Red Hat Linux, the number is set to 56
  47.         # In our testing environment of Solaris and AIX, the number is set to 1
  48.         Log "transfering start..."
  49.         for _FILENAME in `more filelist|cut -b56-` ; do
  50.                 ftp -i -n $SPSC_SERVER <<MACRO_END2
  51.                 user $FTP_USERNAME $FTP_PASSWORD
  52.                 bin
  53.                 get $_FILENAME $_FILENAME.tmp
  54.                 delete $_FILENAME
  55.                 bye
  56. MACRO_END2
  57.         mv $_FILENAME.tmp $_FILENAME
  58.         Log "get $_FILENAME ok"  
  59.         gunzip $_FILENAME
  60.         Log "gunzip $_FILENAME ok"
  61.         . /chroot/gateway/bin/subsync.sh
  62.         done
  63.         rm -f filelist
  64.        
  65.         Log "transfering completed."
  66.        
  67.         # Step 3: Handle files based on your business logic
  68.         # The following handle program should be written by yourself !!!
  69.         #for _FILENAME in `more filelist|cut -b56-` ; do
  70.         #        java HandleUnsubRecord ......
  71.         #done
  72. fi
复制代码

我想从服务器上把文件取下来,然后将文件名写到一个.txt文件中,为了下次再取的时候可以比较一下服务器上的文件我是否取过了,同时在我取完就不删除服务器上的文件了,有没有熟悉方面的人指点我一下,我是shell新人。
1,取来的文件名写到.txt文件中
2,比较服务器上的文件和我.txt中的文件
3,取消删除操作(这个我还会一点,呵呵)
作者: ZealeS    时间: 2004-12-27 23:12
标题: 一个很不错的ftp脚本,但是我不会改哦
用ftp执行
ls *.txt nowfile
就会把ftp的目录下的txt文件名打印到本地目录下的nowfile文件。
作者: un_xxx    时间: 2004-12-28 11:00
标题: 一个很不错的ftp脚本,但是我不会改哦
原帖由 "liuxuexin_xxlhz" 发表:
#!/bin/sh
# ftpget.sh



SPSC_SERVER=192.168.0.1
FTP_USERNAME=11
FTP_PASSWORD=11
FILE_PRENAME=mobile

Log ()
{
   echo [`date '+%Y%m%d %H:%M:%S'` $$] $*
}

if [ "-?" = "$1" ]
then echo usage: ftpgetsample.sh [-d dest_path]
   echo    dest_path: The local path in which files are stored, the recv path is default.
   echo    A sample: ftpget.sh -d /chroot/gateway
else
   exec>>/chroot/gateway/log/recv`date '+%Y%m%d'`.log 2>&1
   if [ "-d" = "$1" ]
      then DEST_PATH=$2
      else DEST_PATH=/chroot/gateway/
   fi

   cd $DEST_PATH
   Log "Begin to get filelist..."
   
   # You can NOT use mget and mdelete commands of ftp to handle this issue,
   # because when you begin to execute the mdelete command,
   # it's possible that a new file have been written into the disk,
   # just before the mdelete command start and after mget command had been finished.
   # as a result, firstly, you should get the file list,
   # secondly, get and delete each file in the list
   
   # Step 1: Get the file list from FTP server
        ftp -i -n $SPSC_SERVER <<MACRO_END1
          user $FTP_USERNAME $FTP_PASSWORD
          bin
  1. ls *$FILE_PRENAME.*  filelist.$$.txt #改成txt文件
复制代码
原帖由 "liuxuexin_xxlhz" 发表:
           bye
MACRO_END1

   if [ ! -s filelist ];then
      Log "No file received......"
      rm -f filelist
      exit
   fi
  1. comm -1 filelist.$$.txt filelist.txt  #比较新文件filelist.$$.txt与老文件filelist.txt
复制代码
原帖由 "liuxuexin_xxlhz" 发表:
#   # Step 2: Get and delete each file in circle.
   # "cut -b56-" means get the filename from 56th bytes in each line in filelist file,
   # you can adjust the number by yourself after read the filelist file.
   # In our testing environment of Turbo Linux and Red Hat Linux, the number is set to 56
   # In our testing environment of Solaris and AIX, the number is set to 1
   Log "transfering start..."
   for _FILENAME in `more filelist|cut -b56-` ; do
                ftp -i -n $SPSC_SERVER <<MACRO_END2
                user $FTP_USERNAME $FTP_PASSWORD
                bin
                get $_FILENAME $_FILENAME.tmp
  1. #           delete $_FILENAME  不要删除文件
复制代码
原帖由 "liuxuexin_xxlhz" 发表:
                bye
MACRO_END2
   mv $_FILENAME.tmp $_FILENAME
   Log "get $_FILENAME ok"
   gunzip $_FILENAME
   Log "gunzip $_FILENAME ok"
   . /chroot/gateway/bin/subsync.sh
   done
   rm -f filelist
   
   Log "transfering completed."
  1. mv filelist.$$.txt filelist.txt
复制代码
原帖由 "liuxuexin_xxlhz" 发表:
# Step 3: Handle files based on your business logic
   # The following handle program should be written by yourself !!!
   #for _FILENAME in `more filelist|cut -b56-` ; do
   #   java HandleUnsubRecord ......
   #done
fi  

作者: liuxuexin_xxlhz    时间: 2004-12-28 13:54
标题: 一个很不错的ftp脚本,但是我不会改哦
怎么样给comm -1 filelist.$$.txt filelist.txt  #比较新文件filelist.$$.txt与老文件filelist.txt加一个if语句来判断我的filelist.txt中如果有这个文件了,就不再get了
作者: liuxuexin_xxlhz    时间: 2004-12-28 14:06
标题: 一个很不错的ftp脚本,但是我不会改哦
question:
1,
使用comm的前提是,两个文件必须是经过排序过的
基本格式:
comm -1|-2|-3 file1 file2
-1:不显示在file1中存在的行
-2:不显示在file2中存在的行
-3:不显示在file1和file2中都存在的行
例如不显示file1,file2都有的行
comm -3 file1 file2

请问comm有没有返回值,我想加一个if来判断下面是否执行get
2,
mv filelist.$$.txt filelist.txt

如果这样的话我每次get下来的文件内容就重新作为filelist.txt的内容了,能不能达到filelist.txt中的内容一步一步的增多,作为前面多次get的内容,以便我来比较服务器上的文件我曾经是否取过了
作者: un_xxx    时间: 2004-12-28 16:08
标题: 一个很不错的ftp脚本,但是我不会改哦
  1. diff filelist.$$.txt filelist.txt |grep "<" |awk '{print $2}' >file to get
  2. rm -f filelist.$$.txt
  3. # # Step 2: Get and delete each file in circle.
  4. # "cut -b56-" means get the filename from 56th bytes in each line in filelist file,
  5. # you can adjust the number by yourself after read the filelist file.
  6. # In our testing environment of Turbo Linux and Red Hat Linux, the number is set to 56
  7. # In our testing environment of Solaris and AIX, the number is set to 1
  8. Log "transfering start..."
  9. for _FILENAME in `more filetoget|cut -b56-` ; do
  10. ftp -i -n $SPSC_SERVER <<MACRO_END2
  11. user $FTP_USERNAME $FTP_PASSWORD
  12. bin
  13. get $_FILENAME $_FILENAME.tmp
  14. #           delete $_FILENAME  不要删除文件
  15. bye
  16. MACRO_END2
  17. mv $_FILENAME.tmp $_FILENAME
  18. Log "get $_FILENAME ok"
  19. gunzip $_FILENAME
  20. Log "gunzip $_FILENAME ok"
  21. . /chroot/gateway/bin/subsync.sh
  22. done
  23. cat filetoget >>filelist.txt
  24. rm -f filetoget

  25. Log "transfering completed."
  26. # Step 3: Handle files based on your business logic
  27. # The following handle program should be written by yourself !!!
  28. #for _FILENAME in `more filelist|cut -b56-` ; do
  29. # java HandleUnsubRecord ......
  30. #done
  31. fi
复制代码

作者: un_xxx    时间: 2004-12-28 16:13
标题: 一个很不错的ftp脚本,但是我不会改哦
从filelist.txt比较开始的前边的都没有修改直接粘贴上就可以了。
作者: liuxuexin_xxlhz    时间: 2004-12-28 16:58
标题: 一个很不错的ftp脚本,但是我不会改哦
能不能解释一下
diff filelist.$$.txt filelist.txt |grep "<" |awk '{print $2}' >file to get
中的 awk '{print $2}' ,当我执行完sh ftpget.sh
之后我的filelist.txt 文件中都是
-rw-rw-rw-
-rw-rw-rw-
-rw-rw-rw-
-rw-rw-rw-

我弄了半天这个diff,可就是区分不出不同的文件来。
我理解你的意思是用diff之后把filelist.$$.txt和 filelist.txt中的内容进行比较之后,选出”<“的文件重定向到 filetoget,但是定向的结果有问题,我把$2换成$3之后,我的filelist.txt文件的内容就是
1
1
1
1

真的不知道是为什么了,我不会用awk,请指教一下了
作者: un_xxx    时间: 2004-12-29 09:34
标题: 一个很不错的ftp脚本,但是我不会改哦
不要意思,我当时没有考虑filelist的内容,当在FTP的时候生成的filelist.$$.txt的格式如下:
  1. -rw-r--r--    1 0        0            1538 Sep 29 08:12 GG.jpg
  2. -rw-r--r--    1 0        0           26307 Dec 22 02:38 Img223132522.jpg
  3. drwxr-xr-x    3 0        0            4096 Dec 22 02:41 Projects
  4. -rw-r--r--    1 0        0            1617 Sep 07 07:06 Untitled1.CRASHED
  5. -rw-r--r--    1 0        0            1264 Jul 31 18:42 anaconda-ks.cfg
  6. -rw-r--r--    1 0        0           11919 Nov 24 00:46 asp2php-gtk-0.76.2-5.i386.rpm
  7. -rw-r--r--    1 0        0           28178 Nov 24 00:46 authconfig-gtk-4.3.4-1.i386.rpm
  8. -rw-r--r--    1 0        0           16948 Nov 12 05:31 autorun
复制代码

不好意思,我再修改下。首先提取filelist.$$.txt只提取最后一项文件名
  1. awk '{print $9}' filelist.$$.txt >filelist.name.txt
复制代码

需要说明的是filelist.txt必须也是只包含文件名的文件,这样有利于diff进行比较。
  1. diff filelist.name.txt filelist.txt |grep "<" |awk '{print $2}' >filetoget
复制代码

注意把产生的临时文件(filelist.name.txt和filelist.$$.txt)删除这样就把filelist.name.txt文件中独有的文件名字复制到filetoget这个文件中。这样我想应该能行了吧,嘿嘿
作者: liuxuexin_xxlhz    时间: 2004-12-30 09:16
标题: 一个很不错的ftp脚本,但是我不会改哦
不好意思,昨天病了,今天继续研究这个东东!
作者: liuxuexin_xxlhz    时间: 2004-12-30 09:23
标题: 一个很不错的ftp脚本,但是我不会改哦
我将
  1. diff filelist.name.txt filelist.txt |grep "<" |awk '{print $2}' >filetoget
复制代码

改写成
  1. diff filelist.name.txt filelist.txt |grep "<" |awk '{print $10}' >filetoget
复制代码
也能达到你说的效果
关键是
  1. for _FILENAME in `more filetoget|cut -b56-` ; do
  2.                 ftp -i -n $SPSC_SERVER <<MACRO_END2
  3.                 user $FTP_USERNAME $FTP_PASSWORD
  4.                 bin
  5.                 get $_FILENAME $_FILENAME.tmp
  6.                 #delete $_FILENAME
  7.                 bye
复制代码
里面的”filetoget“他的内容只是文件名,我就get不下来文件了,在没有修改之前是”filelist.$$.txt  “,而整个ftp程序第一步就是在服务器上获得所要取得的文件
  1. ftp -i -n $SPSC_SERVER <<MACRO_END1
  2.           user $FTP_USERNAME $FTP_PASSWORD
  3.           bin
  4.           ls *$FILE_PRENAME.*  filelist.$$.txt  
  5.           bye  
复制代码

这个问题怎么解决呢??
作者: hawkli    时间: 2004-12-30 09:29
标题: 一个很不错的ftp脚本,但是我不会改哦
顶楼的脚本还不错,就是太长了,看得晕。
作者: liuxuexin_xxlhz    时间: 2004-12-30 09:49
标题: 一个很不错的ftp脚本,但是我不会改哦
呵呵,我也是从晕到不晕的。不过现在又有些晕了,不知道怎么搞定他了。
作者: liuxuexin_xxlhz    时间: 2004-12-30 10:20
标题: 一个很不错的ftp脚本,但是我不会改哦
十三问--第九问中的
在使用 positional parameter 的時候,我們要注意一些陷阱哦:
* $10 不是替換第 10 個參數,而是替換第一個參數($1)然後再補一個 0 於其後﹗
也就是,my.sh one two three four five six seven eigth nine ten 這樣的 command line ,
my.sh 裡的 $10 不是 ten 而是 one0 哦... 小心小心﹗

那我上面用$10,filetoget 里面就是一些文件名呢
[/quote]
作者: liuxuexin_xxlhz    时间: 2004-12-30 11:34
标题: 一个很不错的ftp脚本,但是我不会改哦
我将filetoget改成在filelist.$$.txt就能get下来文件了
  1. for _FILENAME in `more filelist.$$.txt|cut -b56-` ; do
  2.                 ftp -i -n $SPSC_SERVER <<MACRO_END2
  3.                 user $FTP_USERNAME $FTP_PASSWORD
  4.                 bin
  5.                 get $_FILENAME $_FILENAME.tmp
  6.                 #delete $_FILENAME
  7.                 bye
复制代码

我想:读出filetoget中的文件,然后再
   
  1. ftp -i -n $SPSC_SERVER <<MACRO_END1
  2.           user $FTP_USERNAME $FTP_PASSWORD
  3.           bin
  4.           ls  从filetoget中读的文件名    filelist.really_want_to_get  
  5.           bye  
复制代码

然后我再从filelist.really_want_to_get  中get就好拉。
  1. for _FILENAME in `more filelist.really_want_to_get  |cut -b56-` ; do
  2.                 ftp -i -n $SPSC_SERVER <<MACRO_END2
  3.                 user $FTP_USERNAME $FTP_PASSWORD
  4.                 bin
  5.                 get $_FILENAME $_FILENAME.tmp
  6.                 #delete $_FILENAME
  7.                 bye
复制代码

怎么从文件filetoget中读出文件名,然后在用ls指令放到filelist.really_want_to_get  中呢,不知道我的思路行不行?
作者: un_xxx    时间: 2004-12-30 12:30
标题: 一个很不错的ftp脚本,但是我不会改哦
[quote]原帖由 "liuxuexin_xxlhz"]怎么从文件filetoget中读出文件名,然后在用ls指令放到filelist.really_want_to_get  中呢,不知道我的思路行不行?[/quote 发表:
我看你的话有点晕,你是哪个实现了,哪个是想实现而没实现,不好意思,没看明白。
作者: liuxuexin_xxlhz    时间: 2004-12-30 15:07
标题: 一个很不错的ftp脚本,但是我不会改哦
不好意思,是我自己弄错了,你的说法就是正确的。不过在我的程序中有一个cut56这个参数,导致我从服务器上get不下来东东。我把整个修改好的文件贴上来,大家参考!
  1. #!/bin/sh
  2. # ftpget.sh



  3. SPSC_SERVER=192.168.0.1
  4. FTP_USERNAME=11
  5. FTP_PASSWORD=11
  6. FILE_PRENAME=11

  7. Log ()
  8. {
  9.         echo [`date '+%Y%m%d %H:%M:%S'` $$] $*
  10. }

  11. if [ "-?" = "$1" ]
  12. then echo usage: ftpgetsample.sh [-d dest_path]
  13.         echo         dest_path: The local path in which files are stored, the recv path is default.
  14.         echo    A sample: ftpget.sh -d /chroot
  15.         else
  16.         exec>>/chroot/log/recv`date '+%Y%m%d'`.log 2>&1
  17.         if [ "-d" = "$1" ]
  18.                 then DEST_PATH=$2
  19.                 else DEST_PATH=/chroot
  20.                 fi

  21.         cd $DEST_PATH
  22.         Log "Begin to get filelist..."
  23.        
  24.         # Step 1: Get the file list from FTP server
  25.         ftp -i -n $SPSC_SERVER <<MACRO_END1
  26.           user $FTP_USERNAME $FTP_PASSWORD
  27.           bin
  28.           ls *$FILE_PRENAME.*  filelist.$$.txt  
  29.           bye  
  30. MACRO_END1
  31.        
  32.         awk '{print $  9}' filelist.$$.txt >filelist.name.txt
  33.         diff filelist.name.txt filelist.txt |grep "<" |awk '{print $2}' > filetoget
  34.           
  35.         if [ ! -s filetoget ];then
  36.            Log "No file received......"
  37.            rm -f filelist.$$.txt
  38.            rm -f filelist.name.txt
  39.            rm -f filetoget                              
  40.            exit
  41.         fi

  42.         # Step 2: Get and delete each file in circle.
  43.         Log "transfering start..."
  44.         for _FILENAME in `cat filetoget` ; do
  45.                 ftp -i -n $SPSC_SERVER <<MACRO_END2
  46.                 user $FTP_USERNAME $FTP_PASSWORD
  47.                 bin
  48.                 get $_FILENAME $_FILENAME.tmp
  49.                        #delete $_FILENAME
  50.                 bye
  51. MACRO_END2
  52.         mv $_FILENAME.tmp $_FILENAME
  53.         Log "get $_FILENAME ok"  
  54.         gunzip $_FILENAME
  55.         Log "gunzip $_FILENAME ok"
  56.         done
  57.        
  58.         log "transfering completed."
  59.         cat filetoget >> filelist.txt            
  60.         rm -f filelist.name.txt
  61.         rm -f filetoget
  62.         rm -f filelist.$$.txt
  63.        
  64. fi
复制代码

当然了,这里还有几个问题不理解的

  1. if [ ! -s filetoget ];then
  2.            Log "No file received......"
  3.            rm -f filelist.$$.txt
  4.            rm -f filelist.name.txt
  5.            rm -f filetoget                              
  6.            exit
  7.         fi               
复制代码

这里的 -s filetoget,参数-s是什么意思呢?
还有
  1. for _FILENAME in `cat filetoget` ; do
  2.                 ftp -i -n $SPSC_SERVER <<MACRO_END2
  3.                 user $FTP_USERNAME $FTP_PASSWORD
  4.                 bin
  5.                 get $_FILENAME $_FILENAME.tmp
  6.                        #delete $_FILENAME
  7.                 bye
  8. MACRO_END2
复制代码
中的MACRO_END2是干吗用的??
作者: liuxuexin_xxlhz    时间: 2004-12-30 15:09
标题: 一个很不错的ftp脚本,但是我不会改哦
在此十分感谢un_xxx!!
作者: bandt    时间: 2004-12-30 15:23
标题: 一个很不错的ftp脚本,但是我不会改哦
[quote]原帖由 "liuxuexin_xxlhz"]的MACRO_END2是干吗用的??[/quote 发表:


让俺这个菜鸟来回答:1.-s用来判断文件是否为空;2.<<输入的开始符号和结束符号,具体用什么可以自定义:)置于<<的用法,可以查找相关内容。
作者: bandt    时间: 2004-12-30 15:33
标题: 一个很不错的ftp脚本,但是我不会改哦
这句exec>>/chroot/log/recv`date '+%Y%m%d'`.log 2>&1 里面的exec>>是什么意思,请大家指教!
意思是改变输出终端为/chroot/log/recv,然后把该日的日期写入/chrot/log/recv,错误也写入。是吗?
作者: un_xxx    时间: 2004-12-30 15:44
标题: 一个很不错的ftp脚本,但是我不会改哦
这里的-s是测试文件是否为空。
PS:建议学习下《Linux and Unix Shell programming》
作者: liuxuexin_xxlhz    时间: 2004-12-30 15:46
标题: 一个很不错的ftp脚本,但是我不会改哦
请看至顶的十三问中的第六问http://bbs.chinaunix.net/forum/viewtopic.php?p=1583329#1583329




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2