bash 脚本中区分 2 和 02

发布于 2024-10-16 23:45:18 字数 535 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉鱼一梦 2024-10-23 23:45:18

有点不清楚你要求什么。如果您只想去掉一个前导零,那么可以这样做:

month=${month#0}

这会将 02 变成 2,而 12 仍然是 12

如果您需要删除多个零(上面的 002 将变成 02),您需要执行不同的操作,例如使用正则表达式

while [ -z "${month##0*}" ]; do month=${month#0}; done

或使用正则表达式,就像 sed

month=$(sed -e 's/^0*//'<<<"$month")

HTH

编辑
根据您的编辑;正如已经建议的那样,使用 printf

month=$(printf %02d "$month")

这会将 2 变成 0212 保持原样,就像那样123。如果您想强制输入两位数,或者您没有 printf (这是 bash 中内置的 shell,通常也可用其他方式,所以机会非常低),sed 可以再次提供帮助,

month=$(sed -e 's/.*\(..\)$/\1/'<<<"00$month")

它会在前面添加两个零 (002) 并保留最后两个字符 (02),将空字符串转换为 00< /代码>也是如此。不过,这也会将 a 变成 0a。想想看,您不需要 sed

month="00$month"
month="${month:0-2}"

(需要 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:

month=${month#0}

This will turn 02 into 2, and 12 remains 12.

If you need to remove more than one zero (above 002 will turn into 02), you'll need to do something different, like using regular expressions

while [ -z "${month##0*}" ]; do month=${month#0}; done

or use regular expressions, like with sed

month=$(sed -e 's/^0*//'<<<"$month")

HTH

EDIT
As per your edit; as has already been suggested, use printf

month=$(printf %02d "$month")

this will turn 2 into 02, 12 remains as is, as does 123. If you want to force a two digit number or you don't have printf (which is a shell built-in in bash, and usually available otherwise too, so chances are pretty low), sed can help again

month=$(sed -e 's/.*\(..\)$/\1/'<<<"00$month")

which will prepend two zeros (002) and keep the last two characters (02), turning the empty string into 00 as well. This will turn a into 0a too though. Come to think of it, you don't need sed for this

month="00$month"
month="${month:0-2}"

(The 0- is required to disambiguate from default value expansion)

森林很绿却致人迷途 2024-10-23 23:45:18

只需使用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.

冰之心 2024-10-23 23:45:18

要去除所有前导零,无论数字多少,也无论最终数字的数量(例如修复“010”):

shopt -s extglob
month=009
month=${month##+(0)}
echo "$month"

演示:

$ for a in 2 02 002 0002 20 020 00200; do echo ${a##+(0)}; done
2
2
2
2
20
20
200

修复任何日期:

shopt -s extglob
fixdigits () {
    if (( ${#1} > 2 || ${#1} < 1 ))
    then
        echo "Invalid number of digits"
        return 1
    fi
    printf '%.2d\n' 0${1##+(0)}
}

演示:

$ for a in 2 02 002 0002 20 020 00200; do fixdigits $a; done
02
02
Invalid number of digits
Invalid number of digits
02
Invalid number of digits
Invalid number of digits

或者:

shopt -s extglob
fixdigits () {
    digits=${1##+(0)}
    if (( ${#digits} > 2 || ${#digits} < 1 ))
    then
        echo "Invalid number of digits"
        return 1
    fi
    printf '%.2s\n' 0$digits
}

演示:

$ for a in 2 02 002 0002 20 020 00200; do fixdigits $a; done
02
02
02
02
02
02
Invalid number of digits

To strip all leading zeros, regardless of the number and regardless of the number of final digits (it fixes "010" for example):

shopt -s extglob
month=009
month=${month##+(0)}
echo "$month"

Demo:

$ for a in 2 02 002 0002 20 020 00200; do echo ${a##+(0)}; done
2
2
2
2
20
20
200

To fix any date:

shopt -s extglob
fixdigits () {
    if (( ${#1} > 2 || ${#1} < 1 ))
    then
        echo "Invalid number of digits"
        return 1
    fi
    printf '%.2d\n' 0${1##+(0)}
}

Demo:

$ for a in 2 02 002 0002 20 020 00200; do fixdigits $a; done
02
02
Invalid number of digits
Invalid number of digits
02
Invalid number of digits
Invalid number of digits

Alternatively:

shopt -s extglob
fixdigits () {
    digits=${1##+(0)}
    if (( ${#digits} > 2 || ${#digits} < 1 ))
    then
        echo "Invalid number of digits"
        return 1
    fi
    printf '%.2s\n' 0$digits
}

Demo:

$ for a in 2 02 002 0002 20 020 00200; do fixdigits $a; done
02
02
02
02
02
02
Invalid number of digits
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文