简体   繁体   中英

how to get all the arguments in a command captured in a windows .bat file

grep -P  "ERROR.+SQLTransientConnectionException" uts.log  | wc -l

I was trying to copy/capture the above (previous) command to the clipboard.
My attempt was a batch file ec.bat with below contents

echo %* | clip

However, when I tried that on the previously run command like below

ec.bat grep -P  "ERROR.+SQLTransientConnectionException" uts.log  | wc -l

it only copied the grep part(like below) not the entire command line. It left out the pipe(|) and the "wc -l"

grep -P  "ERROR.+SQLTransientConnectionException" uts.log

How can I get/request the entire command line? Is there an environment variable containing the previous command? Is there something similar to the bash/zsh special parameters like explained here

I tried this problem from another angle. I tried to find the history of the command prompt.

From https://www.itechtics.com/view-command-prompt-history-windows/#:~:text=Open%20CMD%20from%20the%20Start,within%20the%20window%20of%20CMD. , I found that you can type a command "doskey /history" which gives the history. I can use tail to grab the previous command and work on it as required.

I guess it helps to work on a problem from different angles.

You could use a doskey macro.

doskey ec=toclip.bat "$*"

And in the batch file toclip.bat

@echo off
set line=%*
setlocal EnableDelayedExpansion
REM *** Remove the surrounding quotes from line
for /F "delims=" %%L in ("!line!") DO (
    endlocal
    (echo(%%~L) | clip
)

It's unexpected, but doskey macros have precendence over normal cmd.exe parsing.
That results into quotes around your arguments.

But it fails, if your arguments contain quotes itself, like

ec grep "hello|world" file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM