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/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自动下载就介绍到这里了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值