【问题标题】:Command that allows re-checking TRUE/FALSE允许重新检查 TRUE/FALSE 的命令
【发布时间】:2016-06-22 05:29:17
【问题描述】:

如何编写允许“无限循环”检查互联网连接是否为 TRUE 的命令(函数),否则等待,然后再次检查等等......

这是我的意思:

havingIP <- function() { if (.Platform$OS.type == "windows") {
ipmessage <- system("ipconfig", intern = TRUE) } else { 
ipmessage <- system("ifconfig", intern = TRUE) }
validIP <- "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
any(grep(validIP, ipmessage)) }

上述解决方案的来源和功劳在这里:

How to determine if you have an internet connection in R

if(havingIP()){ source(....) } else { for(i in 1:5) { Sys.sleep(1); cat(i) }}  

类似这样的东西,但这不合适,因为我只想执行命令source 一次。

while(TRUE){ 
if(havingIP()){ print("working") } else { for(i in 1:5) { Sys.sleep(1);  
cat(i) }}  
}

那么如何在没有loop 的情况下运行它,它将每 5 秒检查一次,如果互联网连接未打开,请再等待 5 秒,依此类推,直到互联网开启,然后只执行一次 source,仅此而已。

对不起,我试图搜索此解决方案,我确定有人问过类似的问题,但找不到任何东西,因为我不知道如何搜索它。谢谢!

【问题讨论】:

    标签: r


    【解决方案1】:

    您可能正在寻找break

    while (TRUE) {
        if (havingIP()) {
            print("working") # execute what you want here
            break            # and if we ever reach here, then exit the while loop
        } else {
            for (i in 1:5) {
                Sys.sleep(1)
                cat(i)
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      李的回答更简单:

      while(!havingIP()) for(i in 1:5) {Sys.sleep(1); cat(i)}
      source(...)
      

      这将暂停执行,直到 havingIP 返回 TRUE。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 2018-07-09
        • 2020-07-18
        • 2022-11-26
        • 2015-09-06
        相关资源
        最近更新 更多