Saturday, 28 June 2014

Passing Arguments to Scripts in Windows

Passing Arguments to Scripts

As with most command-line utilities, arguments can be passed to scripts when they are started. You use arguments to set special parameters in a script or to pass along information needed by the script. Each argument should follow the script name and be separated by a space (and enclosed in quotation marks if necessary). In the following example, a script named Check-sys is passed the parameters Mailer1 and Full:
check-sys mailer1 full
Each value passed along to a script can be examined using formal parameters. The script name itself is represented by the parameter %0. The parameter %1 represents the first argument passed in to the script, %2 the second, and so on until %9 for the ninth argument. For example, if you create a script called Check-sys and then use the following command to call the script:
check-sys mailer1 full actual
you would find that the related parameter values are
  • %0 — check-sys
  • %1 — mailer1
  • %2 — full
  • %3 — actual
You access arguments in scripts using the parameter name: %0 for the script name, %1 for the first script parameter, and so on. For example, if you wanted to display the script name and the first argument passed to the script, you could enter
echo %0
echo %1
If you pass in more than nine parameters, the additional parameters are not lost. Instead, they are stored in a special parameter: %* (percent + asterisk). The %* parameter represents all arguments passed to the script and you can use the SHIFT command to examine additional parameters. If you call SHIFT without arguments, the script parameters are shifted by 1. This means the related value for %0 is discarded and replaced by the related value for %1, and the related value for %2 becomes the related value for %1, and so on. You can also specify where shifting begins so you can retain previous parameters if necessary. For example, if you use
shift /3

%4 becomes %3, %5 becomes %4, and so on. But %0, %1, and %2 are unaffected.

1 comment:

  1. Casino Tycoon - New York - MapYRO
    Casino 광명 출장샵 Tycoon. Casino 광명 출장마사지 Tycoon is a casino in New York, 김해 출장샵 New York, United States. The casino is open daily 24 hours. The casino has a 안양 출장안마 gaming area 고양 출장마사지 and a casino near

    ReplyDelete