Find the answer to your Linux question:
Results 1 to 3 of 3
I am stroirng following output to the file(abc.tmp) : Command := wget Google [B]output[=/B] --12:01:47-- Google => `index.html.2' Connecting to 192.168.2.63:8214... connected. Proxy request sent, awaiting response... 407 Proxy Authentication ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    1

    Grep with regular expression

    I am stroirng following output to the file(abc.tmp) :
    Command := wget Google

    [B]output[=/B]
    --12:01:47-- Google
    => `index.html.2'
    Connecting to 192.168.2.63:8214... connected.
    Proxy request sent, awaiting response... 407 Proxy Authentication Required
    12:01:47 ERROR 407: Proxy Authentication Required.

    I want to write shell script to grep the file and check if it contains 407 Proxy Authentication Required
    How do i do that?

    Thanks in advance for ur help

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Grep in quiet mode. A return code of zero means grep found it.

    Code:
    grep -q '407 Proxy Authentication Required' abc.tmp
    if [ $? -eq 0 ]; then echo "Found it"; fi

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    A more elegant (and more common) way of writing vsemaska's solution would be:
    Code:
    if grep -q '407 Proxy Authentication Required'; then
        echo "Found it"
    fi
    if's condition evaluates to true if the program returns a 0 (successful) exit status. The "if [ ... ]" notation is actually a hidden call to a program called test, which treats its arguments as a more standard boolean arithmetic (1 = true, 0 = false, etc.) and returns success if true and failure otherwise.

    grep's "-q" option means "Instead of printing out lines, just check if there's a match. If there is, return success; otherwise return failure".

    I hope this helps.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...