从WINE中运行的程序执行Shell命令

时间:2021-08-05 21:52:30

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命令?

9 个解决方案

#1


1  

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驱动器映射提供程序的目录。或者,请参阅Anonymous Replier的答案。)

== 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)的功能。我的意思是,你可以修补葡萄酒来做到这一点,但我得到的印象是,这将是更多的工作,而不是它的价值。

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是一个类似Unix的Windows环境。您可以在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更改为安装它的位置。

#2


7  

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?

6.6.3如何将本机程序与Wine中的文件类型相关联?

So you should be able to write shell scripts and call them OK.

所以你应该能够编写shell脚本并调用它们。

#3


5  

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调用它将失败!

#4


4  

Try (where yourprogram is the linux/unix program you want to execute in wine)

尝试(你的程序是你想在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工作的方式。

#5


2  

for me the first solution I found on this site worked - 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脚本解决方案不适用于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语言环境,因此无法使用例如俄语目录名称

#6


1  

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。

#7


0  

The shell script that was listed on WineHQ FAQ can be slightly modified, eg like this:

WineHQ FAQ上列出的shell脚本可以稍作修改,例如:

#!/bin/bash
WFILE=$(echo -E $2)
FILE=$(wine winepath $WFILE)
$1 $FILE

The rest works just as described in the FAQ.

其余的工作正如FAQ中所述。

#8


0  

I love the Far Commander, which does run under wine, so I set up these two scripts:

我喜欢Far Commander,它确实在葡萄酒下运行,所以我设置了这两个脚本:

  1. To launch Linux applications from windows
  2. 从Windows启动Linux应用程序

C:\windows\xt.bat

C:\ WINDOWS \ xt.bat

start /unix /usr/bin/xterm -e %*
  1. To open files in Linux from the wine environment (Far):
  2. 要从wine环境(远程)打开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)/$1)

Now I can type on the Far command line:

现在我可以输入Far命令行:

xt top

xt顶部

xdg SomeDocument.PDF

xdg SomeDocument.PDF

and get results in the Linux environment.

并在Linux环境中获得结果。

#9


0  

How to call a Linux program from a Wine program — five points in terms of API.

如何从Wine程序中调用Linux程序 - 在API方面有五点。

1. Proper PATHEXT

In the new Wine versions (since 2.0.1 at least) it is need to add empty extension (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.

无法将'/ bin / bash'识别为内部或外部命令或批处理脚本。

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 $3 }' )
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 FAQ中,NB:

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

It is likely 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 Wine process is mapped to 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进程不从父Linux进程继承PATH环境变量。请注意,Wine进程中的当前驱动器默认映射到Linux根文件夹,因此无需指定驱动器号。即/ bin / bash只是工作,但不是bash。

Another way is to alter PATH environment variable in Wine accordingly, or to change the current directory.

另一种方法是相应地改变Wine中的PATH环境变量,或者更改当前目录。

Whether 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 Eugene in 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

Linux executable in shared object format cannot be executed from Wine. See: Executables vs Shared objects and 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 exec from Wine in such case. Error message says:

无法从Wine执行共享对象格式的Linux可执行文件。请参阅:可执行文件与共享对象以及如何在64位Linux上从32位Wine执行shell脚本。例如,/ bin / dash可以是“ELF 64位LSB共享对象”(参见文件/ 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

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.请参阅bugreport:CreateProcess在启动Linux程序时没有正确设置hProcess (状态:CLOSED WONTFIX)。

5. Difference in pipes redirection

Perhaps it is a bug in Wine, but parent process should close std handles, that are passed into CreateProcess, just before close self end of the pipes. In Windows this handles can be closed just after CreateProcess function is completed. By MSDN these handles can be closed at once (see CreateProcess function):

也许它是Wine中的一个错误,但是父进程应该关闭std句柄,这些句柄会在关闭管道的自结束之前传递给CreateProcess。在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 pipe in a child Linux process will be closed immediately in such case. But not in the case of a child Windows process.

在Wine 2.0.1中,在这种情况下,子Linux进程中的管道将立即关闭。但不是在Windows子进程的情况下。

#1


1  

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驱动器映射提供程序的目录。或者,请参阅Anonymous Replier的答案。)

