gpt4 book ai didi

linux - 比较两个数字,但如果条件不能正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:54 32 4
gpt4 key购买 nike

我是 bash 脚本的新手。我正在编写一个脚本来计算CPU使用率。所以我正在做的是计算 CPU 使用率并与预定义值(即阈值)进行比较。但我的if条件总是出错。请帮我改正我的脚本。

#!/bin/bash
#checking the cpu usage
cpu_usage=$( mpstat | awk '$12 ~ /[0-9.]+/ { print 100 - $12 }')
cpu_threshold=1.00
if [ $cpu_usage > $cpu_threshold ] ; then
echo "The CPU utilization is above threshold : $cpu_usage %"
else
echo "The CPU utilization is below threshold : $cpu_usage %"
fi

请检查 bash 中的上述脚本,因为我尝试了很多方法,但总是 if 条件给出错误的输出。

最佳答案

@Ankit:尝试:将 if [ $cpu_usage > $cpu_threshold ] 更改为 if [[ $cpu_usage > $cpu_threshold ]]

关于linux - 比较两个数字,但如果条件不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501356/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com