Find the answer to your Linux question:
Results 1 to 5 of 5
Hi linux users! I'm trying to write a bash script and I'm having trouble with it. I have a list of DNS entires from a file called zoneExport.txt. Than I ...
  1. #1
    Just Joined!
    Join Date
    Oct 2002
    Posts
    3

    Need help with using Grep/Variables in bash script

    Hi linux users! I'm trying to write a bash script and I'm having trouble with it. I have a list of DNS entires from a file called zoneExport.txt. Than I want to parse a log file to see if that DNS entry has been queried for. So I'm running a grep command and trying to save it into a variable. What I'm looking for is a variable ($varGrepQ) that has the number of matches for the grep query. I will then run this through an if statement and do some things from there..

    But my problem right now is with this grep query. It keeps outputting '0' even when I know there are records in that file and when I run the same query on the command line I get the actual count. My thought is that the $record variable is not passing right. Any thoughts?


    Code:
    #!/bin/bash
    varLines="$( cat ~/Desktop/DNSLogs/zoneExport.txt )"
    for record in $varLines; do
    echo $record
    #varGrepQ=`grep -c -i "$record" ~/Desktop/DNSLogs/test.log`
    varGrepQ=$(grep -c -i "$record" ~/Desktop/DNSLogs/test.log)
    echo $varGrepQ;
    done

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Could you post samples of both files ? I have not found errors with test data. zoneExport.txt should have one single word per line for the for loop to be valid in this setting.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Oct 2002
    Posts
    3
    I had to rename test.log to test.log.txt to get it to upload.
    Attached Files Attached Files

  4. #4
    Just Joined!
    Join Date
    Oct 2002
    Posts
    3

    Found it.

    Hi. I think I found my problem.. The file I was pulling the source zone from was windows formatted and so I was pulling these odd newline ^M characters in with my variable and obviously that wasn't present in the other file.

    Thanks!

  5. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    I found it a very tough one and I had no clues at what was wrong. Good to read a logical reason.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

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