Blogs

Scripting How-To: Hide the prepended "error:" in

By Erdem posted 08-09-2015 20:18

  

Display Output of Terminated Script Without an Error Statement

 
For SLAX version 1.0 and higher, you can display output of a terminated script without an error statement.
 
The <xsl:message> instruction element writes a message to the user terminal, and including the terminate="yes" attribute on <xsl:message> is the only way to end a script early. However, <xsl:message> messages are handled internally as errors and always prepend "error:" to the displayed message.
 
This is often acceptable because the point of exiting the script early is usually due to an error that needs to be reported to the user. But what if there is no error and you simply want to leave the script early?
 
In this case, you are left with awkward displays such as the following:

 

Code:

1	<xsl:message terminate="yes"> "Goodbye and have a great day!";

Output:

1	error: Goodbye and have a great day!

 

The good news is that there is an easy trick that you can do with the <xsl:message> element in op scripts to hide the initial "error" string. Simply start the message string with a carriage return by overwriting the offending text:

 

Code:

1	<xsl:message terminate="yes"> "\rGoodbye and have a great day!";

Output:

1	Goodbye and have a great day!

 

NOTE: This only useful for op scripts because the message itself is still transmitted as an <xnm:error>, which causes the commit process to fail if it is a commit script.

 

Also, all the carriage return does is move the cursor to the beginning of the line. It does not delete the prior text itself, so your message string needs to be at least six characters long to ensure it overwrites "error:", or if it is shorter than six characters, you should first include six spaces and use the carriage return again before writing your actual message:

 

Code:

1	<xsl:message terminate="yes"> "\r      \rBye!";

Output:

1	Bye!

#commitscript
#xml
#How-To
#Slax
#ScriptingHow-To
#errormessage
#opscript