简体   繁体   English

与 BASH 中的字符串进行比较的问题

[英]Issue comparing to strings in BASH

I need to make a bash script to compare the space of a vg on redhat servers.我需要制作一个 bash 脚本来比较 redhat 服务器上 vg 的空间。 What I have so far is:到目前为止,我所拥有的是:

#!/bin/bash

##VARS
CMD=$(vgs | grep vgSoft | awk -F" " '{print $7}' | awk -F"<" '{print $2}'| awk -F"g" '{print $1}')
SPACE="10.00"

if [[ "$CMD" < "$SPACE" ]]; then
    echo "It's not enough space, you must extend the VG"
    echo "Space is equal to: $CMD GB"
elif [[ "$CMD" == "$SPACE" ]]; then
    echo "It's enough space but consider extend the VG"
elif [[ "$CMD" > "$SPACE" ]]; then
    echo "It's enough space"
    echo "Space is equal to: $CMD GB"
fi

What do I do with this command is obtaing the output of volume groups and parse the data for obtain the desired value vgs | grep vgSoft | awk -F" " '{print $7}' | awk -F"<" '{print $2}'| awk -F"g" '{print $1}'我用这个命令做什么是获取卷组的 output 并解析数据以获得所需的值vgs | grep vgSoft | awk -F" " '{print $7}' | awk -F"<" '{print $2}'| awk -F"g" '{print $1}' vgs | grep vgSoft | awk -F" " '{print $7}' | awk -F"<" '{print $2}'| awk -F"g" '{print $1}' vgs | grep vgSoft | awk -F" " '{print $7}' | awk -F"<" '{print $2}'| awk -F"g" '{print $1}' and save it to the CMD variable. vgs | grep vgSoft | awk -F" " '{print $7}' | awk -F"<" '{print $2}'| awk -F"g" '{print $1}'并将其保存到 CMD 变量中。 Where the output of vgs command is:其中vgs命令的 output 为:

[root@server~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  vgSoft   2  13   0 wz--n-  71.99g <26.14g
  vgSys    1  12   0 wz--n- <63.51g <36.52g

So in this case, the result of mi CMD variable is 26.14 And I'm making the comparision with the 10.00 (That is assigned to the SPACE variable)所以在这种情况下,mi CMD 变量的结果是26.14我正在与 10.00 进行比较(分配给 SPACE 变量)

But I'm having a issue comparing the strings, because is the output of vgs command is for example 9.46 the script says me that "It's enough space" when the output should be "It's not enough space, you must extend the VG" .但是我在比较字符串时遇到了问题,因为vgs命令的 output 是例如 9.46,当 output 应该是“空间不足,您必须扩展 VG”时,脚本告诉我“空间足够”。 As far I remember I don't be able to use the operators "eq" "gt" "ne" because I'm comparing to strings and no integers values据我记得我不能使用运算符“eq”“gt”“ne”,因为我正在比较字符串并且没有整数值

Any king of help is accepted.任何帮助之王都被接受。 Cheers!干杯!

With your shown samples, please try following awk code.对于您显示的示例,请尝试遵循awk代码。 We can do all this in single awk .我们可以在单个awk中完成所有这些。

We have an awk variable named space where I have defined value as 10 as per shown attempts of OP, one could change it as per need.我们有一个名为spaceawk变量,我根据所示的 OP 尝试将值定义为10 ,可以根据需要更改它。

vgs | awk -v space="10" '
FNR==1 || !/vgSoft/{ next }
{
  value=substr($7,2,length($7)-1)+0
}
value<space { 
  print "It\047s not enough space, you must extend the VG"
  next
}
value==space{
  print "It\047s enough space but consider extend the VG"
  next
}
value>space {
  print "It\047s enough space"
}
'

Explanation: Running vgs command first and sending its standard output as a standard input to awk program.说明:首先运行vgs命令并将其标准 output 作为标准输入发送到awk程序。 In awk program creating variable space with value of 10 as per shown attempts.awk程序中,按照所示尝试创建值为 10 的变量space Then in main program, checking condition(s) if its line first line OR line does not contain vgSoft string then simply jump to next line.然后在主程序中,检查条件,如果它的第一行或行不包含vgSoft字符串,则只需跳转到下一行。 If a line contains vgSoft string then creating a variable named value which will have value from 7th field of current line(without < and g from 7th field), then using 3 conditions to check(if value is equal to OR lesser than OR grater than space or not) accordingly printing statements then.如果一行包含vgSoft字符串,则创建一个名为value的变量,该变量将具有来自当前行的第 7 个字段的值(没有<和来自第 7 个字段的g ),然后使用 3 个条件来检查(如果值等于或小于或大于空格与否)然后相应地打印语句。

Fair warning I don't have vgs command with me so I tested this code with putting your sample output of vgs command into an Input_file and running it against awk program and it worked fine for me.公平警告我没有 vgs 命令,所以我测试了这段代码,将vgs命令的示例 output 放入 Input_file 并针对awk程序运行它,它对我来说工作得很好。

You may just use the echo command in combination with bc for comparison as shown below您可以将 echo 命令与 bc 结合使用进行比较,如下所示

$ SPACE=10.00
$ CMD=9.46
$ echo "${CMD}>${SPACE}" | bc
0
$ CMD=10.46
$ echo "${CMD}>${SPACE}" | bc
1

Doing it in just awk :仅在awk中执行此操作:

vgs | awk -v space=10.00 '/vgSoft/ {
   free = substr($7, 2, length($7) - 2)+0 # Assumes the < and g will always be there
   if (free < space) {
     print "It'"'"'s not enough space, you must extend the VG"
     print "Space is equal to: " space " GB"
   } else if (free == space) {
     print "It'"'"'s enough space but consider extend the VG"
   } else {
     print "It'"'"'s enough space"
     print "Space is equal to: " space " GB"
   }
   exit
}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM