English 中文(简体)
bash的印刷材料:“09”和“08”都是无效的,“07”和“06”都是罚款。
原标题:printf in bash: "09" and "08" are invalid numbers, "07" and "06" are fine

This is my bash script - I just want to left-pad a set of numbers with zeroes:

printf "%04d" "09"
printf "%04d" "08"
printf "%04d" "07"
printf "%04d" "06"

产出:

./rename.sh: line 3: printf: 09: invalid number 
0000
./rename.sh: line 4: printf: 08: invalid number 
0000 
0007
0006

什么?

只有09和08个国家造成这一问题:我顺序上的所有其他数字似乎都是科索沃。

最佳回答

如果您有<条码>09”,你可以做些什么。

a="09"
echo "$a"
echo "${a#0}"
printf "%04d" "${a#0}"

为什么这样做? 确切地说,从0开始,但第2个地方没有x的编号被解释为中值。

编码<0。

http://www.un.org/Depts/DGACM/index_french.htm 由此得出的数值可加到f>,然后以4位数表示,以适当印制0

如果您必须期望您获得诸如009”等值,则随着您在开始时不得不使用一种可消除<0<0<>0的所有超文本的通道,或如评论意见所述,extglob的表述,情况就变得更加复杂。

问题回答

Bash s numeric arithmetic evaluation syntax (( ... )) can convert to base 10 (therefor ensuring correct interpretation) with the following syntax: (( 10#$var )). Or, in the case of a raw number: (( 10#08 )). Very simple & clean and can be used anywhere you re sure the base should be 10, but can t guarantee a leading zero won t be included.

So, in your example it would be as follows:

printf "%04d
" $(( 10#09 ))
printf "%04d
" $(( 10#08 ))
printf "%04d
" $(( 10#07 ))
printf "%04d
" $(( 10#06 ))

编制以下产出:

0009
0008
0007
0006

由于你接着以变数而不是变数本身的价值重新工作,因此,升温器(((var++ )) & decrementors (((var- ))赢得了t work,但仍可以相对清洁地作为

我首先碰到了这一解决办法:http://ubuntuforums.org/showthread.php?t=1402291#post8805742” rel=“noretinger”>here,但

处理点不同:

printf "%04.f" "009"

这样做可以得出正确的产出,而没有处理任何原始现金问题(每回答@Oli Charlesworth, 0***被作为中间值处理,但我认为Bash忽略了浮动点数字的中值/高值识别器)。

仅添加到Oli swer,以便在<>%<<<>/code>之后添加一个编号为零的编号。

printf "%04d" "9"

Why did “09” 以及 “08” are invalid numbers, “07” 以及 “06” are fine

由于之前按<代码>0进行分类,是一种常规的简短含义: 序号为

printf "%d
" 010 0100
8
64

printf "%o
" 8 64 0100
10
100
100
printf "%04o
" 8 64 0100
0010
0100
0100

How to work with this?

Using bash integer: preceding number by 10#

Under bash, you could precise by this way, which base is used for number

echo $(( 10#0100))
100
echo $(( 10#0900))
900
echo $(( 10#0900 ))
900

以及

echo $(( 8#100 ))
64
echo $(( 2#100 ))
4

当然!

There are only 10 types of people in the world: those who underst以及 binary, 以及 those who don t.

Using Parameter Expansion

为此,您必须使用一个变量。

a=09
echo ${a#0}
9

This will work fine until you just have only one 0 to drop.

a=0000090

echo ${a#*0}
000090
echo ${a##0}
000090

http://www.un.org/Depts/DGACM/index_french.htm

echo ${a##*(0)}
0000090
shopt -s extglob
echo ${a##*(0)}
90

Using floating point with printf

printf "%.0f
" 09
9

我愿使用<代码>-v 备选办法> printf,用于确定一些变量:

printf -v a %.0f 09
echo $a
9

or even if

a=00090
printf -v a %.0f $a
echo $a
90

Adding * makes shell parameter expansion matching greedy (see, for example, Shell Tipps: use internal string handling)!

# strip leading 0s
- a="009"; echo ${a##0}
+ a="009"; echo ${a##*0}

if a=079 first, remove the trailing zeros by

a=`echo $a | sed  s/^0*// `

接着是零婚。

printf "%04d" $a

更简单地说,从职业转为男性:

nm=$((10#$nm))




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...