Linux 从在 WINE 中运行的程序执行 Shell 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6004070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute Shell Commands from Program running in WINE
提问by UKnoHowWeDo
I have a windows program running on Linux using WINE.
我有一个使用 WINE 在 Linux 上运行的 Windows 程序。
How can I call Linux shell commands from the windows program?
如何从 Windows 程序调用 Linux shell 命令?
采纳答案by Daniel Ralston
Edit: user1182474's comment is correct; Wine doesn't isolate the programs it runs. (It tries to hide it, but not very thoroughly.) I totally failed at using Google. Psen's comment below is more correct, and references the FAQ. (Note that, for that to work, you may need to have the program's directory available through a Wine drive mapping. Or, see Anonymous Replier's answer.)
编辑:user1182474 的评论是正确的;Wine 不会隔离它运行的程序。(它试图隐藏它,但不是很彻底。)我完全没有使用谷歌。下面 Psen 的评论更正确,并参考了常见问题解答。(请注意,要使其正常工作,您可能需要通过 Wine 驱动器映射提供程序目录。或者,请参阅匿名回复者的回答。)
== Old Answer ==
== 旧答案 ==
Wine isolates the programs it runs. The applications are, if all works as intended, presented with an environment indistinguishable from Windows. Unfortunately for your purposes, that means that you can't access the features of the host OS (Linux). I mean, you could patch Wine to do that, but I get the impression that it would be more work than it's worth.
Wine 隔离它运行的程序。如果所有应用程序都按预期运行,那么这些应用程序将呈现与 Windows 无法区分的环境。不幸的是,这意味着您无法访问主机操作系统 (Linux) 的功能。我的意思是,你可以修补 Wine 来做到这一点,但我的印象是它会比它的价值更多的工作。
There is hope! Cygwin is a Unix-like environment for Windows. You could install Cygwin in Wine, and use Cygwin to run your shell script. (Apparently, installing with 'winetricks cygwin' is easiest) Invoke Cygwin's bash shell (inside some Wine program) like this:
还有希望!Cygwin 是用于 Windows 的类 Unix 环境。您可以在 Wine 中安装 Cygwin,并使用 Cygwin 运行您的 shell 脚本。(显然,使用 'winetricks cygwin' 安装是最简单的)像这样调用 Cygwin 的 bash shell(在一些 Wine 程序中):
c:\cygwin\bin\bash myscript
Of course, change c:\cygwin to wherever you install it.
当然,将 c:\cygwin 更改为您安装它的任何位置。
回答by Psen
Try this (runs Gnome calculator on my Linux Mint system):
试试这个(在我的 Linux Mint 系统上运行 Gnome 计算器):
wineconsole cmd
...and from the wine console:
...从葡萄酒控制台:
/bin/sh gcalctool
On this general principle, you can also open documents and associate files with a linux app by editing the wine registry. There is a section about it in the wine FAQ:
根据这个一般原则,您还可以通过编辑 wine 注册表来打开文档并将文件与 linux 应用程序关联。在葡萄酒常见问题中有一个关于它的部分:
6.6.3 How do I associate a native program with a file type in Wine?
So you should be able to write shell scripts and call them OK.
所以你应该能够编写 shell 脚本并调用它们。
回答by Anonymous Replier
Try (where yourprogram is the linux/unix program you want to execute in wine)
尝试(其中 yourprogram 是您要在 wine 中执行的 linux/unix 程序)
ln -s /path/to/yourprogram /path/to/wineprefix/drive_c/windows/system32/yourprogram
That is how I have gotten java working.
这就是我让java工作的方式。
回答by David L.
For example:
例如:
Z:\bin\ls
But maybe you are looking rather for something like http://gnuwin32.sourceforge.net/that you will install into your wine "windows"? Or the already mentioned cygwin.
但也许您正在寻找类似http://gnuwin32.sourceforge.net/ 之类的东西,您将安装到您的葡萄酒“窗口”中?或者已经提到的cygwin。
回答by Eugene
for me the first solution I found on this siteworked - associating an extension with winebrowser, and default gnome file viewer launches from wine when clicking on a file in wine explorer (or in other windows applications).
对我来说,我在这个网站上找到的第一个解决方案有效 - 将扩展与 winebrowser 相关联,并且当单击 wine Explorer(或其他 Windows 应用程序)中的文件时,默认的 gnome 文件查看器从 wine 启动。
Previous solution with shell scripts, which worked in wine 1.4, does not work with wine 1.6.
以前在 wine 1.4中工作的shell scripts 解决方案不适用于 wine 1.6。
However, the problem I noticed is that names in Windows encoding are not converted to Linux locale, preventing this to work with e.g. Russian directory names
但是,我注意到的问题是 Windows 编码中的名称不会转换为 Linux 区域设置,从而阻止它与例如俄语目录名称一起使用
回答by Kos Ivantsov
The shell script that was listed on WineHQ FAQcan be slightly modified, eg like this:
WineHQ 常见问题解答中列出的 shell 脚本可以稍作修改,例如:
#!/bin/bash
WFILE=$(echo -E )
FILE=$(wine winepath $WFILE)
$FILE
The rest works just as described in the FAQ.
其余的工作就像常见问题解答中描述的那样。
回答by Adrian
With newer Wine versions (tested with Wine 1.7.38), you can run a Linux program from within Wine in the following way (here to launch gedit, as an example):
使用较新的 Wine 版本(使用 Wine 1.7.38 测试),您可以通过以下方式在 Wine 中运行 Linux 程序(这里以启动 gedit 为例):
wineconsole cmd
...and from that wine console:
...从那个葡萄酒控制台:
start /unix /usr/bin/gedit
If you want to launch a Linux program directly from within a Windows-application, the following line did work for me:
如果您想直接从 Windows 应用程序中启动 Linux 程序,以下行对我有用:
cmd /c start /unix /usr/bin/gedit
To test this, you can call directly on your Linux console this:
要对此进行测试,您可以直接在 Linux 控制台上调用:
wine cmd /c start /unix /usr/bin/gedit
One important thing to Note: the program you want to start needs to have the executable bit set, otherwise calling it from Wine will fail!
需要注意的一件重要事情:您要启动的程序需要设置可执行位,否则从 Wine 调用它会失败!
回答by MKaama
I love the Far Commander, which does run under wine, so I set up these two scripts:
我喜欢Far Commander,它确实在wine下运行,所以我设置了这两个脚本:
- To launch Linux applications from windows
- 从 Windows 启动 Linux 应用程序
C:\windows\xt.bat
C:\windows\xt.bat
start /unix /usr/bin/xterm -e %*
- To open files in Linux from the wine environment (Far):
- 从 wine 环境 (Far) 在 Linux 中打开文件:
C:\windows\xdg.bat
C:\windows\xdg.bat
cd >C:\windows\command\mypwd
start /unix /etc/init.d/winopen.sh %*
/etc/init.d/winopen.sh
/etc/init.d/winopen.sh
#!/bin/sh
PWDF=`winepath -u 'C:\windows\command\mypwd'`
fromdos $PWDF
xdg-open $(winepath -u $(cat $PWDF)/)
Now I can type on the Far command line:
现在我可以在 Far 命令行上输入:
xt top
xdg SomeDocument.PDF
顶
xdg SomeDocument.PDF
and get results in the Linux environment.
并在 Linux 环境中得到结果。
回答by ruvim
How to call a Linux program from a Wine program — five points in terms of API.
如何从 Wine 程序调用 Linux 程序——API 方面的五点。
1. Proper PATHEXT
1.适当 PATHEXT
In the new Wine versions (since 2.0.1 at least) it is need to add emptyextension (i.e. just dot character: .
) into the list of executable file extensions in PATHEXT
environment variable. Without this addition an error message can say something like:
在新的 Wine 版本中(至少从 2.0.1 开始),需要在环境变量中的可执行文件扩展名列表中添加空扩展名(即只是点字符:)。如果没有此添加,错误消息可能会显示如下内容:.
PATHEXT
Can't recognize '/bin/bash' as an internal or external command, or batch script.
Can't recognize '/bin/bash' as an internal or external command, or batch script.
To fix the initial PATHEXT
value in the registry the following commands snippet can be used (for each WINEPREFIX
):
要修复PATHEXT
注册表中的初始值,可以使用以下命令片段(对于每个WINEPREFIX
):
k='HKLM\System\CurrentControlSet\Control\Session Manager\Environment'
pathext_orig=$( wine reg query "$k" /v PATHEXT | tr -d '\r' | awk '/^ /{ print }' )
echo "$pathext_orig" | grep -qE '(^|;)\.(;|$)' \
|| wine reg add "$k" /v PATHEXT /f /d "${pathext_orig};."
This code checks and then modifies PATHEXT
if it doesn't contain .
item only.
此代码检查然后修改PATHEXT
它是否.
仅包含项目。
See also: How do I launch native applications from a Windows application?in WineHQ FAQ; NB:
另请参阅:如何从 Windows 应用程序启动本机应用程序?在 WineHQ 常见问题解答中;注意:
Note that this change will have to be made every time you upgrade Wine, as it will be reverted whenever the wineprefix is updated.
请注意,每次升级 Wine 时都必须进行此更改,因为只要更新 wineprefix,它就会恢复。
2. Path to executable
2. 可执行文件路径
It is likely that by default you will need to specify the full (or relative) path to an executable file (for example, /bin/bash
), since a Wine process doesn't inherit PATH
environment variable from the parent Linux process.
Note that the current drive in a Wine process is mapped to the Linux root folder by default, so no need to specify a drive letter. I.e. /bin/bash
just works, but not bash
.
默认情况下,您可能需要指定可执行文件(例如/bin/bash
)的完整(或相对)路径,因为 Wine 进程不会PATH
从父 Linux 进程继承环境变量。请注意,Wine 进程中的当前驱动器默认映射到 Linux 根文件夹,因此无需指定驱动器号。即/bin/bash
只是工作,但不是bash
。
Another way is to alter PATH
environment variable in Wine accordingly, or to change the current directory.
另一种方法是相应地更改PATH
Wine 中的环境变量,或更改当前目录。
Whenever a path contains non-ASCII characters — as argument of CreateProcessA
— the path should be in Wine locale and according to LANG
environment variable; see also the answer by Eugenein this topic, and a forum post how to set encoding to use with non-Unicode application in Wine. For CreateProcessW
the path should be in UTF-16 in any case.
当路径包含非 ASCII 字符时——作为参数CreateProcessA
——路径应该在 Wine 语言环境中并根据LANG
环境变量;另请参阅Eugene在本主题中的回答,以及如何设置编码以与 Wine 中的非 Unicode 应用程序一起使用的论坛帖子。因为CreateProcessW
在任何情况下路径都应该是 UTF-16。
3. Executable format
3. 可执行格式
Linux executables in shared object format cannot be executed from Wine. See: Executables vs Shared objectsand How to execute shell scripts from 32-bit Wine on 64-bit Linux. For example, /bin/dash
can be "ELF 64-bit LSB shared object" (see output of file /bin/dash
) and cannot be executed from Wine in such case. Error message says:
共享对象格式的 Linux 可执行文件不能从 Wine 执行。请参阅:可执行文件与共享对象以及如何在 64 位 Linux 上从 32 位 Wine 执行 shell 脚本。例如,/bin/dash
可以是“ELF 64 位 LSB 共享对象”(请参阅 的输出file /bin/dash
)并且在这种情况下不能从 Wine 执行。错误信息说:
wine: Bad EXE format for Z:\bin\dash..
Can't recognize '/bin/dash' as an internal or external command, or batch script.
4. No waiting
4.无需等待
A parent Wine process cannot wait (for example, via WaitForSingleObject
) on a child Linux process since it isn't provided with the child process handle — it is just 0. See bugreport: CreateProcess doesn't set hProcess correctly when starting a Linux program(Status: CLOSED WONTFIX).
父 Wine 进程不能WaitForSingleObject
在子 Linux 进程上等待(例如,通过),因为它没有提供子进程句柄 - 它只是 0。请参阅错误报告:启动 Linux 程序时 CreateProcess 未正确设置 hProcess(状态:关闭 WONTFIX)。
Nevertheless a parent process can indirectly wait a child process via blocking read on the certain pipe if the child process uses stdout (see also bellow).
然而,如果子进程使用 stdout(另见下文),父进程可以通过在特定管道上阻塞读取来间接等待子进程。
5. Difference in pipes redirection
5.管道重定向的区别
Perhaps it is a bug in Wine, but a parent process should close the std handles, that are passed into CreateProcess, only after close the own handles (or just before it) for the corresponding pipes. While in Windows these handles can be closed just after CreateProcess
function is completed. By MSDN these handles may be closed at once after passing (see CreateProcess function):
也许这是 Wine 中的一个错误,但是父进程应该关闭传递给 CreateProcess 的 std 句柄,只有在关闭相应管道的自己的句柄(或就在它之前)之后。而在 Windows 中,这些句柄可以在CreateProcess
功能完成后立即关闭。通过 MSDN,这些句柄可能会在传递后立即关闭(请参阅CreateProcess 函数):
Handles in STARTUPINFO or STARTUPINFOEX must be closed with CloseHandle when they are no longer needed.
STARTUPINFO 或 STARTUPINFOEX 中的句柄在不再需要时必须用 CloseHandle 关闭。
In Wine 2.0.1, the corresponding pipe in a child Linux process will be closed immediately in such case, and the child process will prematurely stop. But not in the case of a child Windows process.
在 Wine 2.0.1 中,Linux 子进程中对应的管道会立即关闭,子进程会提前停止。但不是在子 Windows 进程的情况下。