使用 start cmd /k 和 doskey 的 windows bat 文件不起作用
我尝试将 start cmd 与自定义 doskey 文件一起使用,但失败了。
1.doskey
ls=dir /b $1
start "title" cmd.exe /k doskey /macrofile=1.doskey && ls .
提示
ls 命令无法识别
I tried to use start cmd with custom doskey file, but failed.
1.doskey
ls=dir /b $1
start "title" cmd.exe /k doskey /macrofile=1.doskey && ls .
Prompted
ls command is not recognized
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个问题:
问题 1
&&
将由您运行此命令的 shell 解释,而不是窗口中的 shell,因此您正在运行doskey /macrofile=1.doskey
在新窗口中,但ls .
在旧窗口中。解决方案是用双引号将应传递到新窗口的整个命令括起来:
或者,您可以使用
^
转义两个&
字符:...但是,这只会发现问题 2,所以请继续阅读。
问题 2
您可以从 中推断出(某种程度上) DOSKEY 文档,您无法从未手动输入的命令运行宏(毕竟,DOSKEY 是一个处理来自计算机的交互式输入的工具用户):
(强调我的。)
这仅讨论批处理文件,但它适用于输入命令并将其传递给
cmd /c
或cmd /k
是其中之一(因为您没有在新 shell 中键入命令,所以它会在 shell 启动时自动运行)。根据您描述意图的方式(“使用自定义 doskey 文件开始”),我假设您添加 ls . 只是为了测试它是否有效。在这种情况下,只需删除整个
&& ls .
部分并在新窗口中手动尝试ls .
,您会发现它确实有效!如果这不是您想要的,并且您实际上打算从“cmd /k”命令行或批处理文件运行 DOSKEY 宏,那么您就不走运了 - 相反,您可以调用包含函数的批处理文件并使用它,或创建自定义批处理文件而不是宏。
Two issues:
Issue 1
The
&&
will be interpreted by the shell you run this command from and not the shell in the window, so you are runningdoskey /macrofile=1.doskey
in the new window butls .
in the old one.The solution is to surround the whole command that should be passed to the new window with doublequotes:
Alternatively you could escape both
&
characters with^
:...however, this will just uncover issue 2, so keep reading.
Issue 2
As you can deduce (sort of) from the DOSKEY docs, you cannot run a macro from a command that you didn't type in manually (after all, DOSKEY is a tool to process interactive input from a user):
(Emphasis mine.)
This talks only about batch files, but it applies to any non-interactive way of entering a command - and passing it to
cmd /c
orcmd /k
is one of them (since you aren't typing the command into the new shell, it's run automatically on startup of the shell instead).Based on how you described your intent ("use start with custom doskey file"), I assume you added the
ls .
only to test whether it works. In that case, just remove the whole&& ls .
part and tryls .
manually in the new window, and you'll see that it does work!In case that's not what you wanted, and you actually intended to run DOSKEY macros from that "cmd /k" command line or a batch file, you are out of luck - instead, you could call a batch file that contains functions and use that, or create custom batch files instead of macros.