在字符串中填充零 [英] Padding zeros in a string
问题描述
我正在编写bash脚本以获取一些播客.问题在于某些播客数字是一位数字,而其他的是2/3位数字,因此我需要对它们进行填充以使其全部变为3位数字.
I'm writing a bash script to get some podcasts. The problem is that some of the podcast numbers are one digits while others are two/three digits, therefore I need to pad them to make them all 3 digits.
我尝试了以下操作:
n=1
n = printf %03d $n
wget http://aolradio.podcast.aol.com/sn/SN-$n.mp3
,但是变量'n'不会永久填充.我如何使其永久?
but the variable 'n' doesn't stay padded permanently. How can I make it permanent?
推荐答案
使用反引号指定printf命令(``)的结果:
Use backticks to assign the result of the printf command (``):
n=1
wget http://aolradio.podcast.aol.com/sn/SN-`printf %03d $n`.mp3
请注意,我删除了实际上并不必要的一行. 如果要将'printf%...'的输出分配给n,则可以 使用
Note that i removed one line which was not really necessary. If you want to assign the output of 'printf %...' to n, you could use
n=`printf %03d $n`
然后,使用之前使用的$ n变量替换.
and after that, use the $n variable substitution you used before.
这篇关于在字符串中填充零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!