6

I am creating some common doskey commands I use on a regular basis. I am trying to have multiple variables in the doskey macro and sometimes a variable will have spaces in it. I have tried quoting the variable values but it won't work. The only thing that does work is just adding more vars to the end of the macro string.

What I would like to do is: doskey keycmd=plink -l user -pw password $1 -batch $2 And then execute it as follows: c:> keycmd hostname "dir /p /users"

What doskey sees when executed though is: $1=hostname, $2="dir, and "dir isn't a command and /p and /users is completely dropped.

To get this to work I had to define it this way: doskey keycmd=plink -l user -pw password $1 -batch $2 $3 $4 And execute as follows: c:> keycmd hostname dir /p /users

Is there a way I can group "dir /p /users" into a single $2 variable? For doskey variable parameters you only get 9, $1..$9. Eventually I could run out on complicated commands. I tried single and double quotes to no avail...but single and/or double quotes seem to work everywhere else in Windows cmd command.

Is there a solution to this or am I just trying to do too much with doskey?

4

2 回答 2

5

这应该这样做。告诉最后一个 var 是其他一切

keycmd=echo plink -l user -pw password $1 -batch $*
于 2013-09-29T18:47:44.920 回答
2

虽然最后一个参数中可能有空格($*wild2010答案中使用),但通常不可能在 doskey 命令中包含带空格的参数。

请参阅使用 Doskey

不幸的是,尽管参数可以是长文件名,但它不能包含空格,即使用引号括起来。
如果文件名包含空格,则必须**使用 8.3 版本的名称。我知道没有解决方法**。

DosKey 宏也可以使用$*被称为“全局可替换参数”。
这必须是宏的最后一部分,并被命令行上不是宏名称或分配给编号参数的任何内容替换。
如果上面的例子被改写为:

DOSKEY D2F=DIR $1 $g $*

那么 long filenames-with-spaces 可以用作最后一个参数 - 但用作替代的路径名$1仍然不能包含带空格的目录名。

于 2014-10-06T07:16:18.837 回答