bash 检查命令响应是否不包含 str

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17020823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:38:44  来源:igfitidea点击:

Checking if command response does not contain str

bashstring-comparison

提问by WASasquatch

I have a script which works great... but I'd like it not do anything if there is no one online. I looked over all the documentation on how to check if a str is not within a variable, but it always thows the same error, which I assume may be something else, but literally nothing else is touched besides the addition of the fi at the bottom for the new if statement to check if there is no users online...

我有一个很好用的脚本……但如果没有人在线,我不希望它做任何事情。我查看了有关如何检查 str 是否不在变量内的所有文档,但它总是会出现相同的错误,我认为这可能是其他错误,但实际上除了在底部添加 fi 之外没有触及其他任何内容对于新的 if 语句来检查是否没有用户在线...

Error: startAnnouncement.sh: 44: [[: not found

错误: startAnnouncement.sh: 44: [[: not found

#!/bin/bash

checkServer=$(/etc/init.d/minecraft status)
checkUsers=$(/etc/init.d/minecraft command list)
cd /.smc

# Is the server even running?
if [[ $checkServer =~ "is running" ]]; then

    #Is anyone online to even see the message...?
    if [[ ! $checkUsers =~ "are 0 out" ]]; then

       # No count file? Create it.
        if [ ! -f /.smc/lastAnnouncement.txt ]; then
            echo 0 > /.smc/lastAnnouncement.txt
        fi

        # Load count
        lastAnn=$(cat /.smc/lastAnnouncement.txt)

        # ANNOUNCEMENTS
        announcement[0]='Dont  forget to check out http://fb.com/pyrexiacraftfans for news and updates'
        announcement[1]='Use our Facebook page to request land protection! Visit http://fb.com/pyrexiacraftfans'
        announcement[2]='Stay tuned for the announcement of our spawn build competition soon on our Facebook page!'
        announcement[3]='We have upgraded the server with mcMMO and Essentials Economy! Stay tuned for shop openings!'

        announcementText=${announcement[$lastAnn]}

        # Send announcement
        sendAnnouncement=$(/etc/init.d/minecraft command say $announcementText)

        # Next announcement count
        ((++lastAnn))

        # Write next announacment count
        # Should we restart announcement que?
        if [ $lastAnn -gt $((${#announcement[@]}-1)) ]; then
                echo 0 > /.smc/lastAnnouncement.txt
        else
                echo $lastAnn > /.smc/lastAnnouncement.txt
        fi

    fi

fi

回答by glenn Hymanman

Your conditions don't actually use regex-special chars,

您的条件实际上并不使用正则表达式特殊字符,

if [[ $checkServer =~ "is running" ]]; then
    if [[ ! $checkUsers =~ "are 0 out" ]]; then

Also, you need to use !~operator on the 2nd condition: you have too many arguments

此外,您需要!~在第二个条件下使用运算符:您的参数太多

So you might as well use glob patterns.

所以你不妨使用 glob 模式。

if [[ $checkServer == *"is running"* ]]; then
    if [[ $checkUsers != *"are 0 out"* ]]; then