Hello All,
I'm new to this forum. Sorry for not providing enough info.
Below is my approach in perl (first I tried this in python). Reading the commands from a file "Build_Cmd_List.txt" (which contains arbitrary commands generated during build.) And then executing each command in common shell(cmd.exe).
I'm unable to get the output of the command which is executed in shell and its execution status.

Please help me how can i get the exit status of the command executed and its output & error messages, also let me know if there any better approach to this problem.
#!/usr/bin/perl use IPC::Open3; my($wtr, $rdr, $err); $pid = open3($wtr, $rdr, $err, 'cmd.exe') or die "$!"; print "PID: $pid\n"; # Read the file containing commands # open (FHR, "Build_Cmd_List.txt") or die "$!"; # my @commands = <FHR>; # close(LIST); my $expected = '<$$$>\r\n'; my $prompt_cmd = 'prompt $L$$$$$$$G$_'; my @commands = ($prompt_cmd, 'set Build_DIR=c:\build_proj1','md %Build +_DIR%', 'cd %Build_DIR%', 'echo Inside %Build_DIR%','exit'); for my $cmd (@commands) { $| = 1; #--------------------------------- # Set of statements to replace tokens(using regex) #--------------------------------- print "Executing command after token replacement: $cmd\n"; print $wtr "$cmd\r\n"; # Read the output & error message of the command executed #while(<$rdr>){ # last if m/\Q$expected\E/ ; # print; #} }
My approach in Python is:
import subprocess # Read the File containing list of commands fhr = open("sample_file",'r') # SHELL FOR EXECUTING BUILD COMMANDS SHELL_CMD = "cmd.exe" shellproc = subprocess.Popen(SHELL_CMD,shell=True,stdin=subprocess +.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) expected = '<$$$>\r\n' prompt_cmd = 'prompt $L$$$$$$$G$_' shellproc.stdin.write('%s\n' % prompt_cmd) # Read the file line by line for line in fhr: ## ## SET of Statements to replace all the tokens cmd = <## processed line##> shellproc.stdin.write('%s\n' % cmd) while True: output = shellproc.stdout.readline() if output.endswith(expected): break logger.info(output) print "Waiting for subshell to terminate" shellproc.stdin.write("exit\n") shellproc.wait() print "Successfully Executed all the Commands [Done]"

In reply to Re^2: Executing series of system commands in a shell(cmd.exe) by Anonymous Monk
in thread Executing series of system commands in a shell(cmd.exe) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.