Printf ... some suggestions on using correctly?

Hi all.
Probably a silly question... :frowning:
This is my script.

$ cat ./QuoteGrabber.sh
#!/usr/bin/bash
a=$(./DDECmd.exe request -s TOS -t ASK  -i /ESM1)
b=$(./DDECmd.exe request -s TOS -t BID  -i /ESM1)
c=$(./DDECmd.exe request -s TOS -t VOLUME  -i /ESM1)
d=$(./DDECmd.exe request -s TOS -t IMPL_VOL  -i /ESM1)
echo ASK=$a
echo BID=$b
echo VOLUME=$c
echo IMPL_VOL=$d
echo "--------------"
echo $a $b $c $d

This is the output.

$ ./QuoteGrabber.sh
ASK=1306,75
BID=1306,50
VOLUME=1,023,435
IMPL_VOL=N/A
--------------
 N/A23,435

The last line is not what I expect.:frowning:
Can someone help me to make a formattated output with 4 values in printf?
man printf does' t help me so much.. :frowning:
Thanks

I ran it and it worked as expected displaying all fields with spaces in between them.

Are you displaying the entire script?

Thanks for the reply.
This is the whole script.

$ cat ./QuoteGrabber.sh
#!/usr/bin/bash
symbol='./ESJ1C1290'
a=$(./DDECmd.exe request -s TOS -t ASK  -i $symbol)
b=$(./DDECmd.exe request -s TOS -t BID  -i $symbol)
c=$(./DDECmd.exe request -s TOS -t VOLUME  -i $symbol)
d=$(./DDECmd.exe request -s TOS -t OPEN_INT  -i $symbol)
e=$(./DDECmd.exe request -s TOS -t IMPL_VOL  -i $symbol)
f=$(./DDECmd.exe request -s TOS -t SYMBOL  -i $symbol)
echo ASK=$a
echo BID=$b
echo VOLUME=$c
echo OPEN_INT=$d
echo IMPL_VOL=$e
echo SYMBOL=$symbol
echo "--------------"
echo $a $b $c $d
echo "****************"
printf "%10s %5s %25s %s\n" $a $b $c $d
printf "BID=%10s\t ASK=%10s\t VOLUME=%10s\t IMPL_VOL=%10s\t" $a $b $c $d

this is the whole output (as you can see I have also some problems in using printf )

$  ./QuoteGrabber.sh
ASK=16,50
BID=16,00
VOLUME=99
OPEN_INT=3,440
IMPL_VOL=N/A
SYMBOL=./ESJ1C1290
--------------
 3,440
****************
 3,440                 99
BID=     IMPL_VOL=    3,440

If the data you're working with uses windows style line-endings, \r\n, the \n is lost during shell parsing (field splitting, and in the terminating case command substitution as well) but the \r would remain. A terminating \r in the values stored in $a, $b, $c, or $d would yield the results you're seeing.

To illustrate:

$ printf 'Carriage -> \r <- Return\n'
 <- Return>

It looks like 'Carriage -> ' was never printed, but it was (the ">" after 'Return' is from the arrow that follows 'Carriage'). It's just seems that way due to the how the terminal interprets the \r -- the cursor returns to column 1 of the same line and prior output on that line is overwritten with new output until the next newline moves the cursor to the next line. While some text may be clobbered on the screen, if the output were redirected to a file, inspecting it with a tool such as od(1) would show that all the data is indeed there. The \r is only a problem with regard to displaying/printing on a terminal (and possibly some printers).

To confirm, you could pipe the output of the script into od -c and look for carriage returns, \r. You could also try piping the output through tr -d \\r to see if that fixes the problem.

Regards,
Alister

Thanks alister!
Your solution solve brillantly the problem. :wink: