Bash 每个真实条件执行一次命令(&s);等待条件返回false,然后再次返回true

Bash 每个真实条件执行一次命令(&s);等待条件返回false,然后再次返回true,bash,shell,loops,networking,conditional-statements,Bash,Shell,Loops,Networking,Conditional Statements,我问了一个类似的问题(你可能想检查一下),但没有得到答案,所以我将尝试将其简化为lil #/bin/sh . /etc/ssh/autossh.conf if [[ $(ifconfig pdp_ip0 |grep inet) && ! $(ifconfig en0 |grep inet) ]]; then export NETTEST1=1; echo true else export NETTEST1=0; echo false fi if [[ ! $(ifconfi

我问了一个类似的问题(你可能想检查一下),但没有得到答案,所以我将尝试将其简化为lil

#/bin/sh

. /etc/ssh/autossh.conf

if [[ $(ifconfig pdp_ip0 |grep inet) && ! $(ifconfig en0 |grep inet) ]];
then
export NETTEST1=1; echo true
else
export NETTEST1=0; echo false
fi

if [[ ! $(ifconfig pdp_ip0 |grep inet) && $(ifconfig en0 |grep inet) ]];
then
export NETTEST2=1; echo true
else
export NETTEST2=0; echo false
fi

while
test $NETTEST1 -eq 1;
do
echo true
done

while
test $NETTEST2 -eq 1;
do
echo true
done
这段代码的问题在于,当条件为真时,它将继续执行命令。我需要它只在每个true语句中执行一次,然后等待语句为false,然后再次执行true。我一直在为这件事绞尽脑汁&我想知道这是否有可能。任何帮助都会得到感激

我应该这样做:

    while true; do
    while ifconfig pdp_ip0 |grep inet && ! ifconfig en0 |grep inet;
    do sleep 1; done
    while ! ifconfig pdp_ip0 |grep inet && ifconfig en0 |grep inet; do sleep 1; done
    killall autossh; start-autossh
    done;

我真的不明白你想干什么。像这样的

do_test_1(){
{ifconfig pdp|u ip0 | grep-q inet;}&&
{!ifconfig en0 | grep-q inet;}
}
如果进行测试,则测试1;然后
回显“真一次”
虽然进行测试1;做
睡60#或适量
完成
回声“再次真实”
fi

我真的不明白你想做什么。像这样的

do_test_1(){
{ifconfig pdp|u ip0 | grep-q inet;}&&
{!ifconfig en0 | grep-q inet;}
}
如果进行测试,则测试1;然后
回显“真一次”
虽然进行测试1;做
睡60#或适量
完成
回声“再次真实”
fi

您试图做的是在“边缘转换”上触发一些命令;也就是说,条件从false到true的转换。您可以这样做(省略条件和操作的细节):

假设条件(可以是任何管道)不会在不到一秒钟的时间内来回翻转(或者如果发生这种情况,您不介意错过操作),只要条件从失败变为成功,这应该可以做
做您需要做的事情

这一警告是人们对“边缘过渡”持怀疑态度的原因之一。虽然一秒钟的阈值对于网络上下变化似乎是合适的,这通常至少需要几秒钟,但您还需要考虑这样一个问题,即如果设备在负载(或睡眠)下运行,脚本可能不会经常调度。但总比什么都没有好

为清楚起见,我认为您遇到的问题稍微复杂一些,因此这里有一个稍微明确一些的解决方案:

# Do this forever
while true; do
  # Figure out which interface is up (if any) at the beginning
  if ifconfig en0 | grep -q inet; then IF=en0
    elif ifconfig pdp_ip0 | grep -q inet; then IF=pdp_ip0
    else IF=
  fi

  # Some interface should be up at this point, but if not,
  # there is no point waiting for it to go down.  
  if [[ $IF ]]; then
    while ifconfig $IF | grep -q inet; do sleep 1; done
  fi;
  # Now whatever was up is down. Wait for something to come up.
  while ! ifconfig en0 | grep -q inet && ! ifconfig pdp_ip0 | grep -q inet; do
    sleep 1
  done

  # Some interface is up, so do the dirty deed
  killall autossh; start-autossh
  # And go back to waiting for the interface to drop.
