是否可以将环境变量设置为 cmd.exe 中命令的输出

发布于 2024-09-08 17:45:24 字数 157 浏览 0 评论 0原文

我需要执行相当于

set ENVAR=`some-command`

在 windows/cmd.exe 脚本中执行的操作。 Cygwin 不是一个选择。

对于奖励分数:是否有一些 cmd.exe 相当于一般的反引号?

I need to do the equivalent of

set ENVAR=`some-command`

In a windows/cmd.exe script. Cygwin is not an option.

For bonus marks: Is there some cmd.exe equivalent of backticks in general?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山川志 2024-09-15 17:45:24

一种快速而肮脏的方法是将其重定向到一个文件,然后阅读此内容,例如

some-command>out.txt
set /p ENVAR=<out.txt

我认为 for 也可以帮助您,但我不记得确切的语法。尝试类似

for /f "usebackq" %x in (`some-command`) do set ENVAR=%x

我可能忘记了选项中的一些 tokendelim...

A quick and dirty way would be redirecting it to a file and then reading this, e.g.

some-command>out.txt
set /p ENVAR=<out.txt

I think for can also help you, but I don't remember the exact syntax. Try something like

for /f "usebackq" %x in (`some-command`) do set ENVAR=%x

I probably forgot some token or delim in the options...

随遇而安 2024-09-15 17:45:24

不是“可能”,绝对必须指定“delims=”(这意味着“无分隔符”),除非您希望变量仅包含输入数据的第一个空格或制表符。

建议指定“delims=”作为最后一个选项,以避免操作员和 shell 的选项感知中出现潜在的混淆。

FOR /F "usebackq delims=" %%a IN (`cygpath.exe -u "%~1"`) DO (
    SET CMDNAME=%%~a
    SHIFT
)

参见 关于 FOR /F 的 SS64 文章

Not "probably", it is absolutely a must to specify "delims=" (it means "no delimiters"), unless you want your variable to only contain up to first space or tab of the input data.

It is recommended to specify "delims=" as the last option to avoid potential confusion in options perception by the operator and by the shell.

I.e.

FOR /F "usebackq delims=" %%a IN (`cygpath.exe -u "%~1"`) DO (
    SET CMDNAME=%%~a
    SHIFT
)

See SS64 article on FOR /F.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文