“ cmd / s”是做什么用的? [英] What is `cmd /s` for?

查看:1523
本文介绍了“ cmd / s”是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows命令提示符( cmd.exe )具有可选的 / s 参数,它修改了的行为/ c (运行特定命令,然后退出)或 / k (运行特定命令,然后显示shell提示)。这个 / s 参数显然与一些神秘的报价处理有关。



文档令人困惑,但据我所知,当您执行 cmd / c 某事 以及 某物 包含引号,然后默认情况下 cmd 有时会删除这些引号,而 / s 告诉它不要理会它们。



我不明白的是引号删除会破坏任何内容,因为那是唯一的时间 / s (禁止默认的引用删除行为)是必要的。它仅在某些不可思议的条件下删除引号,并且这些条件之一是 / c 之后的第一个字符必须是引号。因此,它不会删除引号周围的引号;它要么是删除正在运行的EXE路径的引号,要么是删除整个命令行的引号(或者可能是删除命令行的前半部分,这很奇怪)。




  • 如果引用了EXE的路径,例如 cmd / c c:\tools\foo.exe arg1 arg2 ,则不需要引号,如果 cmd 想要删除它们,很好。 (如果路径名称中有空格,则不会删除它们-这是另一条不可思议的规则。)我无法想象有任何理由禁止删除引号,因此 / s 似乎不必要。

  • 如果用引号括住整个命令行,例如 cmd / c foo.exe arg1 arg2 ,则似乎必须删除引号,因为没有名为 foo.exe arg1的EXE。 arg2 在系统上;因此,似乎选择使用 / s 取消报价删除实际上会破坏事情。 (但是实际上,它并没有破坏事物: cmd / s / c foo.exe arg1 arg2 可以正常工作。)



/ s 有一些微妙之处吗?什么时候有必要?

解决方案

Cmd / S非常有用,因为它使您不必担心用引号引起来 。回想一下 / C 参数的意思是执行该命令,就像我在提示符下键入命令然后退出一样。



因此,如果要传递给CMD.exe的命令很复杂,则必须记住CMD的参数引用规则,并正确地转义所有引号,或者使用 / S ,这会触发特殊的非解析规则 Strip first and last 并对待所有其他字符



您将在需要利用CMD Shell功能的地方使用它,而不是直接调用另一个程序。例如,环境变量扩展,输出或输入重定向,或使用CMD.exe内置。



示例:



使用内置的Shell:如果在提示符下键入 DEL / Q / S%TMP%\TestFile ,则执行该操作:

  CMD.exe / S / C DEL / Q / S%TMP%\TestFile 

这将执行SomeCommand.exe,将标准输出重定向到临时文件,并将标准错误重定向到同一位置:

  CMD.exe / S / C%UserProfile%\SomeCommand.exe> %TMP%\TestOutput.txt 2>& 1 

那么 / S 还可以给您带来额外的好处吗?主要是它使您不必担心引用引号,也可以在不确定环境变量是否包含引号的地方提供帮助。 code> / S ,并在开头和结尾处加上引号。



模糊相关:Bourne Shell中的$ *。



某些背景



回想一下main()的参数列表是一个C-ism和Unix-ism。Unix / Linux shell(例如Bourne Shell等)解释命令行,取消引用参数,将通配符如 * 扩展到列表



因此,如果您说:

  $ vi * .txt 

vi命令例如会看到以下参数:

  vi 
a.txt
b.txt
c.txt
d.txt

这是因为unix / linux在内部基于参数列表进行操作。



最终从CP / M和VAX派生的Windows在内部未使用此系统。对于操作系统,命令行只是一个字符串。被调用程序负责解释命令行,扩展文件范围( * 等)并处理未引用引号的参数。



因此,C期望的参数必须由C运行时库修改。操作系统只提供一个带有参数的字符串,如果您的语言不是C(或者即使是),则它可能不会解释为根据Shell规则引用的以空格分隔的参数,而是完全不同的东西。 / p>

The Windows command prompt (cmd.exe) has an optional /s parameter, which modifies the behavior of /c (run a particular command and then exit) or /k (run a particular command and then show a shell prompt). This /s parameter evidently has something to do with some arcane quote handling.

The docs are confusing, but as far as I can tell, when you do cmd /csomething, and the something contains quotation marks, then by default cmd will sometimes strip off those quotes, and /s tells it to leave them alone.

What I don't understand is when the quote removal would break anything, because that's the only time /s ("suppress the default quote-removal behavior") would be necessary. It only removes quotes under a certain arcane set of conditions, and one of those conditions is that the first character after the /c must be a quotation mark. So it's not removing quotes around arguments; it's either removing quotes around the path to the EXE you're running, or around the entire command line (or possibly around the first half of the command line, which would be bizarre).

  • If the path to the EXE is quoted, e.g. cmd /c "c:\tools\foo.exe" arg1 arg2, then quotes are unnecessary, and if cmd wants to remove them, fine. (It won't remove them if the path has a space in the name -- that's another of the arcane rules.) I can't imagine any reason to suppress the quote removal, so /s seems unnecessary.
  • If the entire command line is quoted, e.g. cmd /c "foo.exe arg1 arg2", then it seems like quote removal would be a necessity, since there's no EXE named foo.exe arg1 arg2 on the system; so it seems like opting out of quote removal using /s would actually break things. (In actual fact, however, it does not break things: cmd /s /c "foo.exe arg1 arg2" works just fine.)

Is there some subtlety to /s that's eluding me? When would it ever be necessary? When would it even make any difference?

解决方案

Cmd /S is very useful as it saves you having to worry about "quoting quotes". Recall that the /C argument means "execute this command as if I had typed it at the prompt, then quit".

So if you have a complicated command which you want to pass to CMD.exe you either have to remember CMD's argument quoting rules, and properly escape all of the quotes, or use /S, which triggers a special non-parsing rule of "Strip first and last " and treat all other characters as the command to execute unchanged".

You would use it where you want to take advantage of the capabilities of the CMD shell, rather than directly calling another program. For example environment variable expansion, output or input redirection, or using CMD.exe built-ins.

Example:

Use a shell built-in: This executes as-if you had typed DEL /Q/S "%TMP%\TestFile" at the prompt:

CMD.exe /S /C " DEL /Q/S "%TMP%\TestFile" "

This executes SomeCommand.exe redirecting standard output to a temp file and standard error to the same place:

CMD.exe /S /C " "%UserProfile%\SomeCommand.exe" > "%TMP%\TestOutput.txt" 2>&1 "

So what does /S give you extra? Mainly it saves you from having to worry about quoting the quotes. It also helps where you are unsure whether for example an environtment variable contains quote characters. Just say /S and put an extra quote at the beginning and end.

Vaguely Related: $* in Bourne Shell.

Some background

Recall that the list of arguments to main() is a C-ism and Unix-ism. The Unix/Linux shell (e.g. Bourne Shell etc) interprets the command line, un-quotes the arguments, expands wildcards like * to lists of files, and passes a list of arguments to the called program.

So if you say:

$ vi *.txt

The vi command sees for example these arguments:

vi
a.txt
b.txt
c.txt
d.txt

This is because unix/linux operates internally on the basis of "list of arguments".

Windows, which derives ultimately from CP/M and VAX, does not use this system internally. To the operating system, the command line is just a single string of characters. It is the responsibility of the called program to interpret the command line, expand file globs (* etc) and deal with unquoting quoted arguments.

So the arguments expected by C, have to be hacked up by the C runtime library. The operating system only supplies a single string with the arguments in, and if your language is not C (or even if it is) it may not be interpreted as space-separated arguments quoted according to shell rules, but as something completely different.

这篇关于“ cmd / s”是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