Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

ClearCase Command text in Backticks is ignored

by Deep_Plaid (Acolyte)
on May 08, 2013 at 15:19 UTC ( [id://1032620]=perlquestion: print w/replies, xml ) Need Help??

Deep_Plaid has asked for the wisdom of the Perl Monks concerning the following question:

Oh Perl Masters. I seeketh wisdom on the following issue: I am running some ClearCase commands with a script that uses a ClearCase format option to format the output, so I don't have to do it with PERL. I'm running the command using Backticks, and appears to be ignoring the format option (i.e., gives the non-formatted output). The command is designed to list all the current checkouts within the context of the current UCM project (i.e., the branch in the current view). Here is the code:

$stream = `cleartool lsstream -s`; $output = `cleartool lsco -r -brt $stream -fmt "%En\t%u\n"`; printf("$output\n");

The first command gets the stream name which is essentially the branch name that is used in the second command. The second command lists all the checkouts on that branch and formats the output so you only see the relative file path and user name. The code works, but it lists the full output instead of the formatted output. For example, if the command is run on a branch that has two checked out files, the expected (i.e., formatted) output is:

.\EDC_Export\Test_file2.txt mkjohnson .\EDC_Export\Test_file3.txt mkjohnson

Instead I am getting the full output:

--05-08T08:02 mkjohnson checkout version ".\EDC_Export\Test_file2.tx +t" from \main\p_JOESDRUGS_DT_Int\1 (reserved) Attached activity: activity:CR0002@\PROD_DT_PVOB "CR0002" --05-08T08:31 mkjohnson checkout version ".\EDC_Export\Test_file3.tx +t" from \main\p_JOESDRUGS_DT_Int\1 (reserved) Attached activity: activity:CR0002@\PROD_DT_PVOB "CR0002"

I would assume this has something to do with escaping - I have tried several different combinations with no effect (e.g., using single quotes instead of double, not escaping the double quotes, etc.). I do know that ClearCase format options are especially cantankerous in a Windows environment, so that might have something to do with it.

I have tried using the system command instead, but that has trouble recognizing the $stream variable - it gets passed with the value of 0. Code:

$output = system("cleartool lsco -r -brt $stream -fmt \"%En\t%u\n\"");

I tried ${stream} but that did not work either. Still 0.

If I enter the actual branch name in place of $stream, it works, but the output is all strung together without end of lines. Code:

$output = system("cleartool lsco -r -brt p_JOESDRUGS_DT_Int -fmt \"%En +\t%u\n\"");

Output:

.\EDC_Export\Test_file2.txt mkjohnson.\EDC_Export\Test_file3.txt + mkjohnson0

And the end of the output has a 0, which is probably telling of my foible. Any assistance would be greatly appreciated. Thank you.

Replies are listed 'Best First'.
Re: ClearCase Command text in Backticks is ignored
by kennethk (Abbot) on May 08, 2013 at 15:30 UTC
    I'm not familiar with this particular tool, so this advice is generic.

    If you are having trouble with escaping on a command line call that you do not need to capture output from, the easiest solution is usually multi-argument system. That way, Perl will handle escaping and quoting for you as necessary. Your call might look like:

    my $output = system('cleartool', 'lsco', '-r', '-brt', $stream, '-fmt' +, '%En\t%u\n');

    It's also possible that the last term there should be something more like "%En\t%u\n", which gets to why I suspect your backtick call is failing; I would guess that the format string passed can't contain literal control characters. Perhaps you'll get your desired result from:

    my $output = `cleartool lsco -r -brt $stream -fmt "%En\\t%u\\n"`;
    or possibly
    my $output = `cleartool lsco -r -brt $stream -fmt '%En\\t%u\\n'`;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Hi Ken,

      Thanks so much for your fast reply. Escaping the back slashes in the format option worked! The output using system does the format properly now. Code:
      $output = system("cleartool lsco -r -brt p_JOESDRUGS_DT_Int -fmt \"%En +\\t%u\\n\"");

      The zero is still at the end, but I can always chop that off. Output:

      .\EDC_Export\Test_file2.txt mkjohnson .\EDC_Export\Test_file3.txt mkjohnson 0

      However, I still haven't solved the first issue. I tried your advice, and this solves the problem of recognizing the content of $stream, but the command fails. I also tried it with including the double quotes escaped:

      $output = system('cleartool', 'lsco', '-r', '-brt', $stream, '-fmt', ' +\"%En\\t%u\\n\"');

      But that still fails. However, a different failure, so I'm getting closer. It's a cleartool error and it reveals it can read $stream. Output:

      cleartool: Warning: Type not found: "p_JOESDRUGS_DT_Int". 0

      So I am getting closer. I will try playing around with this combination, but any further advice would be greatly appreciated. Thanks again.

        The zero is still at the end, but I can always chop that off
        If your code is essentially
        my $output = system(...); print $output;
        then you should note that system returns the status code returned by the call, not the output of the call. There's no reason to do the print, and probably no reason to capture the return.
        I also tried it with including the double quotes escaped
        The question I should have included up front is "What is the exact syntax that works on the command line?" If you pass system '\"%En\\t%u\\n\"', I believe it should be equivalent to entering '\\"%En\\t%u\\n\\"' on the CLI, which I think is unlikely to be what you intend.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: ClearCase Command text in Backticks is ignored
by moritz (Cardinal) on May 08, 2013 at 15:45 UTC

      Hi, Moritz.

      Yes, that was it! It was trailing a newline character! I actually figured this out on my own for once - small miracle! I used chomp() and that took care of it (along with Ken's response). Thank you both!!

Re: ClearCase Command text in Backticks is ignored
by runrig (Abbot) on May 08, 2013 at 16:01 UTC

    Are you sure $stream is set to what you think it is?

    If you don't need to capture output and just want it to go to stdout, use system(@array):

    # Not sure if you are trying to pass literal '\t','\n' or tab and newl +ine characters...assuming tab and newline system(cleartool => 'lsco', '-r', '-brt', $stream, '-fmt', "%En\t%u\n" +);

    If you want to capture stdout, open a file handle to the command:

    open(my $fh, "-|", cleartool => 'lsco', '-r', '-brt', $stream, '-fmt', "%En\t%u\n") or +die "Err: $!"; while (<$fh>) { print "OUTPUT: $_"; } close $fh or die "Error: $!";

      I see what you mean, Ken. I was thinking System passed the output of the command to the variable $output, but I do remember reading now that it passes the error code. I had thought I was printing the output of system, but it was actually passing the error code! I removed the printf and all is good. Thanks again. You guys rock! I'm a newbie, obviously.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1032620]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found