bash 将零添加到个位数变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5099119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a zero to single digit variable
提问by Colby
Trying to add a zero before the varaible if it's less than 10 and create said directory. I can't seem to get the zero to add correctly. Keeps resulting in making 02.1.2011
, 02.2.2011
etc,etc.
如果变量小于 10,则尝试在变量前添加零并创建所述目录。我似乎无法正确添加零。不断导致制作02.1.2011
,02.2.2011
等等,等等。
i=0
for i in {01..31}
do
if $i > 10
then
mkdir $path/02.0$i.2011
else
mkdir $path/02.$i.2011
fi
done
回答by paxdiablo
You can replace the whole lot with:
您可以将整个批次替换为:
for i in 0{1..9} {10..31} ; do
mkdir $path/02.$i.2011
done
while still not having to start up any external processes (other than what may be in the loop body).
同时仍然不必启动任何外部进程(循环体中可能存在的进程除外)。
That's probably not that important heresince mkdir
is not one of those things you tend to do a lot of in a tight loop but it willbe important if you write a lot of your quick and dirty code in bash
.
这可能不是那么重要这里既然mkdir
不是那种你会做了很多在紧凑循环的事情之一,但它会如果你写了很多在你的快速和肮脏的代码是很重要的bash
。
Process creation is expensive when you're doing it hundreds of thousands of times as some of my scripts have occasionally done :-)
当您像我的一些脚本偶尔那样做数十万次时,流程创建是昂贵的:-)
Example so you can see it in action:
示例,以便您可以看到它的实际效果:
pax$ for i in 0{7..9} {10..12}; do echo $i; done
07
08
09
10
11
12
And, if you have a recent-enough version of bash
, it will honor your request for leading digits:
而且,如果您有足够新的 版本bash
,它将满足您对前导数字的要求:
A sequence expression takes the form
{x..y[..incr]}
, wherex
andy
are either integers or single characters, andincr
, an optional increment, is an integer. When integers are supplied, the expression expands to each number betweenx
andy
, inclusive. Supplied integers may be prefixed with0
to force each term to have the same width. When eitherx
ory
begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary.
序列表达式的形式为
{x..y[..incr]}
,其中x
和y
是整数或单个字符,而incr
是可选增量,是整数。当提供整数时,表达式扩展到x
和之间的每个数字y
,包括。提供的整数可能以 为前缀,0
以强制每个术语具有相同的宽度。当x
或y
以零开头时,shell 会尝试强制所有生成的术语包含相同数量的数字,并在必要时填充零。
So, on my Debian 6 box, with bash
version 4.1.5:
因此,在我的 Debian 6 机器上,bash
版本为 4.1.5:
pax$ for i in {07..11} ; do echo $i ; done
07
08
09
10
11
回答by Jesse Cohen
You can use
您可以使用
$(printf %02d $i)
To generate the numbers with the format you want.
以您想要的格式生成数字。
for i in $(seq 0 1 31)
do
mkdir $path/02.$(printf %02d $i).2011
done
回答by Juliano
Better:
更好的:
for i in $(seq -f %02g 1 31)
do
mkdir "$path/02.$i.2011"
done
Or even:
甚至:
for i in {01..31}
do
mkdir "$path/02.$(printf "%02d" $i).2011"
done
回答by Paused until further notice.
In Bash 4, brace range expansions will give you leading zeros if you ask for them:
在 Bash 4 中,如果您要求,括号范围扩展将为您提供前导零:
for i in {01..31}
without having to do anything else.
无需做任何其他事情。
If you're using earlier versions of Bash (or 4, for that matter) there's no need to use an external utility such as seq
:
如果您使用的是较早版本的 Bash(或 4,就此而言),则无需使用外部实用程序,例如seq
:
for i in {1..31}
or
或者
for ((i=1; i<=31; i++))
with either of those:
与其中任何一个:
mkdir "$path/02.$(printf '%02d' "$i").2011"
You can also do:
你也可以这样做:
z='0'
mkdir "$path/02.${z: -${#i}}$i.2011"
Using paxdiablo's suggestion, you can make all the directories at once without a loop:
使用 paxdiablo 的建议,您可以一次创建所有目录而无需循环:
mkdir "$path"/02.{0{1..9},{10..31}}.2011
回答by Adam Liss
Will this work for you?
这对你有用吗?
zeroes="0000000"
pad=$zeroes$i
echo ${pad:(-2)}
回答by Amin Abbaspour
$ seq --version | head -1
seq (GNU coreutils) 8.21
$ seq -f "%02g" 1 10
01
02
03
04
05
06
07
08
09
10
回答by Dhaulakhandi
I created this simple utility to perform this, Good Luck , hope this helps stack'ers!!
我创建了这个简单的实用程序来执行此操作,祝您好运,希望这有助于堆栈人员!!
for i in {1..24}
do
charcount=`echo $i|wc -m`
count=`expr $charcount - 1`
if [ $count -lt 2 ];
then
i="0`echo $i`"
fi
echo "$i"
done
回答by kurumi
path=/tmp
ruby -rfileutils -e '1.upto(31){|x| FileUtils.mkdir "'$path'/02.%02d.2011" % x}'
回答by jth
Here's what I did for a script where I'm asking for a day, range of days, regex, etc. with a default to the improved regexes shared by paxdiablo.
这是我为脚本所做的,其中我要求一天、天数范围、正则表达式等,默认为由 paxdiablo 共享的改进的正则表达式。
for day in $days
do if [ 1 -eq "${#day}"] ; then
day="0$day"
fi
I just run through this at the beginning of my loop where I run a bunch of log analysis on the day in question, buried in directories with leading zeroes.
我只是在我的循环开始时运行了这个,在那里我在有问题的那天运行了一堆日志分析,埋在带有前导零的目录中。
回答by Kevin Dolan
Your if/then statement is backwards. You are adding a 0 when it is a above 10, not adding one when it is below.
你的 if/then 语句是倒退的。大于 10 时加 0,小于 10 时不加 1。
Another bug is that you make the cutoff strictly greater than 10, which does not include 10, even though 10 is two digits.
另一个错误是您使截止值严格大于 10,其中不包括 10,即使 10 是两位数。