== 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)的功能。我的意思是,你可以修补葡萄酒来做到这一点,但我得到的印象是,这将是更多的工作,而不是它的价值。

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是一个类似Unix的Windows环境。您可以在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更改为安装它的位置。

#2


7  

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?

6.6.3如何将本机程序与Wine中的文件类型相关联?

So you should be able to write shell scripts and call them OK.

所以你应该能够编写shell脚本并调用它们。

#3


5  

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调用它将失败!

#4


4  

Try (where yourprogram is the linux/unix program you want to execute in wine)

尝试(你的程序是你想在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工作的方式。

#5


2  

for me the first solution I found on this site worked - 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脚本解决方案不适用于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语言环境,因此无法使用例如俄语目录名称

#6


1  

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。

#7


0  

The shell script that was listed on WineHQ FAQ can be slightly modified, eg like this:

WineHQ FAQ上列出的shell脚本可以稍作修改,例如:

#!/bin/bash
WFILE=$(echo -E $2)
FILE=$(wine winepath $WFILE)
$1 $FILE

The rest works just as described in the FAQ.

其余的工作正如FAQ中所述。

#8


0  

I love the Far Commander, which does run under wine, so I set up these two scripts:

我喜欢Far Commander,它确实在葡萄酒下运行,所以我设置了这两个脚本:

  1. To launch Linux applications from windows
  2. 从Windows启动Linux应用程序

C:\windows\xt.bat

C:\ WINDOWS \ xt.bat

start /unix /usr/bin/xterm -e %*
  1. To open files in Linux from the wine environment (Far):
  2. 要从wine环境(远程)打开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)/$1)

Now I can type on the Far command line:

现在我可以输入Far命令行:

xt top

xt顶部

xdg SomeDocument.PDF

xdg SomeDocument.PDF

and get results in the Linux environment.

并在Linux环境中获得结果。

#9


0  

How to call a Linux program from a Wine program — five points in terms of API.

如何从Wine程序中调用Linux程序 - 在API方面有五点。

1. Proper PATHEXT

In the new Wine versions (since 2.0.1 at least) it is need to add empty extension (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.

无法将'/ bin / bash'识别为内部或外部命令或批处理脚本。

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 $3 }' )
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 FAQ中,NB:

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

It is likely 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 Wine process is mapped to 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进程不从父Linux进程继承PATH环境变量。请注意,Wine进程中的当前驱动器默认映射到Linux根文件夹,因此无需指定驱动器号。即/ bin / bash只是工作,但不是bash。

Another way is to alter PATH environment variable in Wine accordingly, or to change the current directory.

另一种方法是相应地改变Wine中的PATH环境变量,或者更改当前目录。

Whether 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 Eugene in 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

Linux executable in shared object format cannot be executed from Wine. See: Executables vs Shared objects and 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 exec from Wine in such case. Error message says:

无法从Wine执行共享对象格式的Linux可执行文件。请参阅:可执行文件与共享对象以及如何在64位Linux上从32位Wine执行shell脚本。例如,/ bin / dash可以是“ELF 64位LSB共享对象”(参见文件/ 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

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.请参阅bugreport:CreateProcess在启动Linux程序时没有正确设置hProcess (状态:CLOSED WONTFIX)。

5. Difference in pipes redirection

Perhaps it is a bug in Wine, but parent process should close std handles, that are passed into CreateProcess, just before close self end of the pipes. In Windows this handles can be closed just after CreateProcess function is completed. By MSDN these handles can be closed at once (see CreateProcess function):

也许它是Wine中的一个错误,但是父进程应该关闭std句柄,这些句柄会在关闭管道的自结束之前传递给CreateProcess。在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 pipe in a child Linux process will be closed immediately in such case. But not in the case of a child Windows process.

在Wine 2.0.1中,在这种情况下,子Linux进程中的管道将立即关闭。但不是在Windows子进程的情况下。