Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

No command window output when running script

by JennieInMI (Acolyte)
on Dec 29, 2012 at 06:23 UTC ( #1010796=perlquestion: print w/ replies, xml ) Need Help??
JennieInMI has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks, My code is actually doing what I want it to, but I'm curious about some unexpected behavior so I am here to ask those who might know. :-)

My script sends a system command to TFS to attempt to extract a build label in order to assess if that build has been baselined. This is my mock-up to test the code:

$tf="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\I +DE\\tf.exe"; $buildLabel = "the build label"; $/ = undef; BuildCheckedInToTFS(); sub BuildCheckedInToTFS(){ my $checkedIn = 0; print "Verifying build label exists in TFS...\n"; `\"$tf\" labels /owner:theOwner \"$buildLabel\" > "TFSLabelInfo.t +xt"`; open TFSLABEL, "TFSLabelInfo.txt" or die $!; my $TFSLabel = <TFSLABEL>; close TFSLABEL; if ($TFSLabel =~ /$buildLabel/){ $checkedIn = 1; print "Build $buildLabel has been checked-in to TFS."; } else { print "Baselining failed. \n:" + "Build $buildLabel has not been checked-in to TFS."; } return $checkedIn; }

For those who are curious, I use the returned value in another portion of the script. Anywho, it is doing just what I want- it reports whether or not the build label in question is present. This is just an audit script so that's all I need. However, when I was just testing the tf command and all I had was this

$tf="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\I +DE\\tf.exe"; $buildLabel = "the build label"; `\"$tf\" labels /owner:theOwner \"$buildLabel\"`;
in my script it produced no apparent result. When I would run the script there would be a pause as though some work were taking place, then the command prompt would simply reappear and there would be no output on the screen. I figured it was checking the mapped drive so I added the redirection to see if something would come of that. As soon as I did, the text file appeared and I got the expected result (a listing of the labels matching the specs). What I don't understand is why I get no output in the command window when I run the tf command via my Perl script. When I simply run the command at the command prompt I get the expected output (a listing of the labels matching the specs) in the window, but when I run it via my script I get nada in the command window. I even tried putting in an echo command and still the same thing - a pause followed by the command prompt. I would expect nothing to show in the window if the output is being redirected, but I don't get why it is happening when the output is not redirected.

This doesn't matter in terms of the function of this script, but I'm curious to know the reason why. I am new to Perl and perhaps what you might call an advanced beginner at programming, so if you need to get very technical a link to a detailed explanation would really be welcome. Thanks in advance!

Comment on No command window output when running script
Select or Download Code
Re: No command window output when running script
by syphilis (Canon) on Dec 29, 2012 at 09:11 UTC
    `\"$tf\" labels /owner:theOwner \"$buildLabel\"`;

    I think it's just that backticks returns the output, rather than prints the output ... and since you haven't collected what was returned, it just gets lost.
    By way of demonstration:
    C:\_32\C>cd C:\_32\C C:\_32\C>perl -e "`cd`" C:\_32\C>perl -e "$x=`cd`;print $x" C:\_32\C
    Cheers,
    Rob

      Hi Rob,

      Thank you for this reply. It makes perfect sense. I wasn't storing what was returned and printing it, so nothing was output to the screen. Returning and outputting are not inherently equivalent.

      I just tried out your example. Cd doesn't return a value so nothing is output with this example, but when I used dir instead I got a nice screenful of info. :-D I'm not on my work computer right now so I can't try out my tf command yet, but I will give it a try later. Thank you again for the answer.

      Jennie

        Cd doesn't return a value so nothing is output with this example

        Interesting ... and there's probably a good reason for that, but I don't know what it is.
        Certainly, the behaviour you're seeing doesn't conform to the documentation I get for cd, which begins with:
        C:\_32\C>cd /? Displays the name of or changes the current directory. CHDIR [/D] [drive:][path] CHDIR [..] CD [/D] [drive:][path] CD [..] .. Specifies that you want to change to the parent directory. Type CD drive: to display the current directory in the specified drive +. Type CD without parameters to display the current drive and directory. .....
        Perhaps you're running in a different shell (I use cmd.exe), or perhaps your cmd.exe shell finds a different 'cd' implementation to run.
        To force your cmd.exe shell to use its own implementation, I think you just need to involve a shell metacharacter in the command - eg:
        cd <nul
        Does that command print out the current directory for you ?

        (Admittedly this has nothing to do with the subject you initially raised ... but I find it a little bit interesting.)

        Cheers,
        Rob

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-23 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (134 votes), past polls