|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 16 楼』:
使用 LLM 解释/回答一下
唔,果然最后的总结工作是由willsort来完成,只有willsort与无奈何两位严谨的态度才适合发这种理论性的贴。But,俺还是班门弄弄斧,关二爷面前耍耍大刀:
2.5.可以用%var:~1%来去掉前引号。
2.6.可以用%var:~1,-1%来去掉前后引号
2.7.可以用%var:~0,-1%来去掉后引号。
Last edited by 3742668 on 2006-5-28 at 02:02 ]
Well, it turns out that the final summary work is done by willsort. Only the rigorous attitudes of willsort and Wunaaihe are suitable for posting this theoretical post. But, I'm still showing off my skills, showing off in front of Guan Yunchang:
2.5. You can use %var:~1% to remove the leading quote.
2.6. You can use %var:~1,-1% to remove both leading and trailing quotes
2.7. You can use %var:~0,-1% to remove the trailing quote.
Last edited by 3742668 on 2006-5-28 at 02:02 ]
|
|
2006-5-28 01:49 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 17 楼』:
使用 LLM 解释/回答一下
三位都是大家,我是灌水的。努力学习了!
All three of you are great experts, and I'm just here to post casually. I've been studying hard!
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2006-5-28 02:50 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 18 楼』:
使用 LLM 解释/回答一下
Re 3742668:
新的小结被编辑到顶楼贴中方便浏览,并将你的建议分别并入到2-1、2-3、2-5中。
正如无奈何兄所言,集体的智慧远大于个体智慧的总和。现在想到另外一道与引号相关的课题,也提请大家讨论。
命令行参数的防御错误
我过去的代码中,在if语句中比较命令命令行参数时,总喜欢使用类似if "%1"=="string1" ...的格式,现在想来,这个习惯是很有些问题的。
因为使用引号的目的在于防御串中可能出现的转义字符,而在命令行参数中,串值在经过CMD的命令行解析后,所有的可能被转义的转义字符应该都已发挥作用而被脱去,剩下的转义字符应该都是在命令行中就被预先防御过。所以,%1中不可能含有空格或其它转义字符而没有引号防御,也因此再在%1外使用引号就成了多余。
再进一步讲,如果%1是经过引号防御转义的字符串,比如"C:\Program files",此时再使用"%1"引用则变成了""C:\Program files"",引号被重新匹配,因而转义字符被暴露出来,导致程序出错,因此此时使用引号就成了错误。
那么,我们是否不需要再if语句中对%1进行防御呢?答案是否定的,因为if中防御字符的出现,最初是为了防御%1为空,就是类似if "%1"=="" ...的格式,所以我一度曾称呼防御字符为“防空字符”。如果不对%1进行防御,则再无参数调用批处理执行到此句会出现语法错误。
至此,我们最好的选择就是将%1脱引号后再使用引号进行防御。比如 if "%~1"=="C:\Program file" ... 或 if exist "%~1" .... 。这确实是很好的办法。
但是,脱引号的实现只有在CMD中才比较简单,而我的批处理很多还在9x和DOS下运行,所以,我需要退而求其次想其它的办法,比如选取其它字符作为命令行参数串的防御字符。在串值比较的if语句中,可选的防御字符有很多,几乎大多非转义的字符都可用来防御。比如我早期常用的 if == ... 。
然而,还需要考虑的是,防御字符还会出现在其它语句中,比如文件存在判断语句if exist中。如果我们使用if exist ... 的范式,则程序的流程显然将脱出我们预期之外。尽管在我通常的惯例是 if == ... 之后再 if exist %1 ... 。
故而,为了保持代码可阅读性,我需要使用风格一致的的防御字符,所以我们剩下的选择就很少了。或许,我们可以使用后缀的句点。比如 if %1.==. ... 或 if exist %1. ...
它可以解决串为文件名时的部分防御问题,但当串为路径. 或者..时,if exist %1. 显然又会出错。
那么还存在更好的解决方案吗?
Re 3742668:
A new summary has been edited into the top post for easy browsing, and your suggestions have been incorporated into 2-1, 2-3, and 2-5 respectively.
As Brother Wu Naihe said, the wisdom of the collective is far greater than the sum of the wisdom of individuals. Now another topic related to quotes is thought of, and everyone is invited to discuss it.
Defensive Errors in Command Line Arguments
In my past code, when comparing command line arguments in an if statement, I always liked to use a format like if "%1"=="string1" ... Now, thinking about it, this habit has quite some problems.
Because the purpose of using quotes is to defend against possible escape characters in the string. However, after the command line parsing by CMD, all possible escape characters that could be escaped should have already been processed and removed, and the remaining escape characters should have been pre-defended in the command line. Therefore, %1 cannot contain spaces or other escape characters without being defended by quotes, and thus using quotes outside %1 is redundant.
Further speaking, if %1 is a string that has been defended by quotes, such as "C:\Program files", then using "%1" to reference it becomes ""C:\Program files"", the quotes are re-matched, and thus the escape characters are exposed, causing the program to go wrong. Therefore, using quotes at this time is wrong.
Then, do we not need to defend %1 in the if statement? The answer is no. Because the defense character in if was originally to defend against %1 being empty, like the format if "%1"=="" ... So I once called the defense character an "empty defense character". If we do not defend %1, then when the batch processing is executed without parameters, a syntax error will occur at this sentence.
At this point, our best choice is to remove the quotes from %1 and then use quotes for defense. For example, if "%~1"=="C:\Program file" ... or if exist "%~1" .... This is indeed a very good way.
However, the implementation of removing quotes is relatively simple only in CMD. And many of my batch processes still run under 9x and DOS, so I need to fall back to think of other methods. For example, select other characters as the defense characters for the command line argument string. There are many optional defense characters in the if statement for string comparison. Almost most non-escape characters can be used for defense. For example, the if == ... that I used early.
However, it also needs to be considered that the defense character will also appear in other statements, such as the file existence judgment statement if exist. If we use the paradigm if exist ..., then the process of the program will obviously deviate from our expected one. Although my usual practice is to have if == ... and then if exist %1 ... .
Therefore, in order to maintain the readability of the code, I need to use a consistent defense character, so our remaining choices are few. Perhaps, we can use a trailing period. For example, if %1.==. ... or if exist %1. ...
It can solve the partial defense problem when the string is a file name, but when the string is a path . or .., if exist %1. will obviously go wrong.
So is there a better solution?
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-5-28 23:42 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 19 楼』:
使用 LLM 解释/回答一下
好长一篇。在我的习惯中,一般能不用到if就会尽量不用if语句,而尽可能地用echo,dir等等命令来操作变量并根据错误返回值来进行相应的操作。即便是使用 if "%~1"==的格式,我想,如果%1中包含有"=="等不可预料的,类似if语句结构的字符串(牛角尖?)时,也难免出错(没有试验过,只不过是很早的时候的一种想法,从而导致一直以来的习惯)。对于if语句来说,或许能不用就不用就是最好的方法吧。当然,这只是我个人菲薄的认识而已,毕竟当初浅尝辄止并未深究。已见willsort官方理论,期待 无奈何 之高见,呵呵。
ps:此贴已被加精华贴,各位看官赶快留个爪印吧,到cn-dos来还是第一次混进传说中的精华贴.
It's a long post. In my habit, generally, I try to avoid using the if statement as much as possible and instead use commands like echo, dir, etc. to operate on variables and perform corresponding operations based on the error return values. Even when using the format of if "%~1"==, I think that if %1 contains strings like "==" that are unpredictable and similar to the structure of the if statement (a bit of a nitpick?), there will inevitably be errors (I haven't tested it, but it's just an idea from a long time ago, which led to my habit all along). For the if statement, maybe not using it when possible is the best approach. Of course, this is just my humble opinion. After all, I just dabbled in it and didn't delve deeply. I have seen the official theory of willsort, looking forward to the insights of Wunaiaohe, heh heh.
ps: This post has been highlighted as an essence post. All readers, hurry up and leave a mark. This is the first time I've mixed into the legendary essence post when coming to cn-dos.
|
|
2006-5-29 13:24 |
|
|
tclgb
初级用户
  小子
