Bash 条件表达式在函数内无法按预期工作

发布于 2024-12-11 13:31:48 字数 2268 浏览 0 评论 0原文

我是一个菜鸟,一边学习一边学习,我很困惑。以下代码片段的第 7 行是我遇到问题的地方。基本上,函数内的 if 语句“FUNCTION_SERVER_START”似乎不起作用。当我说“不起作用”时,我的意思是 1 = 1 应该为 true,但不知何故 1 = 1 为 false,但即使表达式被确定为 false,脚本也会跳过“else”。看起来 if 语句,因为它位于函数内部,所以行为不正常。希望我只是做了一些愚蠢的事情,你可以指出这一点。

对于该脚本的一些背景知识,我编写它是为了帮助管理我的世界服务器。我已尝试尽可能简化此处显示的内容,以便您可以看到正在发生的情况,而不会因 Minecraft 特定内容而减慢速度。如果需要,我可以发布完整的脚本。

我无法让变量“$succeed”等于“true”,因为似乎无论如何 “$SCREEN_CHECK_STATUS”等于(1 或 0)if 语句不会执行此操作。

谢谢。

## FUNCTIONS

FUNCTION_SERVER_START() 
{
  FUNCTION_SCREEN_CHECK
  echo "debug: $SCREEN_CHECK_STATUS"

调试:0

  if [ "$SCREEN_CHECK_STATUS" -eq 1 ]
  then
  {
    echo "Starting the server with option 0."
    command_that_starts_server_opt_0
    succeed="true"
  }
  else
  {
    echo "Starting the server with option 1"
    command_that_starts_server_opt_1
    succeed="true"
  }
  fi
  if [ "$succeed" == "true" ]
  then
  {
    echo "debug: Succeeded!"
  }
  else
  {
    echo "debug: Failed!"

调试:失败!

  }
  fi
}

FUNCTION_SCREEN_CHECK()
## sees if the screen session is running already. if it is, $SCREEN_CHECK_STATUS=1, if not, it's 0 by default
{
  SCREEN_CHECK_STATUS="0"
  if [ "$(screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})" ]  ## runs, "screen -ls <screenname> then filters with grep. basically sees if screen session is already running.
  then
  {
    SCREEN_CHECK_STATUS="1"
  }
  else
  {
    echo "debug: screen session IS NOT running"; fi
  }
  fi
}  


## RUNNER

FUNCTION_SERVER_START

编辑:

sh -xv 说声明数组的行上存在语法错误,所以我使用 bash -xv 并得到了这个:

+ FUNCTION_SERVER_START
+ FUNCTION_SCREEN_CHECK
+ SCREEN_CHECK_STATUS=0
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})"
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]}
++ grep delphi
++ screen -ls delphi
+ '[' '' ']'
+ echo 'debug: delphi screen session IS NOT running'
debug: delphi screen session IS NOT running
+ '[' 0 -eq 1 ']'
+ '[' '' == true ']'
+ echo 'debug: Failed!'
debug: Failed!

I'm a noob learning as I go, and I'm stumped. Line 7 of the following snippet of code is where I'm having problems. Basically it seems like the if statement inside the function, "FUNCTION_SERVER_START" is not working. When I say, "is not working," I mean it's as if 1 = 1 should be true, but somehow 1 = 1 is false, but even when the expression is determined to be false, the script is skipping "else". It seems like the if statement, since it's inside a function, is somehow misbehaving. Hopefully I'm just doing something stupid and you can point that out.

For a bit of background on the script, I am writing it to help manage minecraft servers. I've tried to simplify what is shown here as much as possible so you can see what is going on without being slowed down by the minecraft specific stuff. I can post the full script if need be.

I can't get the variable, "$succeed" to equal, "true" because it seems that no matter what
"$SCREEN_CHECK_STATUS" equals, (1 or 0) the if statement won't do it's thing.

Thank you.

## FUNCTIONS

FUNCTION_SERVER_START() 
{
  FUNCTION_SCREEN_CHECK
  echo "debug: $SCREEN_CHECK_STATUS"

debug: 0

  if [ "$SCREEN_CHECK_STATUS" -eq 1 ]
  then
  {
    echo "Starting the server with option 0."
    command_that_starts_server_opt_0
    succeed="true"
  }
  else
  {
    echo "Starting the server with option 1"
    command_that_starts_server_opt_1
    succeed="true"
  }
  fi
  if [ "$succeed" == "true" ]
  then
  {
    echo "debug: Succeeded!"
  }
  else
  {
    echo "debug: Failed!"

debug: Failed!

  }
  fi
}

FUNCTION_SCREEN_CHECK()
## sees if the screen session is running already. if it is, $SCREEN_CHECK_STATUS=1, if not, it's 0 by default
{
  SCREEN_CHECK_STATUS="0"
  if [ "$(screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})" ]  ## runs, "screen -ls <screenname> then filters with grep. basically sees if screen session is already running.
  then
  {
    SCREEN_CHECK_STATUS="1"
  }
  else
  {
    echo "debug: screen session IS NOT running"; fi
  }
  fi
}  


## RUNNER

FUNCTION_SERVER_START

EDIT:

