0

I need help to print an omnibus "summary" string. I have a trigger that executes a procedure:

begin

for each row critical in alerts.status where critical.AlertKey = 'DISK_USAGE_CRIT'          
begin
execute send_email( critical.Node, critical.Severity, critical.AlertKey, 'NetcoolEmail', critical.Summary, 'WINITMSVR631');
end;
end

That trigger passes the values of critical node, severity, alertkey, 'NetcoolEmail', summary and host name as parameters to a procedure named send_email.

This is the procedure body:

(node char(1), severity int, situation char(1), email char(1), summary char(1), hostname (1)).

This procedure then passes the values of those parameters to variables in a batch file.

set node=%1
set situation=%3
set summary=%5
echo %node% >> C:\IBM\logtest.txt
echo %situation% >> C:\IBM\logtest.txt
echo %summary% >> C:\IBM\logtest.txt

When I echo all the variables and redirect them into a text file, the summary string is truncated while the others being printed as expected.

This is how summary variable looks if it's fully printed:

DISK_USAGE_CRIT[(%_Used>=1 AND WTLOGCLDSK.INSTCNAME AND Disk_Name="C:" ) ON Primary:WINSVR2K8:NT ON C: (%_Used=41 Disk_Name=C: )]

And yet, it is printed like this:

DISK_USAGE_CRIT[(%_Used 

The rest is not being printed.

Why is the summary string not output completely?

Mofi
  • 38,783
  • 14
  • 62
  • 115

1 Answers1

0

Each percent sign % in a string which should be interpreted as literal character within a command process and not as begin/end of an environment variable reference must be doubled.

But if the summary string is passed to batch file as:

"DISK_USAGE_CRIT[(%_Used>=1 AND WTLOGCLDSK.INSTCNAME AND Disk_Name="C:" ) ON Primary:WINSVR2K8:NT ON C: (%_Used=41 Disk_Name=C: )]"

with a double quote at beginning and a double quote at end as last parameter, it is not really necessary to replace each % by %% if using in the batch file additionally delayed environment variable expansion.

The reason for the truncation is > being interpreted as command redirection operator if not found within a double quoted string and not using delayed environment variable expansion.

Therefore it is required here to use delayed expansion on echoing the values of the environment variables containing characters with special meaning for Windows command interpreter.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "node=%~1"
set "situation=%~3"
set "summary=%~5"
echo !node!>>C:\IBM\logtest.txt
echo !situation!>>C:\IBM\logtest.txt
echo !summary!>>C:\IBM\logtest.txt
endlocal

See answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? why using set "variable=value". The first double quote is left to variable name and not right of the equal sign. This makes a big difference as the referenced answer explains in detail.

The space character left of each redirection operator >> is removed to avoid writing a trailing space on each line into the text file. The space character right of each redirection operator >> would not matter, but is here nevertheless removed, too.

For more information about delayed environment variable expansion open a command prompt window, run set /? and read all output help pages for this command very carefully.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115
  • hello mofi, thank you for your suggestion. i already update my batch with your code in it. but it's also returns value like before 'Primary:WINITMSVR631:NT' 'DISK_USAGE_CRIT' 'DISK_USAGE_CRIT[(%_Used it still not printed all the body of a summary. thanks – Budi Permana Nov 06 '16 at 10:43
  • Please verify how you pass the summary string to the batch file by using in the batch file at top `@echo %* >>C:\IBM\logtest.txt`. If you don't get the summary string written into the text file with the surrounding double quotes, you pass the string wrong to the batch file, most likely without the required surrounding double quotes. – Mofi Nov 07 '16 at 07:17
  • Hello mofi, i alread succeed to pass the string and printed it to the logtest.txt. but when i made the summary the body of an email, then send it with blat.exe, the email is not being sent ? is it because the summary is too long ? thanks mofi – Budi Permana Nov 07 '16 at 07:30
  • Well, `blat.exe` must be called with `"!summary!"` as parameter. It is necessary to enclose the summary string again in double quotes. How `blat.exe` interprets this argument string with surrounding double quotes and additionally double quotes inside is unpredictable and depends on the startup code of the used compiler of `blat`. For details about different interpretation of argument strings with double quotes inside see [this answer](http://stackoverflow.com/a/24008269/3074564). Best would be to replace all double quotes by single quotes before passing summary string to batch file. – Mofi Nov 07 '16 at 11:38