积分 76
发帖 26
注册 2007-6-20
状态 离线
|
『第 20 楼』:
使用 LLM 解释/回答一下
反复看了三遍,好
Read it three times repeatedly, good
|
|
2007-7-8 17:24 |
|
|
rockdong
初级用户
 
积分 48
发帖 25
注册 2007-7-30
状态 离线
|
|
2007-8-23 14:28 |
|
|
upsco
初级用户
 
积分 67
发帖 32
注册 2007-11-19
状态 离线
|
『第 22 楼』:
使用 LLM 解释/回答一下
2-4 、可以使用set "var=%var%脱去环境变量var 串尾的最后一个引号,如果串尾
不存在引号则环境变量被清空;
是不是因为版本的原因呀,我在XP中如果var中没引号,set "var=%var%值不变
2-4. You can use set "var=%var% to remove the last quote at the end of the string of environment variable var. If there is no quote at the end, the environment variable is cleared;
Is it because of the version? In XP, if there is no quote in var, the set "var=%var% value remains unchanged
|
|
2007-11-19 20:46 |
|
|
upsco
初级用户
 
积分 67
发帖 32
注册 2007-11-19
状态 离线
|
『第 23 楼』:
使用 LLM 解释/回答一下
原来set "var=%var%,如果var
1、不含引号,数值不变
2、如果有引号,且不在首位,保留倒数第一个引号前的值
3、如果有引号都在串首,清空变量
Original text contains Chinese and the content is about some logic description. Since it's not a complete English sentence but rather a Chinese - structured description, the translation would be:
Original Chinese content: 原来set "var=%var%,如果var
1、不含引号,数值不变
2、如果有引号,且不在首位,保留倒数第一个引号前的值
3、如果有引号都在串首,清空变量
Translated content: Originally set "var=%var%, if var
1. If there are no quotes, the value remains unchanged
2. If there are quotes and they are not at the first position, keep the value before the last occurrence of the quote
3. If all quotes are at the beginning of the string, clear the variable
|
|
2007-11-19 21:16 |
|
|
laihaibin08
新手上路

积分 13
发帖 15
注册 2008-10-19
状态 离线
|
|
2008-10-19 17:53 |
|
|