batch-file – 在Windows批处理文件中回显精确字符串?
发布时间:2020-12-13 22:29:49 所属栏目:Windows 来源:网络整理
导读:我正在尝试回显存储在变量中的字符串,但它似乎需要大量的转义.我正在尝试使用以下代码: setlocal EnableDelayedExpansion@echo offset "grass=@##!$^%**()"echo !grass! 我想逐字回显变量草,所以我在输出中看到@ ##!$^%**().应该做什么?谢谢! 解决方法
我正在尝试回显存储在变量中的字符串,但它似乎需要大量的转义.我正在尝试使用以下代码:
setlocal EnableDelayedExpansion @echo off set "grass=@##&!$^&%**(&)" echo !grass! 我想逐字回显变量草,所以我在输出中看到@ ##&!$^&%**(&).应该做什么?谢谢! 解决方法
回声!草!将始终逐字回显当前值,而无需任何转义.你的问题是,价值不是你想象的那样!当您尝试设置值时,会发生此问题.
设置值的正确转义序列是 set "grass=@##&^!$^^&%%**(&)" 现在来解释一下.你需要的信息埋藏在How does the Windows Command Interpreter (CMD.EXE) parse scripts?年.但它有点难以理解. 你有两个问题: 1)每次解析该行时,%必须以%%的形式进行转义.引号的存在与否都没有区别.延迟扩张状态也没有区别. set pct=% :: pct is undefined set pct=%% :: pct=% call set pct=%% :: pct is undefined because the line is parsed twice due to CALL call set pct=%%%% :: pct=% 2)A!文字必须被转义为^!每当它被解析器的延迟扩展阶段解析.如果一行包含!在延迟扩展期间的任何地方,然后^文字必须转义为^^.但是对于解析器的特殊字符阶段,^也必须被引用或转义为^^.由于CALL会使任何^字符加倍,这可能会进一步复杂化. (抱歉,描述问题非常困难,解析器很复杂!) setlocal disableDelayedExpansion set test=^^ :: test=^ set "test=^" :: test=^ call set test=^^ :: test=^ :: 1st pass - ^^ becomes ^ :: CALL doubles ^,so we are back to ^^ :: 2nd pass - ^^ becomes ^ call set "test=^" :: test=^^ because of CALL doubling. There is nothing that can prevent this. set "test=^...!" :: test=^...! :: ! has no impact on ^ when delayed expansion is disabled setlocal enableDelayedExpansion set "test=^" :: test=^ :: There is no ! on the line,so no need to escape the quoted ^. set "test=^!" :: test=! set test=^^! :: test=! :: ! must be escaped,and then the unquoted escape must be escaped set var=hello set "test=!var! ^^ ^!" :: test=hello ^ ! :: quoted ^ literal must be escaped because ! appears in line set test=!var! ^^^^ ^^! :: test=hello ^ ! :: The unquoted escape for the ^ literal must itself be escaped :: The same is true for the ! literal call set test=!var! ^^^^ ^^! :: test=hello ^ ! :: Delayed expansion phase occurs in the 1st pass only :: CALL doubling protects the unquoted ^ literal in the 2nd pass call set "test=!var! ^^ ^!" :: test=hello ^^ ! :: Again,there is no way to prevent the doubling of the quoted ^ literal :: when it is passed through CALL (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践 Week4 2
- .net – 桌面应用程序中的WPF与Windows窗体
- windows-server-2003 – SYSTEM用户在Win 2003服务器上安排
- windows-7 – 删除某些文件位于Windows 7上的“准备回收”?
- windows – Dockerfile – 在一个RUN命令中删除文件,它仍然
- windows-7 – 跨平台3D(C#)
- 在Windows上的Ruby中杀死进程和子进程
- DOS命令FindStr
- windows – 无缝远程桌面连接
- 简单的Windows Linux服务器配置? Chef / Puppet / Ansible
推荐文章
站长推荐
- windows-8 – WinRT HttpClient阻止闪屏
- command-line-interface – Windows 8.1的wget命
- 批处理文件 – 批处理文件IF%time%不工作
- windows-server-2008-r2 – 在两个域控制器上安装
- windows-7 – 使用CScript运行Javascript?
- windows-server-2008 – 允许人力资源部门将用户
- windows – 为什么我们需要cmake?
- exe和dll之间的接口与不同的C/C++运行时库
- Userprofile register windows 位置
- Windows批处理文件 – 如果窗口标题包含文本,则为
热点阅读