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


Hello All,
I'm trying to execute series of command read from a file using perl.
Below is a sample file
(NOTE: I don't want to use Windows batch script to execute the commands, actual file contains more than 200 lines which is generated based on some configuration during build)
Ex: Sample file contents are as below -
Line 1: set Build_DIR=c:\build_proj1
Line 2: md %Build_DIR%
Line 3: cd %Build_DIR%
Line 4: copy c:\proj\*.* c:\build_proj1
Line 5: dir
LINE 6: {TOOLPATH}\catalyst.exe /E "{CODEDIR}\proj1
\dir1\#catexe2#\#catexe[0]#_multilang.ttk" "{CODEDIR}\bin\#catexe2#"
LINE 7: {SIGNPATH}\signtool.exe proj1.msi "Proj1 Digital Signature"
LINE 8: call test_it.bat
etc.

Before executing the above commands I have to replace the tokens {TOOLPATH}, {CODEDIR} ,{SIGNPATH}, #catexe2# etc.
Also for some commands (ex; signtool.exe) if they fail, retry the command execution for 5 times and return the status.

Please help, how to execute the commands in a shell(cmd.exe) and get the output(including errors) and execution status of each command.

Regards,
SSDP

  • Comment on Executing series of system commands in a shell(cmd.exe)

Replies are listed 'Best First'.
Re: Executing series of system commands in a shell(cmd.exe)
by moritz (Cardinal) on Aug 10, 2011 at 09:57 UTC
    NOTE: I don't want to use Windows batch script to execute the commands

    Why not? It looks like a batch script, so why not execute it as batch?

    If you're determined to rewrite it in perl, you'll have to do a lot of reading about OS interaction, for example system and ENV, and have to do the rewrite yourself, replace shell variables by perl variables and so on.

Re: Executing series of system commands in a shell(cmd.exe)
by ww (Archbishop) on Aug 10, 2011 at 10:20 UTC
    "I'm trying to execute series of command(sic)...."

    you've given us very little indication of your effort. What did you try?

    Or, if your "trying" means you've asked us to write code to satisfy your (incomplete and unclear) specs, you've used an inaccurate description for the wrong question: The Monastery is not a free code-writing service. OTOH, if you want to learn how to code your own solution, consider:

    If you get stuck actually writing the code, then come back with your specific problem (and the code, the data, error message(s) (if any), and a description of how what happens deviates from your desires.

      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]"