Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PERL question (help needed)

Options
  • 07-07-2005 2:58pm
    #1
    Registered Users Posts: 2,297 ✭✭✭




    Hi

    I am writing a wee perl script which will do the following on UNIX:

    1. ipcs -opq | grep q # grab the message queues.
    2. extract the hex value i.e. the 3rd field.
    3. convert the hex value to decimal.
    4. then use the decimal value as lookup in a c-header file (for some application).

    I am doing part 1 as follows:

    open(PIPE, "ipcs -opq | grep q |") or die "Can't run program: $!\n";
    while(<PIPE>) {
    # HELP NEEDED HERE!!
    }

    How do I extract the hex value? I did try the following but it did not work for me:
    open(PIPE, "ipcs -opq | grep q | awk '{print $3}' |")

    Can somebody please enlighten me?

    Thanks - laoisfan


Comments

  • Registered Users Posts: 1,865 ✭✭✭Syth


    Regular expressions.

    Use a regulat expression to parse each line to get the hex value. Not sure about the hex -> dec, I'm sure there's a function to do it. Then just open the c-header file and use more regex goodness to do the lookup.


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Can you provide an example of the output from "ipcs -opq" ? I should be able to help you then.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Here is the output from the command run on a Solaris 9 box:

    $ ipcs -opq
    IPC status from <running system> as of Thu Jul  7 18:10:51 BST 2005
    T         ID      KEY        MODE        OWNER    GROUP CBYTES  QNUM LSPID LRPIDMessage Queues:
    q          0   0x61004aa5 --rw-------     root     root     13     1   527   527
    

    I would have thought you could do something like, in shell...
    # Backquotes around command
    for f in `ipcs -opq | grep "^q" | awk '{print $3}'`
    do
        grep "$f" /dir/to/headers
    done
    


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    #!/usr/bin/perl -w
    
    use strict;
    
    my @values;
    
    while (<DATA>) {
      if (/^q/) {
        my @s = split /\s+/;
        push @values, hex($s[2]);
      }
    }
    
    foreach (@values) {
    	print "$_\n";
    }
    
    __DATA__
    
    T         ID      KEY        MODE        OWNER    GROUP CBYTES  QNUM LSPID LRPIDMessage Queues:
    q          0   0x61004aa5 --rw-------     root     root     13     1   527   527
    
    

    No need for your grep now either. Hope that helps.


  • Registered Users Posts: 2,297 ✭✭✭laoisfan


    daymobrew wrote:
    I would have thought you could do something like, in shell...
    # Backquotes around command
    for f in `ipcs -opq | grep "^q" | awk '{print $3}'`
    do
        grep "$f" /dir/to/headers
    done
    


    Hi

    Yes - I was doing that in shell. Howeve, I could not come up with an elegant way of converting the value from hex to decimal in shell scripting. Perl has a one liner way of doing it as follows:

    $dec = sprintf("%d", 0x80);
    

    So I thought I could throw the sprintf in a while loop in perl, convert the hex vale and then do a lookup in the header file.

    If anyone knows of an easy way to convert hex to dec in shell...........

    Thanks.


  • Advertisement
  • Registered Users Posts: 2,297 ✭✭✭laoisfan


    thanks!! nice one!!


  • Registered Users Posts: 2,297 ✭✭✭laoisfan



    #!/usr/bin/perl -w
    
    use strict;
    
    my @values;
    
    open(DATA, "ipcs -opq | grep q |") or die "Can't run program: $!\n";
    while (<DATA>) {
      if (/^q/) {
            my @s = split /\s+/;
            push @values, hex($s[2]);
      }
    }
    
    foreach (@values) {
            print "$_\n";
    }
    
    close DATA;
    

    thanks for all the help guys, much appreciated!!

    I can now add in the bits that will lookup the decimal value in the C-header files.


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    open(DATA, "ipcs -opq | grep q |") or die "Can't run program: $!\n";
    

    Two things:

    1. Don't use DATA as the handle to open(), its reserved for the __DATA__ block (which I was using in my example).
    2. No need for your "| grep q " as the below is doing just that - checking for a q at the beginning of a line.
      if (/^q/) {
    


Advertisement