Difference in string when passing a carriage return - go

In Go, I run the application from the command line as follows:
myapp -message "FOK \nHost:"
In my code:
var args = os.Args
var command = args[1]
var message= args[2]
the message will have the value "FOK \nHost:". Printing the value of message gives me:
fmt.Println("message-> " + message)
output
message-> FOK \nHost:
but if I set the value of the message in the application using the same value, the output is different.
message = "FOK \nHost:"
output
message mod-> FOK
Host:
I'm trying to accomplish the second result, so I'm trying to figure out why are the arguments string different thane when I assign it in the code.

Related

Is there a way to use the fmt.Scan() function with a message in one line?

Is there is a way to get an input from the user with a message in one line, like in python where you would do name = input("Name: ") instead of doing:
var name string
fmt.Println("Name: ")
fmt.Scan(&name)

VBS Type mismatch CLng

I am using VBScript for the first time as it is the only language that a program can read.
Relevant section of code
Dim input
Function printer ()
input = InputBox("Printer", "Input Required")
printer = input
End Function
The software then calls the value of 'printer', and everything works fine when I input numbers, but if I try any text, it throws the below error message
Error: 13, Type mismatch: 'CLng'
in the line:

JScript: identifying whether double quotes are passed to a WSH script

There are situations when it is important to identify whether double quotes are passed as arguments to a WSH script. For example because they should be passed to another executable to be run.
The standard parsing functions/objects:
objArgs = WScript.Arguments;
for (i = 0; i < objArgs.length; i++)
{
WScript.Echo(objArgs(i));
}
do not differentiate between:
cscript foo.js "bar"
and
cscript foo.js bar
Is it possible with some other approach?
Note: I also tried to sort of escape them with several combinations like:
cscript foo.js '"bar"'
It seems that they are simply stripped away.
Following #Ekkehard.Horner suggestions:
Solution
// parseArgs.js
// Parsing jscript script arguments verbatim
var Shell = new ActiveXObject("WScript.Shell"),
wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2"),
guid = (new ActiveXObject("Scriptlet.TypeLib")).GUID.substring(0,38),
windir=Shell.ExpandEnvironmentStrings("%WinDir%"),
winver="\"" + windir + "\\System32\\winver.exe\" " + guid,
pcol, pid, cmd;
// Run winver.exe hidden and get this script ID as its ParentProcessId
winver=winver.replace(/\\/g, "\\\\");
Shell.Run("winver " + guid, 0);
pcol = new Enumerator (wmi.ExecQuery(
"SELECT * From Win32_Process WHERE CommandLine='"+ winver + "'",
"WQL", 32));
for (; !pcol.atEnd(); pcol.moveNext()){
var prc = pcol.item();
pid=prc.ParentProcessId;
prc.Terminate;
}
// Get the command line for the found PID
pcol = new Enumerator (wmi.ExecQuery(
"SELECT * From Win32_Process WHERE ProcessID="+ pid,
"WQL", 32));
for (; !pcol.atEnd(); pcol.moveNext()){
var prc = pcol.item();
cmd =prc.CommandLine;
}
WScript.Echo(cmd);
// Parse command line for arguments
var ags,
parseCmd=function(cmd){// WMI trims initial spaces
var p = new Object(),
re =/^"/.test(cmd) ? /"[^"]+" */ : /\S+\s*/;
p.nxt=re.test(cmd) ? cmd.match(re)[0] : ""; // extract next token
p.rst=cmd.replace(re, "") ; // remainder
return(p);
}
// Strip c/wscript path
ags=parseCmd(cmd).rst
//WScript.Echo(ags);
// Remove WSH "//xxx" options
ags=ags.replace(/\/\/\w+ +/g, "")
//WScript.Echo(ags);
// Strip script name and get arguments
ags=parseCmd(ags).rst
WScript.Echo(ags);
// Loop args and store as an array
var i=1, aags=[];
while(ags != ""){
var p =parseCmd(ags);
ags=p.rst;
aags.push(p.nxt.replace(/ +$/, ""));
WScript.Echo(i, p.nxt);
i++;
}
WScript.Echo(aags);
Test
Running parseArgs.js gives:
> cscript //nologo parseArgs.js "hello" world
cscript //nologo parseArgs.js "hello" world
"hello" world
1 "hello"
2 world
"hello",world
The line:
> parseArgs.js "hello" world
gives similar results.
Comments
Do we need such a convoluted script? Short answer: no. Long: depends.
In general, assuming you know the name of your script when it is run, you could query WMI for it.
Anyway, when you deploy your script, you do not normally have control on the deploy directory. So, if there is another script running under the same name, you can't know for sure which one is yours.
Another not so edge case is when there are two or more instances of your script running.
The strategy here is to run some dummy standard Windows executable (winver.exe) hidden, passing to it a GUID. In this way, it is safe to identify winver.exe command line by the unique GUID and consequently your script as the parent of winver.exe.
winver.exe does not require arguments, but does not protest if you pass some to it.

SQL server reporting Validate parameter is in correct format

On my reporting application which is developed using SSRS 2005, I have a parameter of type string which accepts time. the time should be in the format "HH:mm:ss" How can I check if the input string is of correct format?
I tried to do the following
IsDate(TimeValue(parametr!stime.Value))
This returns true as long as the value is within range. But if the value is 24:00:00 or a wrong value then an exception is thrown.
I also tried to create a function in the report code as follows:
Public Function CheckNum(sNum as String) as Boolean
Dim msg as String
msg = ""
Try
If IsDate(TimeValue(sNum))=1 Then
Return True
Else
msg="Parameter must be a number"
End If
Catch ex as Exception
Return False
End Try
If msg <> "" Then
MsgBox(msg, 16, "Parameter Validation Error")
Err.Raise(6,Report) 'Raise an overflow
End If
End Function
And when I input a value 24:00:00 I still get an error
" The conversion of a char type to datetime data type resulted in an out of range date time value"
How can I handle the exception so that I don't the error?
EDIT:
public Function CheckNum(sNum as String) as Boolean
Dim REGEX_TIME = "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"
If System.Text.RegularExpressions.Regex.IsMatch(sNum, REGEX_TIME) Then
Return True
Else
Return False
End If
End Function
Then I assigned a parameter(validateTime) value as =Code.CheckNum(Parameters!sTime.Value)
But the value of the parameter is always true. When I specify a value greater than 23, I still see the error. Please see the image
Instead of using IsDate function, use VB.NET regular expressions. SSRS allows full use of .NET functions.
See an example of time regex.
A good tutorial on regex.
Example Code Console Application
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim REGEX_TIME = "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"
Dim InputList As List(Of String) = New List(Of String)
InputList.Add("25:00:21")
InputList.Add("22:00:21")
InputList.Add("AA:00:21")
InputList.Add("17:21:02")
For Each input As String In InputList
If Regex.IsMatch(input, REGEX_TIME) Then
Console.WriteLine("TIME " + input + " IS OK")
Else
Console.WriteLine("TIME " + input + " IS NOT OK")
End If
Next
End Sub
End Module
Output is :
TIME 25:00:21 IS NOT OK
TIME 22:00:21 IS OK
TIME AA:00:21 IS NOT OK
TIME 17:21:02 IS OK
I think, you can capture a InvalidCastException.

Node.js Shell Script And Arguments

I need to execute a bash script in node.js. Basically, the script will create user account on the system. I came across this example which gives me an idea how to go about it. However, the script itself needs arguments like the username, the password and the real name of the user. I still can't figure out how to pass those arguments to the script doing something like this:
var commands = data.toString().split('\n').join(' && ');
Does anyone have an idea how I can pass those arguments and execute the bash script within node.js over an ssh connection.
thanks
See the documentation here. It is very specific on how to pass command line arguments. Note that you can use exec or spawn. spawn has a specific argument for command line arguments, while with exec you would just pass the arguments as part of the command string to execute.
Directly from the documentation, with explanation comments inline
var util = require('util'),
spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']); // the second arg is the command
// options
ls.stdout.on('data', function (data) { // register one or more handlers
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
Whereas with exec
var util = require('util'),
exec = require('child_process').exec,
child;
child = exec('cat *.js bad_file | wc -l', // command line argument directly in string
function (error, stdout, stderr) { // one easy function to capture data/errors
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
Finally, note that exec buffers the output. If you want to stream output back to a client, you should use spawn.
var exec = require('child_process').exec;
var child = exec('cat *.js | wc -l', function(error, stdout, stderr) {
if (error) console.log(error);
process.stdout.write(stdout);
process.stderr.write(stderr);
});
This way is nicer because console.log will print blank lines.
You can use process.argv. It's an array containing the command line arguments. The first element will be node the second element will be the name of the JavaScript file. All next elements will be any additional command line you given.
You can use it like:
var username = process.argv[2];
var password = process.argv[3];
var realname = process.argv[4];
Or iterate over the array. Look at the example: http://nodejs.org/docs/latest/api/all.html#process.argv

Resources