简体   繁体   English

如何在Shell脚本中将uniq命令的输出分配给变量

[英]How to assign the output of uniq command to a variable in shell script

I am writing a script to check the status of WAS server to confirm if the server is STARTED or FAILED to start. 我正在编写脚本来检查WAS服务器的状态,以确认服务器是否已启动或启动失败。 The server has 2 JVMs. 该服务器有2个JVM。 So to check that either of the JVMs are up or not I am using the uniq command. 因此,要检查两个JVM是否已启动,我正在使用uniq命令。

Lets say JVM1 got FAILED and JVM2 is STARTED, so the below command 可以说JVM1失败了,而JVM2已启动,所以下面的命令

sh /home/wasprofile/`hostname`/bin/serverStatus.sh -all > /tmp/ServerState

grep "Application Server" /tmp/ServerState|awk '{print $7}'|uniq

will show the output as: 将输出显示为:

FAILED
STARTED

So now how should i assign this output to two different variables in runtime ?? 所以现在我应该如何在运行时将此输出分配给两个不同的变量? I mean something like this: 我的意思是这样的:

a=FAILED
b=STARTED

Any help on this is really appreciated. 对此,我们将给予任何帮助。

You can use read with process substitution: 您可以将read与进程替换一起使用:

read a b < <(awk '/Application Server/ && !seen[$7]++{printf "%s ", $7}' /tmp/ServerState)

PS: You can avoid grep and uniq both using awk. PS:您可以同时使用awk避免grepuniq

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

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