Depressing system () function in awk

Source: Internet
Author: User

Discover a very embarrassing fact. I tried my best to go to Baidu materials and wanted to use rewrite to implement the automatic jump function after the source site of different domain names fails. However, I missed a serious problem in the whole idea.

According to my idea, perform a curl for the requested url, and then rewrite the url Based on http_code or output as is -- this means that each request, squid, will return to the source to retrieve the header once. For the origin site, the cache rate of the front squid is 0%! It has no effect at all.

You have to try again ...... Look at the squid source code? Khan

Based on the principle of beginning, end, beginning, and end, I decided to write down the old chicken rib idea. According to chapter 11 of squid's authoritative guide, the stream format passed to the redirector is: url ip/fqdn ident method, where FQDN and ident are often empty. METHOD, usually GET and POST. squid can only cache GET data, but cannot ignore the post method. Sometimes, if the POST data header is too large, squid may refuse to forward the content, this is not fun.

After clarifying the format (mainly influenced by the idea of ending the grass and grass), I thought it was not necessary to use perl or php at all. Simple awk would be enough-of course, shell would not work, because shell cannot process such stream-like rows.

The following is an awk script written in my mind:

Copy codeThe Code is as follows :#! /Bin/awk-f
{
If (system ("curl-o/dev/null-s-w % {http_code}" $1 )~ /^ [2 | 3]/) {
Print ": $1"
} Else {
Print ": http://www.baidu.com /"
}
}

But once again, the depressing thing happened to me.

First, no matter what operations I perform in {}, the program prints the system () result;

Second, even if the result of system () is 200, print is also the "http://www.baidu.com" of else {}; and if I test if (200 ~ /^ [2 3]/) {} else {}. The result is normal!

The test process is as follows:

Copy codeThe Code is as follows: [rao @ localhost ~] $ Echo "http://www.google.com" | awk '{if (200 ~ /^ [2 | 3]/) {print ":" $1} else {print ": http://www.baidu.com /"}}'
: Http://www.google.com
[Rao @ localhost ~] $ Echo "http://www.google.com" | awk '{if (system ("curl-o/dev/null-s-w % {http_code}" $1 )~ /^ [2 | 3]/) {print ":" $1} else {print ": http://www.baidu.com /"}}'
200: http://www.baidu.com/

After thinking about it, with the help of Baidu's big brother, I finally figured out the problem: the result of system () is directly returned to the shell, and then awk continues to execute the subsequent program, in this case, if () actually leaves the execution status of system () [0 or 1] "0 "~ /^ [2 3]/. Of course, else is always executed.

The bad problem is that awk's getline cannot directly import the execution result of system () into awk's variable... Unless I first> a file in system, and then getline <this file. MyGod!

If the while ("curl" getline var) execution method is used, how to pass the shell variable in becomes a problem ...... Alas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.