The frustrating system () function in awk _linux the shell

Source: Internet
Author: User
Tags curl

Find a special embarrassing fact. I worked hard to Baidu data, want to use rewrite to achieve different domain name source station after the failure of automatic jump function, but the whole idea of missing a serious problem.

In my mind, make a curl of the URL for the request, and then rewrite the URL or output based on http_code-which means that every request, squid is back to the source to fetch the header once. So for the source station, front squid cache rate, is 0%! No effect at all.

We have to rethink the idea ... Do you want to see the squid source code? Sweat

The spirit of manage to finish the principle, the decision or the original idea of the chicken was finished. According to the Squid Authority Guide Chapter 11, the streaming format passed to the redirector is: URL Ip/fqdn IDENT method, where the FQDN and IDENT are often empty. method, generally, get and post,squid can only cache get data, but not ignore post, because sometimes the post data header is too large, squid may refuse to forward the content, this is not fun.

After clarifying this format (mostly the idea of a cursory ending), I feel like I'm not using either Perl or PHP at all, and simple awk is enough-and, of course, the shell doesn't, because the shell can't handle this flow-like row.

Here's the awk script I wrote in my mind:

Copy Code code as follows:

#!/bin/awk-f
{
if (System ("Curl-o/dev/null-s-W%{http_code}" $) ~/^[2|3]/) {
Print ": $"
} else {
Print ": http://www.baidu.com/"
}
}

But once again, things that make me depressed happen again and again.

First, no matter what I do in {}, the program will print out the results of system ();

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

The test process is as follows:

Copy Code code as follows:

[Rao@localhost ~]$ Echo ' http://www.google.com ' |awk ' {if (200~/^[2|3]/) {print ': ' 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} ' $ ' ~/^[2|3]/) { print ': ' else{print ': http://www.baidu.com/'} '
200:http://www.baidu.com/

After thinking, in Baidu aunt's help, finally figuring out a problem: the system () results are returned directly to the shell display, and then the awk continues to execute the following program, in which case () left is actually the system () execution state "namely 0 or 1" "0" ~/^ [2 3]/, of course, has been executing else.

The bad question is the getline of awk, which cannot directly import the execution results of system () into Awk's variables ... Unless I first system > a file, and then getline< this file. mygod!

And if you take the execution of while ("Curl" getline var), how to pass the shell variable into the problem ... Alas

Related Article

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.