sh -xv said there were syntax errors on lines that declared an array, so I used bash -xv and got this:

+ FUNCTION_SERVER_START
+ FUNCTION_SCREEN_CHECK
+ SCREEN_CHECK_STATUS=0
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})"
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]}
++ grep delphi
++ screen -ls delphi
+ '[' '' ']'
+ echo 'debug: delphi screen session IS NOT running'
debug: delphi screen session IS NOT running
+ '[' 0 -eq 1 ']'
+ '[' '' == true ']'
+ echo 'debug: Failed!'
debug: Failed!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-12-18 13:31:48

傻了,傻了我。我应该发布完整的、未经编辑的脚本片段。只有这样,真正的问题才会显现出来:

FUNCTION_SERVER_START() 
## starts the minecraft server in screen.
## it uses FUNCTION_SCREEN_CHECK to see if a screen session is already running with the server's designated screen name.
## there are two ways of starting the server. one is sending the mc start string (ex: "java -Xmx512 -Xms512 -jar minecraft_server.jar")...
## ...to the page 0 of the screen session already running, and the other way is to execute the same command in a new screen session.
{
  echo "test phase: can a funky call a phunky?"
  FUNCTION_SCREEN_CHECK
  if [ $DEBUG_MODE -eq 1 ]; then echo "debug: \$SCREEN_CHECK_STATUS = $SCREEN_CHECK_STATUS"; fi
  if [ $SCREEN_CHECK_STATUS -eq 1 ]
  then
  {
    if [ $DEBUG_MODE -eq 1 ]; then echo "Starting the ${SCREEN_NAME[$SERVER_SELECTED]} minecraft server assuming screen is running."; fi
    screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff $(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"
    sleep 5
    succeed="true"
  }
  else
  {
    if [ $DEBUG_MODE -eq 1 ]; then echo "Starting the ${SCREEN_NAME[$SERVER_SELECTED]} minecraft server assuming screen is not running."; fi
    screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}"
    sleep 5
    succeed="true"
  }
  fi
  if [ "$succeed" == "true" ]
  then
  {
    echo "\$\$\$\$\$\$\$\$\$\$\$\$\$ test phase: passed the func serv start if"
  }
  else
  {
    echo "****** test phase: failed the func serv start if"
  }
  fi
}

第 14 行缺少双引号,而第 21 行多了一个双引号。

第 14 行错误:

screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff $(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"

第 14 行正确:

screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff "$(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"

第 21 行错误:

screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}"

第 21 行正确:

screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}

但除了我之外,任何人都不可能知道这一点,因为我没有发布正确的代码,哎呀!

我用nano编写了脚本,只有当我将它带入具有语法突出显示的gedit时,我才发现问题。

谢谢大家的评论。

Silly, silly me. I should have posted the full, non-edited script snippet. Only then would the real problem be visible:

FUNCTION_SERVER_START() 
## starts the minecraft server in screen.
## it uses FUNCTION_SCREEN_CHECK to see if a screen session is already running with the server's designated screen name.
## there are two ways of starting the server. one is sending the mc start string (ex: "java -Xmx512 -Xms512 -jar minecraft_server.jar")...
## ...to the page 0 of the screen session already running, and the other way is to execute the same command in a new screen session.
{
  echo "test phase: can a funky call a phunky?"
  FUNCTION_SCREEN_CHECK
  if [ $DEBUG_MODE -eq 1 ]; then echo "debug: \$SCREEN_CHECK_STATUS = $SCREEN_CHECK_STATUS"; fi
  if [ $SCREEN_CHECK_STATUS -eq 1 ]
  then
  {
    if [ $DEBUG_MODE -eq 1 ]; then echo "Starting the ${SCREEN_NAME[$SERVER_SELECTED]} minecraft server assuming screen is running."; fi
    screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff $(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"
    sleep 5
    succeed="true"
  }
  else
  {
    if [ $DEBUG_MODE -eq 1 ]; then echo "Starting the ${SCREEN_NAME[$SERVER_SELECTED]} minecraft server assuming screen is not running."; fi
    screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}"
    sleep 5
    succeed="true"
  }
  fi
  if [ "$succeed" == "true" ]
  then
  {
    echo "\$\$\$\$\$\$\$\$\$\$\$\$\$ test phase: passed the func serv start if"
  }
  else
  {
    echo "****** test phase: failed the func serv start if"
  }
  fi
}

Line 14 is missing a doublequote, and Line 21 has an extra doublequote.

Line 14 wrong:

screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff $(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"

Line 14 right:

screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff "$(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"

Line 21 wrong:

screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}"

Line 21 right:

screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}

But it was impossible for anyone but me to know that because I didn't post the right code, Arrrgh!

I wrote the script in nano, and only when I brought it into gedit which has syntax highlighting did I see the problems.

Thank you everyone for your comments.

揽月 2024-12-18 13:31:48

在这一行的末尾有一个杂散的 fi:

echo "debug: screen session IS NOT running"; fi

您也不需要在 if-else 主体周围放置花括号。

You have a stray fi at the end of this line:

echo "debug: screen session IS NOT running"; fi

You also don't need the curly braces you are putting around your if-else bodies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文