很久之前, 我就说了, PHP是最好的语言, 是宇宙中最好的语言, 因为方便啊, PHP有几千个估计得上万个函数了: http://php.net/manual/en/indexes.functions.php
所以, 生活工作上, 我也会拿PHP来写些命令行的脚本, 比如这个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php if ($argc < 2) { die(); } if (!is_file($argv[1])) { die(); } $offset = $argv[2] ?? 0; // Search and Replace using Regular Expression with Arithmetic Evaluation echo preg_replace_callback( '~\<z\>(.*)\</z\>~i', // pattern for Z coordinate function ($matches) { global $offset; return "<z>".($matches[1] + $offset)."</z>"; }, file_get_contents($argv[1]) ); |
<?php if ($argc < 2) { die(); } if (!is_file($argv[1])) { die(); } $offset = $argv[2] ?? 0; // Search and Replace using Regular Expression with Arithmetic Evaluation echo preg_replace_callback( '~\<z\>(.*)\</z\>~i', // pattern for Z coordinate function ($matches) { global $offset; return "<z>".($matches[1] + $offset)."</z>"; }, file_get_contents($argv[1]) );
目的是用来把一个XML中 所有 <Z></Z> 标签的数值给改变一个偏移. 命令行里这么用:
php shift.php data.xml 1.0 > new_data.xml
我自己平时需要当然觉得超级好用, 碰巧今天公司的一个 support engineer 在客户的机器上也需要做这一件事情 – 因为软件还不支持这个功能, 只能暂时这样帮助客户来实现 work-around.
这个 support engineer 不知道啥是PHP, 也无法在客户的机器上安装程序(但可以下载PHP移动版). 想来想去, 为了运行上面的PHP脚本特意去下载一大包PHP运行库也不是很友好, 于是只能把上面的脚本给翻译成 WINDOWS 支持的VBS.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | Option Explicit Dim args Set args = Wscript.Arguments If args.Count < 2 Then WScript.Echo "First Parameter: BSM Path. Second Parameter: Z Offset." WScript.Quit End If Dim fs Set fs = CreateObject("Scripting.FileSystemObject") Dim bsm: bsm = args(0) Dim offset: offset = args(1) If Not IsNumeric(offset) Then WScript.Echo "Second Parameter: Not a Number" WScript.Quit End If offset = CDbl(offset) If Not fs.FileExists(bsm) Then WScript.Echo "File Not Found: " & bsm WScript.Quit End If Dim text Set text = fs.OpenTextFile(bsm, 1) Dim objRegEx Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.Global = True objRegEx.IgnoreCase = True objRegEx.Pattern = "<Z\>(.*)\</Z\>" Dim s, matches Do Until text.AtEndOfStream s = text.ReadLine Set matches = objRegEx.Execute(s) If matches.Count > 0 Then WScript.Echo "<Z>" & matches.Item(0).SubMatches.Item(0) + offset & "</Z>" Else WScript.Echo s End If Loop text.Close Set text = Nothing Set fs = Nothing Set args = Nothing Set objRegEx = Nothing |
Option Explicit Dim args Set args = Wscript.Arguments If args.Count < 2 Then WScript.Echo "First Parameter: BSM Path. Second Parameter: Z Offset." WScript.Quit End If Dim fs Set fs = CreateObject("Scripting.FileSystemObject") Dim bsm: bsm = args(0) Dim offset: offset = args(1) If Not IsNumeric(offset) Then WScript.Echo "Second Parameter: Not a Number" WScript.Quit End If offset = CDbl(offset) If Not fs.FileExists(bsm) Then WScript.Echo "File Not Found: " & bsm WScript.Quit End If Dim text Set text = fs.OpenTextFile(bsm, 1) Dim objRegEx Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.Global = True objRegEx.IgnoreCase = True objRegEx.Pattern = "<Z\>(.*)\</Z\>" Dim s, matches Do Until text.AtEndOfStream s = text.ReadLine Set matches = objRegEx.Execute(s) If matches.Count > 0 Then WScript.Echo "<Z>" & matches.Item(0).SubMatches.Item(0) + offset & "</Z>" Else WScript.Echo s End If Loop text.Close Set text = Nothing Set fs = Nothing Set args = Nothing Set objRegEx = Nothing
看吧, PHP几行就能安排的明明白白的事情在VBS上就得啰哩啰嗦的写个几十行. 不过VBScript的好处就是所有WINDOWS版本都支持, 发过去, 客户只需要按照说明在命令行里敲入命令即可跑VBS了.
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK