我问了一个类似的问题here(你可能想检查)并没有收到答案,所以我要去尝试并简化它。每个真实条件执行一次命令,并等待,直到条件返回假,然后再次为真
#/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
这段代码的问题是它会在条件为真时继续执行命令。我需要它只对每个真实语句执行一次,然后等到语句为假然后再次为真。我一直在打我的头&我想知道如果它甚至possilbe。任何帮助总是感激。
我应该做这样的:
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;
的'ifconfig'命令被执行一次(也是唯一一次)在'if'声明。然后,脚本将退出(如果NETTEST1和NETTEST2都为零)或永远循环(如果NETTEST1或NETTEST2为1)。 – isedev 2014-09-29 23:22:47
是的,但需要不断检查接口的状况。回声真正在while循环id把killall和我正在执行的另一个脚本。如果我这样做,它会不必要地杀死我的脚本。 – Geofferey 2014-09-29 23:41:00
切题(我真的不明白):'如果[[! $(ifconfig pdp_ip0 | grep inet)&& $(ifconfig en0 | grep inet)]];'不必要的复杂(尽管它会起作用)。你可以更简单地写:“如果! ifconfig pdp_ip0 | grep -q inet && ifconfig en0 | grep -q inet;那么......' – rici 2014-09-30 00:04:22