Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doskey macro with spaces in passed variable?

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?

like image 716
rolinger Avatar asked Feb 02 '13 23:02

rolinger


2 Answers

This should do it. Tell the last var to be everything else

keycmd=echo plink -l user -pw password $1 -batch $*
like image 173
wild2010 Avatar answered Sep 22 '22 16:09

wild2010


While you might have spaces in the last parameter (using $* as in wild2010's answer), it is generally not possible to include parameters with spaces in a doskey command.

See Using Doskey:

Unfortunately, although a parameter can be a long filename, it cannot include spaces, even if enclosed in inverted commas.
If a filename includes spaces, it is necessary to **use the 8.3 version of the name. I know of no work around to this**.

DosKey macros can also use $* which is termed a "global replaceable parameter".
This must be the last part of a macro and is replaced by whatever is on the command line that is not the macro name or assigned to numbered parameters.
If the above example was rewritten as:

DOSKEY D2F=DIR $1 $g $*

then long filenames-with-spaces could be used as the final parameter - but the path name used as a substitute for $1 still could not contain directory names with spaces.

like image 29
VonC Avatar answered Sep 22 '22 16:09

VonC