bash 确保 int 变量长度为 2 位数,否则在前面添加 0 使其长度为 2 位数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1898712/
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
Make sure int variable is 2 digits long, else add 0 in front to make it 2 digits long
提问by Mint
How do I check a int variable ($inputNo) to see if it's 2 or more decimal digits long?
如何检查 int 变量 ($inputNo) 以查看它是否为 2 个或更多十进制数字?
Example:
例子:
inputNo="5"
Should be changed to: 05
应改为:05
inputNo="102"
Should be left alone: 102
应该单独留下:102
I thought about using wc
and if
statements, but wc -m
doesn't seems to give the actual characters passed into wc
, as wc
always seems to give +1 to the characters that is given.
我考虑过使用wc
andif
语句,但wc -m
似乎没有给出传入的实际字符wc
,因为wc
似乎总是给给出的字符+1。
But I don't know how to add a 0 in front of the current input number.
但是我不知道如何在当前输入的数字前面加一个0。
回答by paxdiablo
You can use the bash-builtin printf
with the -v
option to write it to a variable rather than print it to standard output:
您可以使用printf
带有-v
选项的 bash-builtin将其写入变量而不是将其打印到标准输出:
pax> inputNo=5 ; printf -v inputNo "%02d" $inputNo ; echo $inputNo
05
pax> inputNo=102 ; printf -v inputNo "%02d" $inputNo ; echo $inputNo
102
You'll want to make sure it's numeric first otherwise the conversion will fail. If you want to be able to pad anystring out to two or more characters, you can also use:
您首先要确保它是数字,否则转换将失败。如果您希望能够将任何字符串填充为两个或更多字符,您还可以使用:
while [[ ${#inputNo} -lt 2 ]] ; do
inputNo="0${inputNo}"
done
which is basically a while
loop that prefixes your string with "0" until the length is greater than or equal to two.
这基本上是一个while
循环,在您的字符串前加上“0”,直到长度大于或等于 2。
Note that this can also be done in bash
by prefixing the number with two zeroes then simply getting the last two characters of that string, checking first that it's not already at least the desired size:
请注意,这也可以bash
通过在数字前面加上两个零然后简单地获取该字符串的最后两个字符来完成,首先检查它是否至少不是所需的大小:
if [[ ${#inputNo} -lt 2 ]] ; then
inputNo="00${inputNo}"
inputNo="${inputNo: -2}"
fi
The difference is probably not too great for a two-digit number but you may find the latter solution is better if you need larger widths.
对于两位数来说,差异可能不会太大,但如果您需要更大的宽度,您可能会发现后一种解决方案更好。
If you're using a shell otherthan bash
(unlikely, based on your tags), you'll need to find the equivalents, or revert to using external processes to do the work, something like:
如果您使用的是壳等比bash
(不可能的,根据你的标签),你需要找到等价物,或恢复到使用外部程序做的工作,是这样的:
while [[ $(echo -n ${inputNo} | wc -c) -lt 2 ]] ; do
inputNo="0${inputNo}"
done
This does basically what you were thinking off in your question but note the use of -n
in the echo
command to prevent the trailing newline (which was almost certainly causing your off-by-one error).
这基本上符合您在问题中的想法,但请注意-n
在echo
命令中使用 来防止尾随换行符(这几乎肯定会导致您的一对一错误)。
But, as stated, this is a fall-back position. If you're using bash
, the earlier suggestions of mine are probably best.
但是,如前所述,这是一个后备立场。如果您正在使用bash
,我之前的建议可能是最好的。
回答by Paused until further notice.
For general-purpose padding whether the string is numeric or not
无论字符串是否为数字,用于通用填充
No need for piping echo
into wc
or using a while
loop.
无需管道echo
进入wc
或使用while
循环。
In Bash, you can get the length of a string like this: ${#inputNo}
.
在bash中,你可以得到这样的字符串的长度:${#inputNo}
。
And since you can do substrings, you can do this instead:
因为你可以做子字符串,所以你可以这样做:
if [[ ${#input} < 2 ]]
then
inputNo="00${inputNo}"
inputNo="${inputNo: -2}"
fi
回答by miku
You can use http://bash-hackers.org/wiki/doku.php/commands/builtin/printf, an example from there:
您可以使用http://bash-hackers.org/wiki/doku.php/commands/builtin/printf,一个例子:
the_mac="0:13:ce:7:7a:ad"
# lowercase hex digits
the_mac="$(printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${the_mac//:/ 0x})"
# or the uppercase-digits variant
the_mac="$(printf "%02X:%02X:%02X:%02X:%02X:%02X" 0x${the_mac//:/ 0x})"