Re: printf treats arguments of %c not as expected

2011-06-23 Thread Jan Schampera

Hi,


I agree this is not a bug. %c works as described.

However, Mr. Wang may want to read one of Greg's brilliant FAQ entries [1].

In general it's a bit of a pity that printf can do character-number 
conversion, but not (directly) back. But it is like it is and the 
workarounds are not really complicated.



Jan


[1] http://mywiki.wooledge.org/BashFAQ/071

--
Be conservative in what you do, be liberal in what you accept from others.
- jbp, master of the net, in RFC793




Re: printf treats arguments of %c not as expected

2011-06-23 Thread Yunfeng Wang
Thanks Jan, Greg's solution is great!

wyf

2011/6/23 Jan Schampera jan.schamp...@web.de:
 Hi,


 I agree this is not a bug. %c works as described.

 However, Mr. Wang may want to read one of Greg's brilliant FAQ entries [1].

 In general it's a bit of a pity that printf can do character-number
 conversion, but not (directly) back. But it is like it is and the
 workarounds are not really complicated.


 Jan


 [1] http://mywiki.wooledge.org/BashFAQ/071

 --
 Be conservative in what you do, be liberal in what you accept from others.
 - jbp, master of the net, in RFC793





Re: printf treats arguments of %c not as expected

2011-06-23 Thread Jan Schampera

Yunfeng Wang wrote:


Perhaps bash should clarify this issue in its documents such that users like me
would not be misguided again.


Since the reference to printf(3) is misleading sometimes, I made some 
document [1] for Bash's printf only. It's far from perfect, but at least 
 it mentions %s and first character ;-)



Jan


[1] http://wiki.bash-hackers.org/commands/builtin/printf

--
Be conservative in what you do, be liberal in what you accept from others.
- jbp, master of the net, in RFC793




Re: printf treats arguments of %c not as expected

2011-06-23 Thread Greg Wooledge
 2011/6/23 Jan Schampera jan.schamp...@web.de:
  However, Mr. Wang may want to read one of Greg's brilliant FAQ entries [1].
  [1] http://mywiki.wooledge.org/BashFAQ/071

On Thu, Jun 23, 2011 at 04:00:35PM +0800, Yunfeng Wang wrote:
 Thanks Jan, Greg's solution is great!

Just for the record, I didn't write the content of BashFAQ/071.  I don't
know who did; the oldest wiki history entry says redondos but the
wiki's history is also incomplete, since a lot of the FAQ content was
transferred from heiner's older FAQ.  Also, the bottom comment of the
page is signed mjf; I don't know who that is, and I don't know whether
that's the original content author, or someone who was simply pointing
out where the quote trick is documented.

The wiki is very much a group effort, as it should be.



Re: printf treats arguments of %c not as expected

2011-06-22 Thread Jonathan Nieder
Hi,

Yunfeng Wang wrote:

 $ printf %c 65 66 67
 666

 The expected output is ABC, i.e. characters with ASCII code of 65 66 67

I believe the current behavior is correct.  POSIX (XCU.4.printf) sayeth[*]:

11. The argument to the 'c' conversion specifier can be a string
containing zero or more bytes.  If it contains one or more
bytes, the first byte shall be written and any additional bytes
shall be ignored.  If the argument is an empty string, it is
unspecified whether nothing is written or a null byte is written.

I would suggest using something like

perl -e 'print(chr(65), chr(66), chr(67), \n);'

or

for i in 65 66 67
do
eval printf \'\\$(printf %03o $i)\'
done
printf '\n'

for your application.

Back to the bug: I don't see any explanation of printf %c when I run
man bash.  Perhaps your manual is different from mine, but if you,
perhaps it would be possible to suggest a few words to explain this
for future readers.

Thanks and regards,
Jonathan

[*] http://unix.org/2008edition/