命令grep的输出,并在使用它与QUOT;若"声明中,庆典 [英] Grep output of command and use it in "if" statement, bash

查看:332
本文介绍了命令grep的输出,并在使用它与QUOT;若"声明中,庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这里是另一个有关StarMade服务器。

Okay so here's another one about the StarMade server.

previously我有这个脚本用于检测碰撞,它只会通过日志搜索:

Previously I had this script for detecting a crash, it would simply search through the logs:

#!/bin/bash
cd "$(dirname "$0")"
if ( grep "[SERVER] SERVER SHUTDOWN" log.txt.0); then
sleep 7; kill -9 $(ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}')
fi

这将找到[SERVER]服务器关闭,并杀死之后的过程,但是这不是一个防水的方法,因为不同的错误,它可能是可能的消息未出现,使这个脚本也没用。

It would find "[SERVER] SERVER SHUTDOWN" and kill the process after that, however this is not a waterproof method, because with different errors it could be possible that the message doesn't appear, rendering this script useless.

所以我有这个工具,可以发送命令到服务器,但是当服务器处于崩溃状态返回EOF异常。我基本上要抓住这个命令的输出,并在if语句上面使用它,而不是目前的grep命令,以这样一种方式,它会执行时发现的grepjava.io.EOFException下面的命令

So I have this tool that can send commands to the server, but returns an EOF exception when the server is in a crashed state. I basically want to grab the output of this command, and use it in the if-statement above, instead of the current grep command, in such a way that it would execute the commands below when the grep finds "java.io.EOFException".

我可以使它的输出写入一个文件,然后从那里用grep显示它,但我不知道,是不是还有更好/更有效的方法来做到这一点?

I could make it write the output to a file and then grep it from there, but I wonder, isn't there a better/more efficient method to do this?

编辑:好了,打完了一下搜索我放在一起以下内容:

okay, so after a bit of searching I put together the following:

if ( java -jar /home/starmade/StarMade/StarNet.jar xxxxx xxxxx /chat) 2>&1 > /dev/null |grep java.io.EOFException);

这将是一个有效的if语句?我需要它在第一个命令的输出相匹配java.io.EOFException,如果它匹配,用然后(得到的那部分工作)执行一些东西。

Would this be a valid if-statement? I need it to match "java.io.EOFException" in the output of the first command, and if it matches, to execute something with "then" (got that part working).

推荐答案

不知道解决您的问题,但这一行:

Not sure to solve your problem, but this line:

ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}'

可以更改

ps -aef | awk '/[S]tarMade.jar/ {print $2}'

[S] prevents AWK 从发现本身。

The [S] prevents awk from finding itself.

或者只是像这样获得 PID

pidof StarMade.jar

这篇关于命令grep的输出,并在使用它与QUOT;若"声明中,庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