我如何遵循命令的回复来使新命令起作用?

我如何遵循命令的回复来使新命令起作用?

我正在尝试和测试一些将安装正确版本的守护进程的脚本,但首先在代码中我想检查它是否真正运行并检查守护进程的版本(然后将强制下载并更新它)。但我一开始就卡住了。

#!/bin/bash

RED='\033[0;91m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'`

#Checking Server status
/usr/local/bin/evos-cli getinfo
if [[ "/usr/local/bin/evos-cli getinfo" = "error: couldn't connect to server" ]];
then
    echo -e "${GREEN}I will start Evos server then i check for version... ${NC}"
    evosd
    sleep 10s 
else
    echo -e "${GREEN} Server is running i check for the version soon! ${NC}"
    sleep 2s
fi

#Version Checking
/usr/local/bin/evos-cli --version
if [[ "/usr/local/bin/evos-cli --version" = "Evos Core RPC client version v1.1.3.0-2ba4298" ]];
then
    echo -e "${GREEN} Version of wallet is correct! ${NC}"
else
    echo -e "${YELLOW} Version of wallet is Incorrect! ${NC}"
fi

#Checked Status
sleep 3s
echo -e "${YELLOW} Status Checked! ${NC}"

守护进程上有许多命令可用,如果服务器停止并且您键入任何命令,您将收到“错误:无法连接到服务器”这就是为什么我想else在“错误:无法连接到服务器”没有发生时触发选项。即使服务器停止其触发else命令,该命令也会跳过启动服务器进程并说服务器正在运行。else即使版本与提供的命令匹配,版本检查器也会触发选项。当我使用此代码时,该版本触发了正确的版本(但代码是无用的,因为那里的任何内容都会说正确)

/usr/local/bin/evos-cli --version
if [ "/usr/local/bin/evos-cli --version Evos Core RPC client version v1.1.3.0-2ba4298" ]; 
then
    echo -e "${GREEN} Version of wallet is correct! ${NC}"
else
    echo -e "${YELLOW} Version of wallet is Incorrect! ${NC}"
fi

#Checked Status
sleep 3s
echo -e "${YELLOW} Status Checked! ${NC}"

evos-cli getinfo,或/usr/local/bin/evos-cli getinfo如何在终端中触发命令的示例。

尝试了很多变体,但我无法弄清楚。

答案1

在测试中

if [[ "/usr/local/bin/evos-cli getinfo" = "error: couldn't connect to server" ]];

你似乎想比较输出/usr/local/bin/evos-cli getinfo一个字符串。你什么大概在这个特定的测试中要做的是测试命令是否成功。

这将通过

if /usr/local/bin/evos-cli getinfo >/dev/null 2>&1; then
    # this worked
else
    # the command failed
fi

要比较命令的输出,请使用命令替换:

if [ "$(/usr/local/bin/evos-cli --version)" = "Evos Core RPC client version v1.1.3.0-2ba4298" ]; then
    # version was the expected string
else
    # we got an unknown version
fi

或者,如果您只想匹配该字符串的第一部分,请使用bash,

if [[ "$(/usr/local/bin/evos-cli --version)" == "Evos Core RPC client version v1.1.3.0"* ]]; then
    # version was the expected string
else
    # we got an unknown version
fi

相关内容