Suppressing system command called from awk script

285 views Asked by At

I am currently running this script in Windows 7.

So, I have a program that is meant to color-code output from another command (mkmk) and tally up varying numbers of errors and other notable stats, etc. So right now, it starts as a batch file which

  1. Turns off echo
  2. Sets some color values to variables
  3. Calls the mkmk command and the awk script together

The awk script then parses line by line, and calls a function which then calls an exe that performs the Colorizing. Works like follows:

/: error/ {
    CntError++ ;
    TraError[CntError] = $0;
    colorf(cErrorDcb,$0) ;
    next
}    

function colorf(fg, str ) {
   if ( Couleur < 1 )
   {
     printf("%s\n",str);
   }     
   else
   { 
      if ( System == "UNIX" )
      {
         printf("%s%s%s%s\n",fg,bg,str,NORMAL);
      }
      else 
      {
         system("Colorize.exe /c:" fg " \""  str "\"");
         printf("\n");
      }
   }
}

So, everything works, EXCEPT that every time system("Colorize.exe") is called (a lot), the command is outputted in the terminal and clutters up the output. It doesn't appear to be affected by the @echo off command in my batch file since it is called inside the awk script. Is there anyway to hide only these system commands but keep the rest of my awk output?

1

There are 1 answers

0
Ed Morton On BEST ANSWER

I have no idea what the magical Windows incantations are to control what displays on the terminal but in general instead of calling system() and letting the command it calls produce it's own output that's getting mixed in with the awk output, use getline to read the result of the call to populate a variable and then print that from awk. Something like this:

/: error/ {
    TraError[++CntError] = $0
    print colorf(cErrorDcb,$0)
    next
}    

function colorf(fg, str,         cmd, line, colorStr) {
   if ( Couleur < 1 ) {
      colorStr = str
   }     
   else if ( System == "UNIX" ) {
      colorStr = fg bg str NORMAL
   }
   else {
      cmd = "Colorize.exe /c:" fg " \""  str "\""
      colorStr = ( (cmd | getline line) > 0 ? line : str )
      close(cmd)
   }
   return colorStr
}

I got rid of all of the useless semi-colons too.

Best advice - get cygwin!