【问题标题】:BASH variable timeout if not aquired in X time如果未在 X 时间内获取,则 BASH 变量超时
【发布时间】:2014-01-27 20:26:07
【问题描述】:

嘿,伙计们,如果变量超过 X 时间试图获取内容,我正在尝试找出一种方法来使变量超时,这是基于触摸服务器以验证它是否具有 SSL。如果服务器在 X 秒内没有响应,我只想将变量设置为空(或者如果可能的话设置一些其他标志)

我用的是

response=$(echo ^D |openssl s_client -connect ${line}:443 2> /dev/null |openssl x509 -noout -hash |grep -E '^[[:xdigit:]]{8}')

现在$linebaidu.com

我试过这样的

( cmdpid=$BASHPID; 
  ( sleep 10; kill $cmdpid; echo -e "\n\t$line missed window, terminating...\n" 
  ) & exec response=$(echo ^D |openssl s_client -connect ${line}:443 2> /dev/null |openssl x509 -noout -hash |grep -E '^[[:xdigit:]]{8}')
)

但意识到了几个问题,例如 A) 我在子 shell 中,无法取出我的变量,B) 我正在尝试运行 response=#hash 并返回错误等

在捕获我的变量时运行超时的最佳方法是什么?

谢谢

【问题讨论】:

    标签: bash pid subshell


    【解决方案1】:
    IFS= read -t 10 -d '' variable  <   <(yourcommand)
    

    例如

    IFS= read -t 10 -d '' response  <   <(echo ^D |openssl s_client -connect ${line}:443 2> /dev/null |openssl x509 -noout -hash |grep -E '^[[:xdigit:]]{8}')
    

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2018-07-03
      • 2022-01-17
      • 2020-12-01
      • 1970-01-01
      相关资源
      最近更新 更多