Bash 在Shell脚本中更改if语句中的值

Bash 在Shell脚本中更改if语句中的值,bash,shell,if-statement,Bash,Shell,If Statement,我想在下面的脚本中更改全局变量status的值,但我不知道这有什么问题 status="alive"; for i in (*code here*) do ping -c3 (*host modified by the code*) > /dev/null if [ $? -eq 0 ] then status="alive"; else status="dead"; fi if [ "$status" == "alive" ] then

我想在下面的脚本中更改全局变量status的值,但我不知道这有什么问题

status="alive";
for i in (*code here*)
do 
  ping -c3 (*host modified by the code*) > /dev/null
  if [ $? -eq 0 ]
  then
    status="alive";
  else
    status="dead";
  fi

  if [ "$status" == "alive" ]
  then    
  (*code here*)
  fi
此状态变量用于检查主机是否关闭,如果主机未关闭,则执行ping或其他操作

那么最后一个if语句是否正常工作???请告诉我哪里有错。提前感谢


问题解决了

我是什么?它是如何使用的?传递给ping的主机是什么?i是包含主机列表的文本文件中的主机。当然,它ping所有主机。在执行ping之前,您不会测试主机是活的还是死的。问题是,这是您的最后一个if语句status==alive正确工作吗?是的。非常感谢。那么,你能在这里告诉我错误吗?你所展示的代码部分,包括已完成的工作,没有经过测试的问题。所以,错误一定是在你没有给我们看的地方。使用-x选项运行脚本,以便查看哪里出错。如果所有其他操作都失败:复制并粘贴完整的代码。
status="alive";
for i in (*code here*)
do 
  ping -c3 (*host modified by the code*) > /dev/null
  if [ $? -eq 0 ]
  then
    status="alive";
  else
    status="dead";
  fi

  if [ "$status" == "alive" ]
  then    
    (*code here*)
  fi
done    #<-- this was missing!!