如何使用curl和printf打印ftp文件信息 [英] How to print ftp file information using curl and printf
问题描述
我正在尝试获取文件信息以进行批量下载,并且难以使用管道连接到printf。
I am trying to get file information for batch downloading and having difficulty with pipes to printf.
测试用例
$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp
Last-Modified: Wed, 12 Aug 2015 15:47:26 GMT
Content-Length: 2037
Accept-ranges: bytes
一个简单的grep剪切就可以了文件大小
A simple grep to cut that is working for file size
$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Content-Length | cut -d ' ' -f 2
2037
要剪切的Grep在文件日期和时间上起作用
Grep to cut is working for file date and time
$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Last-Modified | cut -d ' ' -f 3,4,5,6
12 Aug 2015 15:47:26
但是,将grep扩展到cutf无效对于服务器文件的日期和时间,printf是否返回本地系统日期和时间?
However, extending grep to cut to printf is not working for the server file date and time, printf is return the local system date and time?
$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Last-Modified | cut -d ' ' -f 3,4,5,6 | printf '%(%Y-%m-%d %H:%M:%S)T '
本地日期和时间,而非远程日期和时间?
Local date and time, not remote date and time?
2019-08-24 20:32:53
文件大小和日期将在脚本中用于批量下载和自定义日志记录。这些内容...
The file size and date will be used in scripts for batch downloads and custom logging. Something along these lines...
infilesize=$(curl -sI $inpath/$infile | grep Content-Length | cut -d ' ' -f 2)
infiledate=$(curl -sI $inpath/$infile | grep Last-Modified | cut -d ' ' -f 3,4,5,6 | printf '%(%Y-%m-%d %H:%M:%S)T ' )
printf $infilesize 2>&1 | tee -a $logpath/$logfile
printf $infiledate 2>&1 | tee -a $logpath/$logfile
我的第一个障碍是解决printf的管道语法?
My first hurdle is fix the pipe syntax for printf?
对其他管道方法(sed和awk)开放。
Open to other pipe approaches, sed and awk.
相对缺乏bash的经验,所以请欣赏冗长的代码建议和/或优美的代码和说明,并希望学习好的技术。
Relatively inexperienced with bash, so appreciate verbose code suggestions and or elegant code and explanations, wanting to learn good techniques.
预先感谢。
更新
date
子shell方法适用于远程服务器文件的日期和时间。
The date
subshell approach is working for remote server file date and time.
但是,我现在遇到 printf
在单行上输出多个变量的问题。我四处张望,但难以理解printf的参数处理并收到意外的结果。
However, I am now having problems with printf
to output multiple variables on a single line. I have had a look around but having difficult understanding the arguments handling for printf and receiving unexpected results.
printf $infilename $infilesize $infiledate 2>&1 | tee -a $outpath/$logfile.txt
这是一个独立的问题,如何使用printf打印多个变量
This has been asked as a separate question How can I print multiple variables using printf
推荐答案
您甚至不需要 printf
,只需使用中的子外壳date
以显示 cut
日期信息:
You don't even need printf
, just use a subshell from date
to display the cut
date information:
curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Last-Modified | \
date -d "$(cut -d ' ' -f 3,4,5,6)" "+%Y-%m-%d %H:%M:%ST"
结果:
2015-08-12 15:47:26T
这篇关于如何使用curl和printf打印ftp文件信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!