Bash配置文件中的For Loop帮助

时间:2018-11-27 19:12:06

标签: bash scripting

我正在竭尽全力找出如何在bash个人资料中修复for循环。 所以这是我的问题,我有一个别名为“ command”的命令,它可以正常工作。

为了简洁起见,我们将使用别名command ='teamviewer'

因此,我试图使bash配置文件通过for循环来检测Internet连接。我能够通过成功的方式来确定何时启动状态以及何时关闭状态。不,我陷于for循环中,并且遇到了使其无法正常工作的问题。 for循环如下

#For-loop begins below
# Variable Assignment and Alias Command for Network Connectivity
# ==============================================================

alias command='teamviewer'
alias inet_state="ip addr show | awk '{print $8,$9}' | awk 'NR >= 7 && NR <=7'"
inetstate-good='$(state UP)'
inetstate-bad='$(state DOWN)'

# Loop for Internet Connection & Start Broadsign
# ==============================================
echo "$(inet_state);
for inet_state in "$(inet_state);
do {
     if inet_state="$(inetstate-bad)";
       then 'sleep 9999999999';
     elif inet_state="$(inetstate-good)";
       command;      
     fi}
done

# ==============================================

任何帮助将不胜感激:D

2 个答案:

答案 0 :(得分:0)

您可以尝试一下。来自Jesse here

的网络测试
# Test for network connection. Taken from Jesse in
# https://stackoverflow.com/questions/929368/how-to-test-an-internet-connection-with-bash

for interface in $(ls /sys/class/net/ | grep -v lo); do
    if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1; fi
done

# Then do the job
if [ $OnLine == 1 ]; then
    teamwiewer
else
    sleep 9999999999
fi

答案 1 :(得分:0)

我终于开始工作了。 回复较晚,抱歉。 我的一个朋友建议使用fping。

while true; do
    if [[ "${inet_state}" = "${inet_state_good}" ]];
            then sleep 10 && <insert_process_name && break
    elif [[ "${inet_state}" = "${inet_state_bad}" ]];
            then echo "Your Player has a technical issue, please call XXXXXXXXXXXXX ext. 150" && sleep 999999999 && sudo pkill <insert_process_name> 
    fi
done