linux - 如何将命令的输出(输出为数字)与数字进行比较?尝试查看输出是否大于或等于 1

标签 linux bash shell unix scripting

我正在尝试运行一个本质上运行 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 语句上。

最佳答案

psg 的输出置为 defunct | wc -l 到一个文件中,然后在 test 中使用该文件工作:

[ $(cat outputfile) -ge 1]

以这种方式允许输出文件是一个数字。

关于linux - 如何将命令的输出(输出为数字)与数字进行比较?尝试查看输出是否大于或等于 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51435945/

相关文章:

c - tcp 缓冲区中的总数据包长度是否可以超过分配的缓冲区大小?

linux - 如何在linux中将变量设置为当前日期和date-1?

linux - 在类 unix 环境中从 shell 获取人类友好的文件大小 [可移植性]

bash - 如何解析 mmcli 输出中的调制解调器编号、标识符和名称?

可访问互联网时用于通知的 Bash 脚本

linux - 带有引号参数的系统用户、smb 用户和 netatalk 用户的 Bash 脚本

linux - 如何使用 ADL 在 Linux 上调试 Adob​​e AIR 应用程序?

linux - ubuntu 和 unix 关于目录和隐藏文件的东西

c - 使用 gdb 跟踪程序

linux - 使用 LInux 的 Bash 命令