如何从Windows上的Ruby运行非ASCII / Unicode shell命令?
发布时间:2020-12-14 05:42:34 所属栏目:Windows 来源:网络整理
导读:我无法弄清楚编写一个 shell命令在 Windows上从 Ruby运行的正确方法.以下脚本重现了该问题: # encoding: utf-8def test(word) returned = `echo #{word}`.chomp puts "#{word} == #{returned}" raise "Cannot roundtrip #{word}" unless word == returneden
我无法弄清楚编写一个
shell命令在
Windows上从
Ruby运行的正确方法.以下脚本重现了该问题:
# encoding: utf-8 def test(word) returned = `echo #{word}`.chomp puts "#{word} == #{returned}" raise "Cannot roundtrip #{word}" unless word == returned end test "good" test "b?d" puts "Success" # win7,cmd.exe font set to Lucinda Console,chcp 65001 # good == good # b?d == b??d 这是Ruby中的错误,还是我需要手动将命令字符串编码为特定的编码,然后才能传递给cmd.exe进程? 更新:我想清楚地说明问题不在于将输出读回Ruby,而是纯粹将命令发送到shell.展示: # encoding: utf-8 File.open("bbb?d.txt","w") do |f| f.puts "nothing to see here" end filename = Dir.glob("bbb*.txt").first command = "attrib #{filename}" puts command.encoding puts "#{filename} exists?: #{ File.exists?(filename) }" system command File.delete(filename) #=> # UTF-8 # bbb?d.txt exists?: true # File not found - bbb??d.txt 您可以看到文件是否正确创建,File.exists?方法确认Ruby可以看到它,但是当我尝试在其上运行attrib命令时,它试图使用不同的文件名. 解决方法
尝试像这样设置环境变量LC_CTYPE:
LC_CTYPE=en_US.UTF-8 在命令shell或Ruby脚本内全局设置: ENV['LC_CTYPE']='en_US.UTF-8' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 给定选择,混合模式程序集与单独的互操作DLL的优缺点
- BMP格式详解
- 如何管理XP,Vista和Win 7客户端的Windows组策略?
- 命名空间“Microsoft.WindowsAzure”中不存在类型或命名空间
- 批处理文件 – 使用通配符删除文件夹/子文件夹的Windows批处
- windows – 句柄关闭后,哪种类型的文件会自行删除?
- windows – 如何同时启动多个程序?
- windows-server-2008 – 混合以太网速度的Windows 2008 Ser
- active-directory – 阻止EXE在XP域环境中执行
- .net – 我将如何执行WPF Windows应用程序的许可
推荐文章
站长推荐
- windows-7 – Windows 7 RC在BootCamp上与MacBoo
- msi – 是否可以重新启动管理安装点?
- 完全复制Windows中的应用程序安装
- Windows – 用于笔记本电脑的Accelerometer API
- 在安装了libv8的windows上安装therubyracer –wi
- 使用MinGW在Windows上编译小型Gcc项目
- windows-server-2008 – 单向网络连接
- winapi – 使用Win32 API的Windows“真实”用户列
- windows-phone-7 – WP7.5全景页面中的AppBar
- windows-installer – 如何合并模块升级?
热点阅读