在单个数字变量中添加0。

时间:2022-03-06 09:47:17

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.

尝试在varaible之前添加一个0,如果它小于10并且创建一个说的目录。我似乎无法正确地添加0。持续造成2011年1月2日,2011年2月2日等。

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

10 个解决方案

#1


45  

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 here since mkdir is not one of those things you tend to do a lot of in a tight loop but it will be 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]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. When integers are supplied, the expression expands to each number between x and y, inclusive. Supplied integers may be prefixed with 0 to force each term to have the same width. When either x or y begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary.

序列表达式接受form {x..y[.]在其中,x和y是整数或单个字符,而incr是一个可选的增量,是一个整数。当提供整数时,表达式会扩展到x和y之间的每个数字,包括。提供的整数可以以0为前缀,以迫使每个项具有相同的宽度。当x或y从0开始时,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

#2


19  

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

#3


11  

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

#4


4  

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

#5


2  

Will this work for you?

这对你有用吗?

zeroes="0000000"
pad=$zeroes$i
echo ${pad:(-2)}

#6


2  

$ seq --version | head -1
seq (GNU coreutils) 8.21
$ seq -f "%02g" 1 10
01
02
03
04
05
06
07
08
09
10

#7


0  

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,而不是在下面加一个。

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是两位数。

#8


0  

path=/tmp
ruby -rfileutils -e '1.upto(31){|x| FileUtils.mkdir "'$path'/02.%02d.2011" % x}'

#9


0  

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所共享的改进的regex。

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.

我在循环开始的时候,在我的循环开始的时候,我运行了一堆日志分析,在这个问题中,被埋在带前导零的目录中。

#10


0  

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

#1


45  

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 here since mkdir is not one of those things you tend to do a lot of in a tight loop but it will be 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]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. When integers are supplied, the expression expands to each number between x and y, inclusive. Supplied integers may be prefixed with 0 to force each term to have the same width. When either x or y begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary.

序列表达式接受form {x..y[.]在其中,x和y是整数或单个字符,而incr是一个可选的增量,是一个整数。当提供整数时,表达式会扩展到x和y之间的每个数字,包括。提供的整数可以以0为前缀,以迫使每个项具有相同的宽度。当x或y从0开始时,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

#2


19  

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

#3


11  

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

#4


4  

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

#5


2  

Will this work for you?

这对你有用吗?

zeroes="0000000"
pad=$zeroes$i
echo ${pad:(-2)}

#6


2  

$ seq --version | head -1
seq (GNU coreutils) 8.21
$ seq -f "%02g" 1 10
01
02
03
04
05
06
07
08
09
10

#7


0  

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,而不是在下面加一个。

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是两位数。

#8


0  

path=/tmp
ruby -rfileutils -e '1.upto(31){|x| FileUtils.mkdir "'$path'/02.%02d.2011" % x}'

#9


0  

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所共享的改进的regex。

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.

我在循环开始的时候,在我的循环开始的时候,我运行了一堆日志分析,在这个问题中,被埋在带前导零的目录中。

#10


0  

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