Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an argument that starts with "//" to a wsh script?

If I have the following script (that just prints the first argument to the console):

@if (@X)==(@Y) @end /* JScript comment
    @echo off
    cscript //E:JScript //nologo "%~f0" %*
    exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */

WScript.Echo(WScript.Arguments.Item(0));

And I try something like

C:\>test.bat "//test"

I get the following error

Input Error: Unknown option "//test" specified.

Despite the quotes.It is taken as an option to the the windows script host. How can pass an argument that starts with //. Named arguments?

like image 792
npocmaka Avatar asked Feb 07 '17 12:02

npocmaka


People also ask

How to pass arguments in a bash script?

You can pass the arguments to any bash script when it is executed. There are several simple and useful ways to pass arguments in a bash script. In this article guide, we will let you know about some very easy ways to pass and use arguments in your bash scripts. Create a new file with any name using the “touch” command, e.g., “file.sh”.

How do you pass a variable in a shell script?

In a shell script, you can pass variables as arguments by entering arguments after the script name, for example ./script.sh arg1 arg2. The shell automatically assigns each argument name to a variable. Arguments are set of characters between spaces added after the script.

How do you assign an argument to a variable in shell script?

The shell automatically assigns each argument name to a variable. Arguments are set of characters between spaces added after the script. To specify an argument that includes spaces, you need to enclose the complete argument in double quotation marks. Each script argument entered at the command line has a default variable name.

How to understand about script shell argument in Linux?

Each script argument entered at the command line has a default variable name. The first argument after the script name is assigned to the variable $1, the second argument to $2, and so on. This is a simple script displays information about a user to understand about script shell argument.


2 Answers

cscript //E:JScript //nologo "%~f0" // %*

Pass a double slash to end the cscript own argument parsing.

note: I don't know if it is documented anywhere, but tested on windows 7 and 10

Test script:

Option Explicit

Dim argument

    For Each argument In WScript.Arguments
        WScript.Echo "argument: " & argument
    Next 

    For Each argument In WScript.Arguments.Named
        WScript.Echo "Named: " & argument
    Next 

    For Each argument In WScript.Arguments.UnNamed
        WScript.Echo "UnNamed: " & argument
    Next 

Output (sorry, spanish locale):

W:\>cscript //nologo test.vbs //test
Error de entrada: Opción desconocida "//test" especificada.

W:\>cscript //nologo test.vbs // //test /one two
argument: //test
argument: /one
argument: two
Named: /test
Named: one
UnNamed: two

W:\>cscript test.vbs // //nologo //test /one two
Microsoft (R) Windows Script Host versión 5.812
Copyright (C) Microsoft Corporation. Reservados todos los derechos.

argument: //nologo
argument: //test
argument: /one
argument: two
Named: /nologo
Named: /test
Named: one
UnNamed: two

W:\>
like image 129
MC ND Avatar answered Sep 19 '22 11:09

MC ND


It works with named arguments after all.

WScript.Echo(WScript.Arguments.Named.Item("test"));

and

cscript myscript.wsf /test:"//test"
like image 24
npocmaka Avatar answered Sep 21 '22 11:09

npocmaka