1. 概述
Linux強大的命令行,能夠完成各種不同的功能。然而,如果只是無休止的輸入命令來完成功能,那麼就太有點費事了。那麼怎麼能夠解決這個問題了? Linux shell腳本給我們提供了答案。通過shell編程可以把命令進行組合,去自動的完成管理與執行任務。而不需要一次又一次的輸入命令。這篇文章中,主要介紹一下,怎麼利用shell腳本去完成自動下載文件的功能。
2. 基本知識
(1) Linux shell編程基礎-包括awk,sed,正則表達式,在前面已經介紹過了。
(2)ftp,lftp
FTP是一個文件傳輸協議,分爲主動傳輸與被動傳輸,而傳送的方式有binary和acsii兩種,get用於下載文件,put用於上傳文件。
FTP is the user interface to the ARPANET standard File Transfer Proto-
col. The program allows a user to transfer files to and from a remote
network site
-n 禁止自動登錄
-i 關閉交互式ftp,默認情況下ftp是一種交互式操作
-n Restrains ftp from attempting ‘‘auto-login’’
-i Turns off interactive prompting during multiple file transfers.
! [command] [args]]
Invoke an interactive shell on the local machine. If there are
arguments, the first is taken to be a command to execute
directly, with the rest of the arguments as its arguments.
! -代表回到shell腳本
get remote-file [local-file]
Retrieve the file remote-file and store it on the local machine.
If the local file name is not specified, it is given the same
name it has on the remote machine, subject to alteration by the
current case, ntrans, and nmap settings. The current settings
for type, form, mode, and structure are used while transferring
the file.
the file.
put local-file [remote-file]
Store a local file on the remote machine. If remote-file is
left unspecified, the local file name is used after processing
according to any ntrans or nmap settings in naming the remote
file. File transfer uses the current settings for type, format,
mode, and structure.
lftp: lftp是linux下命令行的ftp客戶端。
! shell command
Launch shell or shell command.
!ls
To do a directory listing of the local host.
pget [OPTS] rfile [-o lfile]
Gets the specified file using several connections. This can speed up
transfer, but loads the net and server heavily impacting other users.
Use only if you really have to transfer the file ASAP. Options:
-c continue transfer. Requires lfile.lftp-pget-status file.
rom pget:default-n setting)
put [-E] [-a] [-c] [-O base] lfile [-o rfile]
Upload lfile with remote name rfile. If -o omitted, the base name of
lfile is used as remote name. Does not expand wildcards, use mput for
that.
-o <rfile> specifies remote file name (default - basename of lfile)
-c continue, reput
it requires permission to overwrite remote files
-E delete source files after successful transfer (dangerous)
-a use ascii mode (binary is the default)
-O <base> specifies base directory or URL where files should be placed
(3)定期調度腳本
Linux 系統使用cron程序定期運行作業。cron程序在後臺運行。
a. cron的主配置文件是/etc/crontab
[root@localhost home]# cat </etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
前面4行是環境變量,第一行是使用的shell,第二行是可執行的命令。
第三行是定義用戶名,用來發送郵件的。 第四行是設置在執行命令時所使用的主目錄。
crond是cron服務的守護進程。
所有用戶定義的crontab都被保存在/var/spool/cron/username中
創建crontab, crontab -e
crontab -u root -l
可列出當前用戶的cron表格。
crond即cron守護進程每分鐘會檢查/etc/crontab,/var/spool/cron目錄。
b. cron的控制
/etc/cron.allow和/etc/cron.deny用來限制對cron服務的使用,每一行都是用戶。
根用戶都可以使用cron
如果cron.allow存在,那麼cron.allow列出的用戶纔可以使用cron,並且cron.deny被忽略
如果cron.allow不存在,那麼cron.deny中列出的用戶都禁止使用cron服務
cat /var/log/cron 可以查看自定義的命令有沒有運行,包含日誌信息。
c. crontab的語法
minute hour day month dayofweek command
分鐘 小時 日期 月份 星期 執行的命令
*-代表所有有效值
- 指定一個整數範圍
, 隔開一系列的值指定一個列表
/ 指定間隔頻率
3. Linux ftp實現自動下載功能
#!/bin/bash
#using ftp download files
ftp -n -i<<! # -n禁止自動登錄,-i取消交互式
open ftp.hrbeu.edu.cn
user anonymous 11 #用戶名密碼,匿名登錄,密碼可以隨意
cd 電影
cd RMVB電影
hash #下載時顯示進度
ls * /home/ftptemp1.txt #將ls的內容保存到文件中去
!
sed -n '/[Rr][Mm][Vv][Bb]$/w /home/ftptemp2.txt' /home/ftptemp1.txt #過濾出rmvb,RMVB電影
count=`cat /home/ftptemp2.txt|wc -l` #wc計算行數,查看有多少電影可供下載
echo "可供下載的電影數目爲:$count"
sed -n '1p' /home/ftptemp2.txt>/home/ftptemp3.txt #示例下載第1個電影
sed -n '4p' /home/ftptemp2.txt>>/home/ftptemp3.txt #下載第4個電影
#ls裏面有日期,爲了截出電影名
gawk 'BEGIN{FS=":"}{print $NF}' /home/ftptemp3.txt>/home/ftptemp4.txt
sed -n 's/[0-9]/{2/}//;s/ //p' /home/ftptemp4.txt >/home/ftptemp5.txt
IFS=$'/n' #默認的for是按照空格來分的,電影名中可能有空格所以定義字段分隔符爲/n
for var in `cat /home/ftptemp5.txt`
do
echo "the file name is: $var"
lftp <<!
open ftp.hrbeu.edu.cn
user anonymous 11
cd 電影
cd RMVB電影
pget -n 10 "$var" -o /usr/movie/"$var" #下載,注意一定要加雙引號,爲了防止文件名有空格
! #表示下一行是shell腳本
done
#rm -rf ftptemp1.txt
#rm -rf ftptemp2.txt
rm -rf ftptemp3.txt
rm -rf ftptemp4.txt
#rm -rf ftptemp5.txt
自動下載:
[root@localhost home]# crontab -e
crontab: installing new crontab
[root@localhost home]# crontab -u root -l
49 23,11 * * * /home/shell/ls.sh
40 15 * * * /home/ftp.sh
可以看出有兩個命令,第一個是ls.sh命令。時間是23點49分,或者是11點49分運行。
第二個就是ftp.sh自動下載文件命令。
查看日誌:
cat /var/log/cron
這個腳本只是簡單的實現了你想要下載的電影,其中ftptemp1.txt裏面包含了所有的電影名,如果你想要下載哪個,就在程序中寫上行號,行號從1開始。然後利用cron會自動的下載。但是還不夠智能,例如,如果已經下載了某個電影,應該不重複下載,所以必須掃描目錄進行判斷。
最後可以把Linux下載的電影直接通過NFS傳送到Windows下。
關於Linux shell編程實現ftp自動下載就介紹到這裏了。