- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- printf shell command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:00 PM
тАО02-11-2007 11:00 PM
I just bounced into something strange. Can anyone explain the below ?
------------------------------
..
$ printf "%02d\n" 05
05
$ printf "%02d\n" 06
06
$ printf "%02d\n" 07
07
$ printf "%02d\n" 08
printf: Error converting 08
00
$ printf "%02d\n" 09
printf: Error converting 09
00
$ printf "%02d\n" 10
10
$ printf "%02d\n" 8
08
$ printf "%02d\n" 9
09
------------------------------
As you can see, for some reason printf doesn't like 08 or 09, but acts as expected for the others.. 8 and 9 (without the leading zero) also seems ok.
Is this fishy, or is it monday again and am I missing something blatantly obvious ?
Cheers
Wout
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:09 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:12 PM
тАО02-11-2007 11:12 PM
Re: printf shell command
---
$ printf "%02d\n" 10
10
$ printf "%o\n" 10
12
---
The plan is to convert any one or two digit integer to a two digit one, leading zero if applicable.
I could use sed just as well, but I'm intrigued about my 'issue' ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:18 PM
тАО02-11-2007 11:18 PM
Re: printf shell command
It is monday for sure :-p
Thanks !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:27 PM
тАО02-11-2007 11:27 PM
Re: printf shell command
your code is right, just don't pass the leading zero as input!
printf "%02d\n" 1 => 01
printf "%02d\n" 9 => 09
printf "%02d\n" 10 => 10
etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:37 PM
тАО02-11-2007 11:37 PM
Re: printf shell command
you mix input- and output specifications:
The %-specifier is an output directive - you specify this.
For the input, the printf knows nothing about a typedef or so - it just sees a string. This string - the 08 - is scanned heuristically for a valid input specification. The convention is now, e.g.
0N - octal number N
0xNN - hex number NN
Any "violation" will be reported; the diagnostic output will differ between implementations of printf (e.g. printf: 08 not completely converted.).
To get your requested output format, use:
typeset -Z2 j
typeset -i i=0
while [ i -lt 12 ]
do
j=$i
print $j
((i+=1))
done
The output:
00
01
02
03
04
05
06
07
08
09
10
11
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:39 PM
тАО02-11-2007 11:39 PM
Re: printf shell command
I've quickly sed'ded the leading zero away, no problem there.. Thanks for sorting out my confusion, I was really dazzled there for a second :-)
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2007 11:44 PM
тАО02-11-2007 11:44 PM
Re: printf shell command
the typeset -Z2 will work for this as well:
typeset -Z2 j=08
print $j
08
mfG Peter
- Tags:
- typeset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-12-2007 12:15 AM
тАО02-12-2007 12:15 AM
Re: printf shell command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-12-2007 01:50 AM
тАО02-12-2007 01:50 AM
Re: printf shell command
Bill Hassell, sysadmin