I am a new bee for vbScript. I have a script that I am calling with an arguments which contains double quote. Following is the call
.vbs "a" 2 where variable a is JO"N
My VbScript:
If WScript.Arguments.Count > 0 Then
MsgBox "NAME:" + WScript.Arguments.Item(0)
MsgBox "NAME:" + WScript.Arguments.Item(1)
Else
MsgBox "Please pass a parameter to this script"
End if
*Note this works when a = JON but does not work when a= JO"N. I tried putting escape characters" (Example: "WScript.Arguments.Item(0)") but it does not work.
You can't.
At least, as the WScript argument parser handles and removes all quotes from the values in Arguments collection, you can not use the default argument handling for this task.
note: And we are leaving out of the problem if the final command you are running when calling your script (you have not included how/from where you make the call) will or not have problems because the additional quote interferes argument quoting rules.
You need to use some workaround to get the double quote to reach the script. Some approachs could be:
Replace the quote and any other problematic character (use some kind of escape sequence) before calling the script and revert the process inside your script.
Save the value you want to pass into an environment variable (how to make it depends on how you are callign the script) and then retrieve the value from your script (ex. here).
Use WMI to retrieve the full command line used to start the script, including all the quotes (ex. here) and write your own argument parser routine.
From my point of view, I would use the second option.
This will work: Chr(34) & Wscript.Arguments.Item(0) & Chr(34). Here Chr(34) function returns a double quote using its ASCII code 34.
Related
A Perl system call must send the following string to the UnixShell:
'"XYZ"'
In my Perl script I have used the following command:
system("cleartool mkattr -replace ATTRIBUTE '"$attribute"' lbtype:$label");
Everything is well passed to the Shell Unix, except both uses of the quote character:
'
Indeed,
cleartool mkattr -replace ATTRIBUTE
The above command is passed as it is exactly what I want.
The Perl variables $attribute and $label are well interpreted.
But I don't know what to do to obtain exactly:
'"XYZ"'
Here XYZ is the value of the Perl variable $attribute
OS is AIX (Unix) and Shell is ksh. cleartool is the command line interface of Clearcase but no Clearcase skill is necessary to fix my problem.
If you want to execute a system command and don't have to use any shell syntax like redirects, it's usually better and safer to use the list form of system:
system(
'cleartool', 'mkattr', '-replace', 'ATTRIBUTE',
qq{"$attribute"}, qq{lbtype:$label}
);
# or, if you really want to pass both types of quotes:
system(
'cleartool', 'mkattr', '-replace', 'ATTRIBUTE',
qq{'"$attribute"'}, qq{lbtype:$label}
);
See perldoc -f system
It's not clear from your question if you want to pass '"XYZ"' or "XYZ".
See "Quote and Quote like Operators" and use qq{...}:
system(qq{cleartool mkattr -replace ATTRIBUTE '"$attribute"' lbtype:$label});
qq{...} is exactly like "..." except you can then use double quotes " in your string without escaping them.
You can use any character directly after the qq and must then use the same character to denote the end-of-string, i.e. qqX...X would work the same way. You would run into problems if your string contains Xes, so don't do that.
You can also use paired characters as delimiter ({}, (), <>) which is what you usually will see.
I have a batch file that I call with something like this
call do.cmd "one two"
In do.cmd I am launching a program and pass to it first parameter from above:
#echo off
some_program.exe -name='%1'
The value to some_program.exe for name variable must be passed inside single quotes without surrounding double quotes. To get rid of double quotes in passed parameter I make a temporary variable like this:
set v_tmp=%1
set v_tmp=%v_artist:"=%
And then launch my program by
some_programm.exe -name='%v_tmp%'
The problem start when with do.cmd some text having & sign is passed. If I leave batch file code as is, variaable setting will fail because & will act as a divider. If I escape & sign by
set v_tmp=%1
set v_tmp=%v_tmp:&"=^^^&%
set v_tmp=%v_tmp:"=%
then some_program will output text having ^&..
The question is how do I get from call do.cmd "one & two" line to the correct & sign escaping and double quote removal so that to have in result some_program.exe -name='one & two'?
You could try to use delayed expansion here.
#echo off
set "arg1=%~1"
setlocal EnableDelayedExpansion
some_program.exe -name='!arg1!'
This works, as %~1 removes enclosing quotes, if present.
And !arg1! always expands the variable in a safe manner.
I have 2 EXEs in VB 6. EXE 1 calls the other EXE2 through shell command
EXE1 :
Shell(PathName\EXE2,0)
Now all I want is to pass a string type variable to EXE2 which I wish to receive in that EXE2.
How can I achieve the same?
Thanks in advance
Simply append the string (possibly quoted) to the end of the filename:
Shell("""PathName.exe"" wibble", vbNormalFocus)
or:
Shell("""PathName.exe"" ""wibble"" ""wibble 2""", vbNormalFocus)
This value can then be read in the other application using the Command$() function which will include everything after the path name and space, including any quotes around the parameters (e.g. "wibble" "wibble 2").
I am trying to run the following code in vbscript:
ReturnCode = WshShell.Run("C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe", 0, True)
I get an error when I run this script saying it cannot find the file. I think the problem is spaces in the path, but I don't want to reinstall this application to a different path. How do I get around this?
EDIT: Also, I need to be able to put arguments after the executable. Do the arguments go inside the quotes or outside?
You can get around this by surrounding the path in quotes. But to do so, you need to escape them correctly(with "), so:
ReturnCode = WshShell.Run("""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe""", 0, True)
EDIT: Keep the path in double quotes and add around them as necessary:
"""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe"" argumentGoesHere"
Put the executable inside of double-quotes:
ReturnCode = WshShell.Run( _
"""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe""", _
0, True)
I've never been a big fan of multiple quotes appearing in my code, though it is definitely a solution that works.
What I prefer to do to make my code (to me) more readable is to use chr(34) (the ASCII version of the quotation mark) when I'm adding quotes designed to surround a file name or other string that must be encased in quotes. It's more typing, but to me it avoids the potential confusion that a line like """x y"" ""z 1 2""" can cause.
For the example used by the OP, it would look like this:
ReturnCode = WshShell.Run(chr(34) & "C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe" & chr(34), 0, True)
The reason I like this is maybe a little clearer when it comes to a path where you're throwing in command line arguments. For example, when you look at this:
"""C:\Program Files\Some Vendor\Application\program.exe"" -file ""data file.txt"""
It's kind of hard to see what all those quotes are and to figure out what quotes are around what.
Compare that to:
chr(34) & "C:\Program Files\Some Vendor\Application\program.exe" & chr(34) & _
" -file" & chr(34) & "data file.txt" & chr(34)
To my eye, the chr(34) becomes a way to easily visually differentiate between quotation marks that are part of a string definition (e.g., "data file.txt") and those which are needed for Windows to understand the path/filename properly (which show as chr(34) in the example).
If you're consistent about using chr(34) to mean "this is a quotation mark I need in order for Windows to understand the next item in the code" and the normal quotation mark to specify the beginning and ending of a string value, it can even make debugging issues a little easier.
But ultimately each person should do what works for them. My approach takes more typing and might confuse someone who doesn't know what chr() is. The other takes less typing but requires you to do a bit more mental parsing of the string. Neither is really right or wrong.
put three double quotes around the path.
How do I write a shell script (bash on HPUX) that receives a string as an argument containing an asterisk?
e.g. myscript my_db_name "SELECT * FROM table;"
The asterisk gets expanded to all the file names in the current directory, also if I assign a variable like this.
DB_QUERY="$2"
echo $DB_QUERY
The asterisk "*" is not the only character you have to watch out for, there's lots of other shell meta-charaters that can cause problems, like < > $ | ; &
The simple answer is always to put your arguments in quotes (that's the double-quote, " ) when you don't know what they might contain.
For your example, you should write:
DB_QUERY="$2"
echo "$DB_QUERY"
It starts getting awkward when you want your argument to be used as multiple parameters or you start using eval, but you can ask about that separately.
You always need to put double quotes around a variable reference if you want to prevent it from triggering filename expansion. So, in your example, use:
DB_QUERY="$2"
echo "$DB_QUERY"
In the first example, use single quotes:
myscript my_db_name 'SELECT * FROM table;'
In the second example, use double quotes:
echo "$DB_QUERY"