bash 脚本中区分 2 和 02
我有一个 bash 脚本,它将日期、月份和年份作为单独的参数。使用它,构造一个 url,然后使用 wget
获取内容并将其存储在 html 文件中(例如 t.html)。现在,用户可以输入 2 位数的月份/日期(如 02 而不是 2,反之亦然)。如何区分上述两种格式并在脚本中纠正这一点?
该网址的工作原理如下:
日期:需要输入 2 位数字。因此,必须将 7 作为 07 提供,才能正确构建 url。在这里,我正在寻找一种检查,如果日期小于 10 并且前面没有零,则会在日期后附加一个零。因此,在构造 url 之前,日期字段的 7 应变为 07。
月份:需要输入 2 位数字,但这里如果月份 则 url 会自动附加 0。 10. 因此,如果用户输入 2,则 url 形成 02,但如果用户输入 02,则 url 形成 002。这里,可能需要附加或删除 0。
PS:我知道这个方法后面跟着 url s*c&s,但我只需要使用它。
谢谢,
斯里拉姆
I have a bash script that takes the date, month and year as separate arguments. Using that, a url is constructed which then uses wget
to fetch content and store it in an html file (say t.html). Now, the user may enter a 2 digit month/date (as in 02 instead of just 2 and vice-versa). How do I distinguish between the above two formats and correct for this from within the script?
The url works as follows:
Date: a 2 digit input is needed. So a 7 must be supplied as 07 for the url to be constructed properly. Here I am looking for a check that would append a zero to the date in case it is less than 10 and does not already have a zero in front. So, 7 should become 07 for the date field before the url is constructed.
Month: a 2 digit input is needed, but here the url automatically appends the 0 in case month < 10. So, if the user enters 2, then the url forms 02, but if the user enters 02, the url forms 002. Here, the 0 may need to be appended or removed.
P.S: I know this method followed by the url s*c&s, but I just need to work with it.
Thanks,
Sriram
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有点不清楚你要求什么。如果您只想去掉一个前导零,那么可以这样做:
这会将
02
变成2
,而12
仍然是12
。如果您需要删除多个零(上面的
002
将变成02
),您需要执行不同的操作,例如使用正则表达式或使用正则表达式,就像
sed
HTH
编辑
根据您的编辑;正如已经建议的那样,使用
printf
这会将
2
变成02
,12
保持原样,就像那样123
。如果您想强制输入两位数,或者您没有printf
(这是 bash 中内置的 shell,通常也可用其他方式,所以机会非常低),sed
可以再次提供帮助,它会在前面添加两个零 (
002
) 并保留最后两个字符 (02
),将空字符串转换为00< /代码>也是如此。不过,这也会将
a
变成0a
。想想看,您不需要sed
(需要
0-
来消除默认值扩展的歧义)It's a little unclear what you're asking for. If you're only looking to strip one leading zero, then this will do:
This will turn
02
into2
, and12
remains12
.If you need to remove more than one zero (above
002
will turn into02
), you'll need to do something different, like using regular expressionsor use regular expressions, like with
sed
HTH
EDIT
As per your edit; as has already been suggested, use
printf
this will turn
2
into02
,12
remains as is, as does123
. If you want to force a two digit number or you don't haveprintf
(which is a shell built-in in bash, and usually available otherwise too, so chances are pretty low),sed
can help againwhich will prepend two zeros (
002
) and keep the last two characters (02
), turning the empty string into00
as well. This will turna
into0a
too though. Come to think of it, you don't needsed
for this(The
0-
is required to disambiguate from default value expansion)只需使用
printf
即可。例如
$(printf "%02d" $day)
添加前导零以防大小写。和
$(printf "%1d" $month)
去除零以防大小写。Just use
printf
.For example
$(printf "%02d" $day)
to add the leading zero in case.And
$(printf "%1d" $month)
to strip the zero in case.要去除所有前导零,无论数字多少,也无论最终数字的数量(例如修复“010”):
演示:
修复任何日期:
演示:
或者:
演示:
To strip all leading zeros, regardless of the number and regardless of the number of final digits (it fixes "010" for example):
Demo:
To fix any date:
Demo:
Alternatively:
Demo: