Bash条件表达式在函数内部不能正常工作

时间:2011-10-23 07:57:01

标签: bash

我去的时候是一个菜鸟,我很难过。以下代码片段的第7行是我遇到问题的地方。基本上它看起来像函数中的if语句,“FUNCTION_SERVER_START”不起作用。当我说“不工作”时,我的意思是好像1 = 1应该是真的,但不知何故1 = 1是假的,但即使表达式被确定为假,脚本也会跳过“else”。似乎if语句,因为它在函数内部,在某种程度上是行为不端。希望我只是做一些愚蠢的事情,你可以指出这一点。

关于脚本的一些背景知识,我正在编写它以帮助管理minecraft服务器。我试图尽可能地简化这里显示的内容,这样你就可以看到正在发生的事情,而不会被特定于我的特定东西放慢速度。如果需要,我可以发布完整的脚本。

我无法得到变量,“$ success”等于“true”,因为它似乎无论如何 “$ SCREEN_CHECK_STATUS”等于,(1或0)if语句不会这样做。

谢谢。

## 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:失败!

  }
  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!

2 个答案:

答案 0 :(得分:3)

傻,傻,我。我应该发布完整的,未经编辑的脚本片段。只有这样才能看到真正的问题:

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]}

但除了我以外,任何人都不可能知道,因为我没有发布正确的代码,Arrrgh!

我用nano编写了这个脚本,只有当我把它带到带有语法高亮的gedit时才能看到问题。

感谢大家的评论。

答案 1 :(得分:1)

你在这一行的末尾有一个迷路:

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

你也不需要围绕if-else身体的花括号。