Linux shell应用2-自动下载文件

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/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# run-parts01 * * * * root run-parts /etc/cron.hourly02 4 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 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 filesftp -n -i<<! # -n禁止自动登录,-i取消交互式 open ftp.hrbeu.edu.cnuser 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.txtsed -n ‘s/[0-9]/{2/}//;s/ //p’ /home/ftptemp4.txt >/home/ftptemp5.txtIFS=$’/n’ #默认的for是按照空格来分的,电影名中可能有空格所以定义字段分隔符为/nfor var in `cat /home/ftptemp5.txt`doecho "the file name is: $var"lftp <<!open ftp.hrbeu.edu.cnuser anonymous 11 cd 电影cd RMVB电影pget -n 10 "$var" -o /usr/movie/"$var" #下载,注意一定要加双引号,为了防止文件名有空格! #表示下一行是shell脚本done#rm -rf ftptemp1.txt#rm -rf ftptemp2.txtrm -rf ftptemp3.txtrm -rf ftptemp4.txt#rm -rf ftptemp5.txt

自动下载:

[root@localhost home]# crontab -ecrontab: installing new crontab

[root@localhost home]# crontab -u root -l49 23,11 * * * /home/shell/ls.sh40 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自动下载就介绍到这里了。

何不去远方!昆明呀——赶一个花海;

Linux shell应用2-自动下载文件

相关文章:

你感兴趣的文章:

标签云: