简体   繁体   English

如何在 windows.bat 文件中捕获的命令中获取所有 arguments

[英]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我的尝试是包含以下内容的批处理文件 ec.bat

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.它只复制了 grep 部分(如下所示)而不是整个命令行。 It left out the pipe(|) and the "wc -l"它省略了管道(|)和“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是否有类似于此处解释的 bash/zsh 特殊参数的东西

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.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. ,我发现你可以输入一个命令“doskey /history”来给出历史。 I can use tail to grab the previous command and work on it as required.我可以使用 tail 获取上一个命令并根据需要处理它。

I guess it helps to work on a problem from different angles.我想这有助于从不同的角度解决问题。

You could use a doskey macro.您可以使用 doskey 宏。

doskey ec=toclip.bat "$*"

And in the batch file toclip.bat并在批处理文件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.这是出乎意料的,但是 doskey 宏优先于普通的 cmd.exe 解析。
That results into quotes around your arguments.这会导致您的 arguments 周围出现引号。

But it fails, if your arguments contain quotes itself, like但它失败了,如果你的 arguments 本身包含引号,比如

ec grep "hello|world" file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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