Sharing and descriptions of circulating FTP scripts

Source: Internet
Author: User
Tags ftp file

This script is written by myself. As a shell beginner, you can learn it together.
1st the first line of the behavior script tells the system which shell to choose when running the script. If there is no first line, the system selects the default shell.
1 #! /Bin/sh
Line 2-9 verifies the parameters. If the number of parameters is not 1, an error is returned.
2 # Number of verification parameters
3 if [$ #-ne 1]
4 then
5 echo "Param error! "
6 echo "Usage: $0 date"
7 echo "Example: $0 20110808"
8 exit 1
9 fi
10
11 # scheduling date
Row 12-14 defines Variables
12 etl_date = $1
13
14 OK = 'echo $ {etl_date} | grep-w "20 [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] "| wc-l'
Line 15-22 verifies the parameter format. If the format is not 20 xxxxxx, an error is returned.
15 # verify the parameter format
16 if [$ {OK}-eq 0]
17 then
18 echo "Param error! "
19 echo "Usage: $0 date"
20 echo "Example: $0 20110808"
21 exit 1
22 fi
23
Line 24-26 defines Variables
24 sub1 = 'echo $ {etl_date} | cut-c 1-4'
25 sub2 = 'echo $ {etl_date} | cut-c 5-6'
26 sub3 = 'echo $ {etl_date} | cut-c 7-8'
27
Row 3 reads all system names from the configuration file ftp. cfg.
28 # retrieve all systems
29 system_all = 'awk-F = '{if ($1 ~ /System_name/) print $2} '/home/edw/cjb/bin/ftp/ftp_get.cfg'
30
31 # obtain the number of systems
Row 3 calculates the number of system names that have just been read. The number is system_num.
32 system_num = 'echo $ {system_all} | awk-F + '{print NF }''
33
34 echo "there are currently [$ {system_all}] $ {system_num} systems"
35
Row 3 shows that num is used to increase from 1 to system_num to execute a loop. If num is not used, shift the variable.
36 # auxiliary parameters for cyclic execution
37 num = 1
38
39 # execute the $ {system_name} Loop Based on the number of systems $ {system_num}
40 while [$ {num}-le $ {system_num}]
41 do
42 sys_name = 'echo $ {system_all} | awk-F + '{print $' $ num '}''
43 echo "the $ {num} systems are $ {sys_name }"
Line 44-48 reads the ftp IP address, user name, password, and remote/local path from the configuration file ftp_get.cfg.
44 remote_ip = 'awk-F = '{if ($1 ~ /'$ {Sys_name}' _ ADD/) print $2} '/home/edw/cjb/bin/ftp/ftp_get.cfg'
45 user_name = 'awk-F = '{if ($1 ~ /'$ {Sys_name}' _ USR/) print $2} '/home/edw/cjb/bin/ftp/ftp_get.cfg'
46 pass_word = 'awk-F = '{if ($1 ~ /'$ {Sys_name}' _ PWD/) print $2} '/home/edw/cjb/bin/ftp/ftp_get.cfg'
47 rmt_dir = 'awk-F = '{if ($1 ~ /'$ {Sys_name}' _ REMOTEDIR/) print $2} '/home/edw/cjb/bin/ftp/ftp_get.cfg'
48 loc_dir = 'awk-F = '{if ($1 ~ /'$ {Sys_name}' _ LOCALDIR/) print $2} '/home/edw/cjb/bin/ftp/ftp_get.cfg'
49
50 # second parameter resolution
Row 51st uses eval to perform secondary parsing on rmt_dir. After reading rmt_dir from ftp_get.cfg, it still contains parameters. The second Parsing is required, and then assigned to remote_dir, which is the same as row 54th.
51 remote_dir = 'eval echo $ {rmt_dir }'
52 echo "remote_dir is $ {remote_dir }"
53 # second parameter resolution
54 local_dir = 'eval echo $ {loc_dir }'
55 echo "local_dir is $ {local_dir }"
56
57 # automatic creation if the local path does not exist
58 [-d $ {local_dir}] | mkdir-p $ {local_dir}
59
60 # Enter the target path
61 cd $ {local_dir}
62
63 # automatic creation if the log file does not exist
64 [-f autorun _ $ {etl_date}. log] | touch autorun _ $ {etl_date}. log
65
66 # insert runtime to log
67 echo "====================== RunTime is ['date + \" % Y-% m -% d % T \ "'] =================================="> autorun _ $ {etl_date }. log
68
69 echo "starting from $ {sys_name} system count ..."
Row 3 reads the ftp file name from $ {sys_name}. cfg and starts the ftp operation.
70 cat/home/edw/cjb/bin/ftp/$ {sys_name}. cfg | grep-v '^ #' | while read LINE
71. do
72 f_name = 'echo $ {LINE} | awk '{print $1 }''
73 # second parameter resolution
74 file_name = 'eval echo $ {f_name }'
75 echo file_name is $ {file_name}
76 # Start ftp
Line 7-84 for the entire ftp Process
77 ftp-n $ {remote_ip} <EOF
78 user $ {user_name }$ {pass_word}
79 cd $ {remote_dir}
80 bin
81 get $ {file_name}
82 close
83 bye
84 EOF
85 done | tee-a autorun _ $ {etl_date}. log
86 echo "Please view the log 'pwd'/autorun _ $ {etl_date}. log"
87
88 # after each execution of a system, num automatically adds 1 to start to fetch the number from the next system
89 num = 'expr $ {num} + 1'
90 done
 
The following is a simple description. If you have any questions, please leave a message and contact me.


Author: "To_Be_Monster_Of_IT"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.