代码之家  ›  专栏  ›  技术社区  ›  R. Barrett

bash profile中的循环帮助

  •  0
  • R. Barrett  · 技术社区  · 7 年前

    我正在努力找出如何修复bash配置文件中的for循环。 所以这里是我的问题,我有一个名为“command”的别名命令。

    为了简洁起见,我们将称alias 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 回复  |  直到 7 年前
        1
  •  0
  •   miimote    7 年前

    你可以试试这个。杰西的网络测试 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
    
        2
  •  0
  •   R. Barrett    7 年前

    我终于成功了。 很抱歉延迟回复。 我的一个朋友建议使用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