如何使用printf打印多个变量 [英] How can I print multiple variables using printf

查看:816
本文介绍了如何使用printf打印多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让printf在一行上输出多个变量.

I am trying get printf to output multiple variables on a single line.

难以处理printf的参数处理和格式,并收到意外的结果.

Having difficultly with argument handling and formats for printf and receiving unexpected results.

代码段

printf '%s %s %s' $infile $insize $indate 2>&1 | tee -a $logfile
find $infile -printf ' %p %s %CY-%Cm-%Cd %CH:%CM:%.2TS \n' 2>&1 | tee -a $logfile

意外结果
*第一个文件大小未显示,日期和时间不正确(printf)
*第二个文件大小正确,日期和时间不正确(查找-printf)

Unexpected result
* first file size not showing, date and time not correct (printf)
* second file size correct, date and time not correct (find -printf)

ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp 2019-08-25 00:00:00 ./README.ftp 2037 2019-08-25 14:22:26

预期结果

ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp 2037 2015-08-12 15:47:26 ./README.ftp 2037 2019-08-24 20:32:53

远程文件

Last-Modified: Wed, 12 Aug 2015 15:47:26 GMT
Content-Length: 2037

本地文件

2037 Aug 13  2015 README.ftp

谢谢.

推荐答案

解决方案

printf '%s %d %s' "$infile" "$insize" "$indate" 2>&1 | tee -a "$logfile"
find "$infile" -printf ' %p %s %CY-%Cm-%Cd %CH:%CM:%.2TS \n' 2>&1 | tee -a "$logfile"

结果

ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp 2037 2015-08-12 15:47:26 ./README.ftp 2037 2019-08-24 20:32:53

man 1 printf清晰明了的参数处理说明.

man 1 printf clear and simple explanation of argument handing.

GNU 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