【问题标题】:How can I compare the output of a command, (the output is a number) to a number? Trying to see if the output is greater than or equal to 1如何将命令的输出(输出是数字)与数字进行比较?试图查看输出是否大于等于1
【发布时间】:2018-12-28 09:02:07
【问题描述】:

我正在尝试运行一个基本上运行 psg defunct | wc -l 的脚本,并根据其输出是否大于或等于 1 来提供输出。

但是,我无法让-ge 标志起作用。我想知道是否有人对我做错了什么有任何意见,如果你能帮助我纠正它。

这是当前脚本:

 #!/bin/bash -x
 export LANG="C"
 #
 # Created by Blake Smreker | b0s00dg
 # Purpose is to assist users with defunct process tickets
 #
 #

 menu ()
 {
 echo "What is the server name?"
 read srvrname
 badboi=$"$srvrname"
 }

 vars ()
 {
 psgwc=$"/u/bin/psg defunct | wc -l"
 psgd=$"/u/bin/psg defunct"
 die=$"/bin/kill -9 `psg defunct | awk '{print $3}'`"
 belowmsg=$'echo The number of defunct processes is below threshold! Resolve the ticket and put the following in the ticket:'
 abovemsg=$'echo There is more than 100 zombies! Getting you more information:'
 }

 connect ()
 {
 connectme="/usr/bin/dzdo -u oseho /bin/ssh -qo PreferredAuthentications=publickey root@$badboi"
 }

 main ()
      {
 ${connectme} ". /u/data/environment; $psgwc"

 if ${connectme} $psgwc -ge 1
 then
     $abovemsg
     echo "Determine the owner of the below process. Then copy and paste the below, and send it to the team on Service Now."
     echo "psg defunct | wc -l:"
     ${connectme} $psgwc
     ${connectme} $psgd
 else
     $belowmsg
     echo "psg defunct | wc -l:"
     ${connectme} $psgwc
 fi
 }

 menu
 vars
 connect
 main

这是错误信息:

+ /usr/bin/dzdo -u oseho /bin/ssh -qo PreferredAuthentications=publickey root@oses4101 /u/bin/psg defunct '|' wc -l -ge 1
wc: illegal option -- g
usage: wc [-c | -m | -C] [-lw] [file ...]

似乎认为我使用-ge 是我使用wc 的一部分。但是,当我尝试纠正它时,它不起作用。

问题在于底部的 if 语句。

【问题讨论】:

    标签: linux bash shell unix scripting


    【解决方案1】:

    psg defunct | wc -l 的输出放入一个文件,然后使用test 中的文件工作:

    [ $(cat outputfile) -ge 1]
    

    以这种方式允许输出文件为数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多