在 Bash 中提取 curl return 的一部分以分配给变量

Extract part of a curl return in Bash to allocate to a variable

我想从 bash 脚本中的 curl 返回的网页中提取字符串值,但不确定如何去做?

我感兴趣的值总是由 curl 返回,如下所示:

    <head>
    <title>UKIPVPN.COM FREE VPN Service</title>
    <style type='text/css'>
      #button {
        width:180px;
        height:60px;
        font-family:verdana,arial,helvetica,sans-serif;
        font-size:20px;
        font-weight: bold;
      }
    </style>
  </head>
  <br>
  <br>
     <font color=blue><center>  <h1>Welcome to Free UK IP VPN Service</h1>               </center></font>

     <form method='post' action='http://www.ukipvpn.com'>
  <center><input type='hidden' name='sessionid' value='4b5q43mhhgl95nsa9v9lg8kac7'></center><br>
  <center><input id='button' type='submit' value='  I AGREE  ' /><br><br>     <h2> Your TOS Let me use the Free VPN Service</h2></center>
     </form>



       <br><center><font size='2'>No illegal activities allowed. In case of abuse, users' VPN access log is subjected to expose to related authorities.</font></center>
       </html>

我想提取到 Bash 中的变量的值是值='this is the value i am interested in'.

感谢您的帮助;

安迪

您可以试试下面的方法。

$ val=$(curl somelink | grep -oP "name='sessionid'[^<>]*\bvalue\s*=\s*'\K[^']*")

some arguments against 使用正则表达式解析 HTML。

这是使用 tidyxmlstarlet 的更强大的基于 XPath 的版本:

var=$(curl someurl | 
  tidy -asxml 2> /dev/null | 
  xmlstarlet sel -t -v '//_:input[@name="sessionid"]/@value' 2> /dev/null);