done

您试图做的是在“边缘转换”上触发一些命令;也就是说,条件从false到true的转换。您可以这样做(省略条件和操作的细节):

假设条件(可以是任何管道)不会在不到一秒钟的时间内来回翻转(或者如果发生这种情况,您不介意错过操作),只要条件从失败变为成功,这应该可以做
做您需要做的事情

这一警告是人们对“边缘过渡”持怀疑态度的原因之一。虽然一秒钟的阈值对于网络上下变化似乎是合适的,这通常至少需要几秒钟,但您还需要考虑这样一个问题,即如果设备在负载(或睡眠)下运行,脚本可能不会经常调度。但总比什么都没有好

为清楚起见,我认为您遇到的问题稍微复杂一些,因此这里有一个稍微明确一些的解决方案:

# Do this forever
while true; do
  # Figure out which interface is up (if any) at the beginning
  if ifconfig en0 | grep -q inet; then IF=en0
    elif ifconfig pdp_ip0 | grep -q inet; then IF=pdp_ip0
    else IF=
  fi

  # Some interface should be up at this point, but if not,
  # there is no point waiting for it to go down.  
  if [[ $IF ]]; then
    while ifconfig $IF | grep -q inet; do sleep 1; done
  fi;
  # Now whatever was up is down. Wait for something to come up.
  while ! ifconfig en0 | grep -q inet && ! ifconfig pdp_ip0 | grep -q inet; do
    sleep 1
  done

  # Some interface is up, so do the dirty deed
  killall autossh; start-autossh
  # And go back to waiting for the interface to drop.
done

ifconfig
命令在
if
语句中执行一次(且仅执行一次)。然后脚本将退出(如果NETTEST1和NETTEST2都为零)或永远循环(如果NETTEST1或NETTEST2都为1)。是的,但它需要不断检查接口的条件。其中echo true位于while循环id put killall和我正在执行的另一个脚本上。如果我这样做,它将继续不必要地扼杀我的脚本。与这个问题(我不太理解)相关的是:
If[!$(ifconfig pdp|u ip0 | grep inet)&&$(ifconfig en0 | grep inet)]是不必要的复杂(尽管它可以工作)。您可以更简单地编写:
if!ifconfig pdp|u ip0 | grep-q inet和ifconfig en0 | grep-q inet;然后…
这确实是一个复杂的问题,我正在努力解决。我需要检查脚本在其中一个接口关闭时杀死一个二进制文件,然后重新启动它。问题是,这是一件每件事都需要做一次的事情。这就是为什么我需要等待false返回,然后再执行true。这与我的网络接口被关闭和AutoSH不知道如何切换到活动接口有关。@Geofferey:iOS是否有
/etc/network/if up.d
/etc/network/if down.d
脚本?这将是最简单的解决方案。
ifconfig
命令在
if
语句中执行一次(并且仅执行一次)。然后脚本将退出(如果NETTEST1和NETTEST2都为零)或永远循环(如果NETTEST1或NETTEST2都为1)。是的,但它需要不断检查接口的条件。其中echo true位于while循环id put killall和我正在执行的另一个脚本上。如果我这样做,它将继续不必要地扼杀我的脚本。与这个问题(我不太理解)相关的是:
If[!$(ifconfig pdp|u ip0 | grep inet)&&$(ifconfig en0 | grep inet)]是不必要的复杂(尽管它可以工作)。您可以更简单地编写:
if!ifconfig pdp|u ip0 | grep-q inet和ifconfig en0 | grep-q inet;然后…
这确实是一个复杂的问题,我正在努力解决。我需要检查脚本在其中一个接口关闭时杀死一个二进制文件,然后重新启动它。问题是,这是一件每件事都需要做一次的事情。这就是为什么我需要等待false返回,然后再执行true。这与我的网络接口被关闭和AutoSh no有关