将浮点变量转换为整数?

发布于 2024-08-03 02:12:40 字数 352 浏览 3 评论 0原文

如果页面加载时间超过 6 秒,下面所示的 shell 脚本将显示警告。问题是 myduration 变量不是整数。如何将其转换为整数?

myduration=$(curl http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}) > /dev/null ; \
[[ $myduration -gt 1 ]] && echo "`date +'%y%m%d%H%M%S'

加载页面花费了超过 6 秒 http://192.168.50.1/mantisbt/view.php?id=1

The shell script shown below will show a warning if the page takes more than 6 seconds to load. The problem is that the myduration variable is not an integer. How do I convert it to integer?

myduration=$(curl http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}) > /dev/null ; \
[[ $myduration -gt 1 ]] && echo "`date +'%y%m%d%H%M%S'

It took more than 6 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

森末i 2024-08-10 02:12:40

假设 $myduration 是小数或整数

$ myduration=6.5
$ myduration=$( printf "%.0f" $myduration )
$ echo $myduration
6

Assuming $myduration is a decimal or integer

$ myduration=6.5
$ myduration=$( printf "%.0f" $myduration )
$ echo $myduration
6
昨迟人 2024-08-10 02:12:40

你可以这样做:

float=1.23
int=${float%.*}

我在 bash 上使用它。

You can do this:

float=1.23
int=${float%.*}

I am using this on bash.

屋檐 2024-08-10 02:12:40

这还不完全清楚,但我认为您是在问如何在 bash 中将浮点值 (myduration) 转换为整数。这样的事情可能会对您有所帮助,具体取决于您想要对数字进行舍入的方式。

#!/bin/bash

floor_val=
ceil_val=

function floor() {
    float_in=$1
    floor_val=${float_in/.*}
}

function ceiling() {
    float_in=$1
    ceil_val=${float_in/.*}
    ceil_val=$((ceil_val+1))
}


float_val=$1
echo Passed in: $float_val
floor $float_val
ceiling $float_val

echo Result of floor: $floor_val
echo Result of ceiling: $ceil_val

用法示例:

$ ./int.sh 12.345
Passed in: 12.345
Result of floor: 12
Result of ceiling: 13

It's not entirely clear, but I think you're asking how to convert a floating-point value (myduration) to an integer in bash. Something like this may help you, depending on which way you want to round your number.

#!/bin/bash

floor_val=
ceil_val=

function floor() {
    float_in=$1
    floor_val=${float_in/.*}
}

function ceiling() {
    float_in=$1
    ceil_val=${float_in/.*}
    ceil_val=$((ceil_val+1))
}


float_val=$1
echo Passed in: $float_val
floor $float_val
ceiling $float_val

echo Result of floor: $floor_val
echo Result of ceiling: $ceil_val

Example usage:

$ ./int.sh 12.345
Passed in: 12.345
Result of floor: 12
Result of ceiling: 13
指尖凝香 2024-08-10 02:12:40

从变量中删除页面内容:

当我尝试您的命令时,myduration 包含我在测试中使用的 URL 处的页面 HTML 内容以及时间值。通过添加 -s 来抑制进度条,并将 -o /dev/null 添加到 curl 的选项中,我能够删除重定向到 /dev/null 并仅保存 myduration 中的时间。

由于 myduration 的值可能很短,因此您可以使用 ire_and_curses 显示技术,该技术通常会产生零,其结果是小于您正在测试的 1(但请注意,您的日志消息显示“6 秒”)。

更精细的分辨率:

如果您想要进行更精细的分辨率测试,您可以使用如下技术将 myduration 乘以 1000:

mult1000 () {
    local floor=${1%.*}
    [[ $floor = "0" ]] && floor=''
    local frac='0000'
    [[ $floor != $1 ]] && frac=${1#*.}$frac
    echo ${floor}${frac:0:3}
}

编辑:此版本的 mult1000 可以正确处理“0.234”、“1”、“2.”、“3.5”等值
和“6.789”。对于超过三位小数的值,无论值如何,多余的数字都会被截断而不进行四舍五入(“1.1119”变为“1.111”)。

您的脚本进行了我上面提到的更改并使用 mult1000 (使用我自己的示例时间):

myduration=$(curl -s -o /dev/null http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}); [[ $(mult1000 $myduration) -gt 3500 ]] && echo "`date +'%y%m%d%H%M%S'` took more than 3.5 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1 " >> /home/shantanu/speed_report.txt

这里它被分成多行(并简化),以使其在这个答案中更具可读性:

myduration=$(curl -s -o /dev/null http://example.com -w %{time_total})
[[ $(mult1000 $myduration) -gt 3500 ]] &&
  echo "It took more than 3.5 seconds to load thttp://example.com" >> report.txt

Eliminate page contents from the variable:

When I tried your command, myduration contained the HTML contents of the page at the URL I used in my test plus the time value. By adding -s to suppress the progress bar and adding -o /dev/null to the options for curl, I was able to remove the redirect to /dev/null and have only the time saved in myduration.

Since the value of myduration is likely to be short, you can use the technique ire_and_curses shows which will often yield zero as its result which would be less than the 1 you are testing for (note that your log message says "6 seconds", though).

Finer resolution:

If you'd like to have a finer resolution test, you can multiply myduration by 1000 using a technique like this:

mult1000 () {
    local floor=${1%.*}
    [[ $floor = "0" ]] && floor=''
    local frac='0000'
    [[ $floor != $1 ]] && frac=${1#*.}$frac
    echo ${floor}${frac:0:3}
}

Edit: This version of mult1000 properly handles values such as "0.234", "1", "2.", "3.5"
and "6.789". For values with more than three decimal places, the extra digits are truncated without rounding regardless of the value ("1.1119" becomes "1.111").

Your script with the changes I mentioned above and using mult1000 (with my own example time):

myduration=$(curl -s -o /dev/null http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}); [[ $(mult1000 $myduration) -gt 3500 ]] && echo "`date +'%y%m%d%H%M%S'` took more than 3.5 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1 " >> /home/shantanu/speed_report.txt

Here it is broken into multiple lines (and simplified) to make it more readable here in this answer:

myduration=$(curl -s -o /dev/null http://example.com -w %{time_total})
[[ $(mult1000 $myduration) -gt 3500 ]] &&
  echo "It took more than 3.5 seconds to load thttp://example.com" >> report.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文