1

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

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

Code snippet

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

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

Expected result

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

Remote file

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

Local file

2037 Aug 13  2015 README.ftp

Thanks in advance.

Gabe
  • 226
  • 3
  • 13
  • Take a look at `help printf`. – Cyrus Aug 24 '19 at 22:56
  • 3
    `man 1 printf` is probably more informative. The critical thing is that `printf`'s first argument is a *format string* that tells it how to format what it prints; the remaining arguments are the things to print. Also, put double-quotes around variable references (e.g. `"$inpath/$infile.gz"` instead of just `$inpath/$infile.gz`) to prevent unexpected parsing. – Gordon Davisson Aug 24 '19 at 23:08
  • 3
    Can you show us the output of `printf '%s %s %s\n' $inpath/$infile.gz $infilesize $infiledate` and then show us the output you want? – Mark Plotnick Aug 24 '19 at 23:08
  • 1
    *$infilesize* perhaps requires **%d**. – 0andriy Aug 25 '19 at 04:01
  • @GordonDavisson thanks for the `man 1 printf` reference was informative. And for the double quotes handling advice, have tidied up variable names. – Gabe Aug 31 '19 at 03:52

1 Answers1

0

Solution

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"

Result

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 clear and simple explanation of argument handing.

GNU Manual Table of Output Conversions for format summary

Gabe
  • 226
  • 3
  • 13
  • You should probably still [quote your variables.](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Aug 31 '19 at 04:39