Microsoft Windows 2. Scripting Guide. Microsoft. If you have worked with command- line tools, you are already familiar with the concept of arguments. For example, when you run Ping. Internet Protocol (IP) address of the computer being pinged as a command- line argument: Providing values to a script by means of the command line is convenient in cases in which you expect the values to be different each time the script runs. If the values were hard coded within the script, changing the values would require you to edit the script. Using command- line arguments saves time in these cases. Again, this is no different from working with command- line tools. Ping. exe uses command- line arguments to provide flexibility: This way, you can attempt to ping any computer anywhere. If Ping. exe did not accept command- line arguments, you would either need a separate version of the program for each computer you wanted to ping, or you would have to edit and recompile the source code any time you wanted to ping a different computer. Command- line arguments are stored in the Wsh. Arguments collection, which you access through the Arguments property of the WScript object as shown in Figure 3. In addition to storing all command- line arguments in the Wsh. Arguments collection, WSH automatically filters each argument into the Wsh. Named or Wsh. Unnamed collection based on the format of the argument. Figure 3. 8 WSH Command- Line Argument Collections. How Command- Line Arguments Are Stored and Filtered. When you run a script with command- line arguments, the WSH runtime stores these values in a location in memory represented by the Wsh. Arguments collection. The WSH runtime stores the arguments in the Wsh. Arguments collection in the order in which they were entered on the command line: WScript. Arguments. Item(0) contains the first command- line argument, WScript. Arguments. Item(1) contains the second argument, and so on. In addition to storing all command- line arguments in the Wsh. Arguments collection, WSH automatically filters the command- line arguments into one of two subcollections: Wsh. Named or Wsh. Unnamed. Arguments that conform to /name: value format are stored in the Wsh. Named collection, and arguments that do not follow the /name: value format are stored in the Wsh. Unnamed collection. The purpose behind filtering arguments will become clear later in this section. For example, the following command runs the Server. Stats. vbs script and passes three command- line arguments to the script: /s: atl- dc- 0. Command Line Arguments Program In Unix\/linuxBecause of this, command- line arguments that contain white space must be enclosed in quotation marks to be treated as a single argument. For example, in this command, the third argument contains a blank space. As a result, the entire argument must be enclosed in quotation marks. Count and Length return the total number of command- line arguments entered on the command line. The Wsh. Named filtered collection contains the two named arguments. Command Line Arguments Program In Unix You CombineNamed arguments are arguments that consist of two parts: a name and a value. The name must be prefaced with a forward slash, and a colon must separate the name from the value. The slash prefix and the colon separator are fixed and cannot be changed. For example, you cannot use a hyphen in place of the slash; the following command will not pass Server as a named argument; instead, it will treat - Server: atl- dc- 0. Server: atl- dc- 0. If you examine Figure 3. Wsh. Named collection. The name portion of the argument becomes the index, or key, and is used with the Wsh. Named Item property to identify the argument to retrieve. The name is also used with the Wsh. Named Exists method to check whether a named argument was provided to the script at run time. The slash prefix and the colon separator are discarded, and only the value portion of the named argument is stored in the Item property of the Wsh. Named collection. Like Wsh. Arguments, the Wsh. Named Count method and Length property return the number of filtered arguments in the Wsh. Named collection. The Wsh. Unnamed filtered collection contains the one unnamed argument: perf. The Wsh. Unnamed Count method and Length property return the number of filtered arguments in the Wsh. Unnamed collection. There are three ways to access command- line arguments: You can access the entire set of arguments using the Wsh. Arguments collection. You can access the arguments that have names using the Wsh. Named collection. Microsoft C startup code uses the following rules when interpreting arguments given on the operating system command line: Arguments. Working with Command-Line Arguments. In the original Unix tradition, command-line options. You can access the arguments that have no names using the Wsh. Unnamed collection. Note that the '-q' optional argument could be located anywhere on the command line and the program. To use command line arguments in your program. It is the number of arguments passed into the program from the command line, including the name of the program. C Command Line Arguments. Command line arguments. When using the Unix command line. A detailed list of the command-line arguments and options for. Using the Default Arguments Collection. As described in the preceding topic, all command- line arguments are stored in the default arguments collection, Wsh. Arguments. The Wsh. Arguments collection is accessed through the WScript Arguments property. Wsh. Arguments provides two methods and four properties to read and work with command- line arguments. Methods. Count. Returns the total number of arguments in the Wsh. Arguments collection. Show. Usage. Echoes usage instructions about the script. This is constructed from information provided in the < runtime> section of a Windows Script File (. Windows Script Files are not discussed in this book. Properties. Named. Provides access to the Wsh. Named collection. Wsh. Named is a filtered collection of arguments, where each argument conforms to the following format: /name: value. Command- line arguments that conform to the /name: value format are called named arguments in WSH. Unnamed. Provides access to the Wsh. Unnamed collection. Wsh. Unnamed is a filtered collection of arguments, drawn from Wsh. Arguments, that do not conform to the /name: value format. Windows Script Host refers to these as unnamed arguments. Length. Returns the total number of arguments in the Wsh. Arguments collection. Note. You might have noticed that the Length property is identical to the Count method. Length was provided to maintain a level of consistency with the ECMAScript Language Specification, Standard ECMA- 2. JScript is based on. Although either Count or Length can be used to determine the number of arguments passed to VBScript, you must use Length with JScript. Any attempt to use Count with JScript will result in a run- time error. Item(n). Retrieves the element from the Wsh. Arguments collection that corresponds to the index number enclosed in parentheses. The following command runs a fictitious script named Get. Events. vbs with three arguments. To read the three arguments, you use the WScript Arguments property in combination with the Wsh. Arguments Item property as shown here. Server. Name = WScript. Arguments. Item(0). Event. Log = WScript. Arguments. Item(1). Event. ID = WScript. Arguments. Item(2). Because collections are zero- based, WScript. Arguments. Item(0) points to the first argument in the Wsh. Arguments collection. WScript. Arguments. Item(1) points to the second argument, which is enclosed inside quotation marks because the argument, Directory Services, contains a space. Omitting the quotation marks would cause the two words to be treated as separate arguments, which would lead to errors because incorrect values would be assigned to the Event. Log and Event. ID variables. WScript. Arguments. Item(2) points to the third argument. The collection looks like the one shown in Table 3. Table 3. 6 Sample WSH Arguments Collection. Item. Value. 0atl- dc- 0. Directory Service. If the fictitious script employed additional arguments, WScript. Arguments. Item(3) would point to the fourth argument, WScript. Arguments. Item(4) would point to the fifth argument, and so on. There is no fixed limit on the number of arguments that can be stored in the Wsh. Arguments collection. However, the entire command line, which includes the host name, host options, script name, and script arguments, cannot exceed the maximum command- line length. Exceeding the maximum command- line length generally is not a problem unless you use the WSH Drag and Drop feature to populate Wsh. Arguments. The maximum command- line length also applies to WSH Drag and Drop. You can reduce the amount of typing necessary to access each argument by setting a reference to the Wsh. Arguments collection by way of the WScript Arguments property. Use the VBScript Set keyword followed by the variable name you want to use to access the Wsh. Arguments collection. Set is required because collections are standard COM objects. The following example is functionally equivalent to the preceding example despite some syntactical differences. While omitting any kind of argument verification might be OK for a quick ad hoc script, failing to perform some level of verification can lead to error- prone scripts, especially when the script is shared with other users. The following command runs the fictitious Get. Events. vbs script without any arguments. Running Get. Events. C: \Scripts\Get. Events. Microsoft VBScript runtime error: Subscript out of range. Any attempt at using the Item property to access an argument that does not exist will result in a Subscript out of range run- time error. Listing 3. 6 demonstrates how to use the Wsh. Arguments Count method to verify that the correct number of command- line arguments is provided to the script at run time. Listing 3. 6 Using Count to Verify the Number of Arguments Used. If WScript. Arguments. Count = 3 Then. Server. Name = WScript. Arguments. Item(0). Event. Log = WScript. Arguments. Item(1). Event. ID = WScript. Arguments. Item(2). Wscript. Echo . If the value is equal to 3, the script initializes the Server. Name, Event. Log, and Event. ID variables with the three arguments in the Wsh. Arguments collection. Otherwise, the script echoes usage instructions and immediately exits. Unnamed Command- Line Arguments. Unnamed arguments are entered on the command line as values only, without an associated name. At times the order of your arguments might be irrelevant; for example, a script might require you to type in three computer names, and it makes no difference which computer the script runs against first.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |