Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOSKEY recall alias

Is it possible to recall an alias with DOSKEY? Simple example .. I wish to do something like that:

DOSKEY a=someCommand
DOSKEY b=someOtherCommand
DOSKEY c=andAThirdCommand

:: How to do this? -> DOSKEY all=a+b+c

I already know that I can do this by writing this:

DOSKEY all=someCommand ^& someOtherCommand ^& andAThirdCommand

but in the sense of reusing stuff I'd like to reuse my defined aliases from above. Is it possible like I desire?

Thanks!

PS: Saw this here, but it's not a satisfying answer. It seems that it won't work though. :(

like image 711
BAERUS Avatar asked Oct 20 '22 03:10

BAERUS


1 Answers

Good question, hard to answer... However, I can suggest a workaround with a simple batch script(s).

Suppose we have defined doskey a=commandA and doskey a=commandB and doskey c=commandC macros.

  1. Static approach: let's name our script e.g. dem (define macro) and place it somewhere in your path. Then dem acb a c b should define a new macro acb (ready to further use) as follows: doskey acb=commandA $T commandC $T commandB. That script could be established by a little adaptation from the script dsk provided (hint: instead of launching a macro text, constitute the text for new macro, but be aware of another escaping).

  2. Dynamic approach: let's name our script e.g. dsk (doskey) and place it somewhere in your path. Then dsk a b c should call macros a, b and c in that sequence. Number of parameters (macro names) passed to script is not limited. The script works well with very simply defined macros, but

    • allows % percent sign use like in doskey a=echo %variable% macro and/or even for loops like in doskey a=for /F "tokens=*" %G in ('dir /b /s *.txt') do @echo %G;
    • allows $T concatenated commands in a macro (equivalent to & in a batch file) like doskey a=dir $T set; done by replacing $T with & (in rare cases does not suffice, then need to split and perform commands separately).

Known issues and/or restrictions (impossible to resolve without knowing more about real structure of macros used); the dsk script

  • a for loop could bring problems with %%parameter variable; vetoed to use in a macro: %%{ (outer loop) and %%? %%@ (inner loop)
  • does not make allowance for piping or redirecting like doskey a=dir ^> somefile;
  • does not make allowance for ^& concatenated commands in a macro (but allows doskey intrinsic $T concatenation);
  • does not make allowance for launching batch .bat or .cmd scripts (needs call %xitem% instead of %xitem% in the :doItem procedure (cca 50th line);
  • if a macro name isn't found, it's simply overskipped...

The script dsk is as follows:

:: dsk.bat
@ECHO OFF >NUL
@SETLOCAL enableextensions enabledelayedexpansion
set "CheckOnly=0"

set "DoneList="
set "ToDoList="
set "xitem=x"
for %%{ in (%*) do (
  set "_G_found=0"
  echo.
  for /F "tokens=1* delims==" %%? in ('doskey /macros') do (
    if /i "%%{"=="%%?" (
      for /F "tokens=*" %%_ in ("%%@") do set "item=%%_"
      if /i "!item:~0,3!"=="for" set "item=!item:%%=%%%%!"
      if "%CheckOnly%"=="1" (
          echo :  to do: '!item!'
      ) else (
          echo :  To Do: '!item!'
          call :doItem !item!
      )
      set "DoneList=!DoneList! +%%{"
      set "_G_found=1"
    ) 
  )
  if "!_G_found!"=="0" (
    set "DoneList=!DoneList! -%%{"
    echo :  macro: [%%{] ^(not found^)
    if "!ToDoList!"=="" set "ToDoList=!ToDoList!, [%%{]"
    if "!ToDoList!"=="!ToDoList:[%%{]=!" set "ToDoList=!ToDoList!, [%%{]"
  )
)
echo.
echo :  
echo :    trailing progress report
echo :  
if "%ToDoList%"=="" (
    echo :    all found: %DoneList:~1%
) else (
    echo :    +found, -not found: %DoneList:~1%
    echo :   %ToDoList:~2% not found in macro name list 
)
echo :    end of batch %~f0
echo :  
:endlocal
@ENDLOCAL
@goto :eof

:doItem
  set "xitem=%*"
  set "xitem=%xitem:$T=&%"
  %xitem%
@goto :eof

With next scenario:

d:\bat>doskey /macros
y=for /F "tokens=*" %g in ('dir /b rand.bat') do @echo %g
x=dir /B /AD $T dir /B /AD "D:\Remote\bat\COCL\bu bu bu" $T set y
a=echo CheckOnly=%CheckOnly%
b=rand.bat

and dsk y b a x n call gives next output:

d:\bat>dsk y b a x n

:  To Do: 'for /F "tokens=*" %%g in ('dir /b rand.bat') do @echo %%g'
rand.bat

:  To Do: 'rand.bat'
The system cannot find the batch label specified - doItem

:  To Do: 'echo CheckOnly=%CheckOnly%'
CheckOnly=0

:  To Do: 'dir /B /AD $T dir /B /AD "D:\Remote\bat\COCL\bu bu bu" $T set y'
files
a.dot 1.dot 2.dot 3
Environment variable y not defined

:  macro: [n] (not found)

:
:    trailing progress report
:
:    +found, -not found: +y +b +a +x -n
:   [n] not found in macro name list
:    end of batch d:\bat\dsk.bat
:
d:\bat>

The script is rather verbose for debugging purposes (and variable CheckOnly as well, with values 1= only echo commands, 0= echo and execute commands in a macro).

like image 197
JosefZ Avatar answered Oct 27 '22 08:10

JosefZ