适用于 Windows、Linux、MacOS X 的跨平台脚​​本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2637214/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:16:03  来源:igfitidea点击:

cross-platform scripting for windows, Linux, MacOS X

windowslinuxscriptingmultiplatform

提问by KarolDepka

I'm looking for cross-platform scripting (language) for windows, Linux, MacOS X. I'm tired of .bat / bash .

I would like to do things like for example ,,lock workstation'' at automatic login (I had this in X-Window but the solution was pretty ugly; now, I would like that on MS Windows and not that ugly :-) ).
Generally: automate tasks.

Or would I be better off with Windows Scripting Host?
PowerShell also comes to mind, but that's seems to Windows-only for my taste. Can languages like Python, Ruby, (Java?) interact (elegantly? sensibly?) with WSH?

Also things like DBUS, DCOM, etc come to mind as part of the picture.

Currently I use a mixture of Java, .bat, bash, Ruby, Scala; some VBA for Excel. Which sometimes gets pretty ugly.

I would like a cross-platform general solution with/using ,,native'' parts close to OS-specifics. Like e.g. Ruby driving some Windows-specific stuff (just a guess).
What do You use?
TIA

我正在寻找适用于 Windows、Linux、MacOS X 的跨平台脚​​本(语言)。我厌倦了 .bat / bash 。

我想做一些事情,例如,在自动登录时锁定工作站(我在 X-Window 中有这个,但解决方案非常难看;现在,我希望在 MS Windows 上这样做,而不是那么难看 :-)) .
一般:自动化任务。

还是使用 Windows Scripting Host 会更好?
PowerShell 也出现在我的脑海中,但这似乎是 Windows 的唯一符合我的口味。像 Python、Ruby、(Java?)这样的语言可以(优雅地?明智地?)与 WSH 交互吗?

DBUS、DCOM 等也作为图片的一部分浮现在脑海中。

目前我混合使用 Java、.bat、bash、Ruby、Scala;一些用于 Excel 的 VBA。这有时会变得非常难看。

我想要一个跨平台的通用解决方案,其中包含/使用接近操作系统特定的 ,,native'' 部分。就像 Ruby 驱动一些特定于 Windows 的东西一样(只是猜测)。
你用什么?
TIA

采纳答案by Tomislav Nakic-Alfirevic

I think you're juggling on the edge of contradictory: you would like platform-independent (commendable) but also "close to OS specifics".

我认为您在矛盾的边缘徘徊:您想要独立于平台(值得称赞)但也“接近操作系统细节”。

If, however, you put a bit more emphasis on platform independence, I've been entertaining the idea of using groovy (a more java-friendly relative of ruby) for general purpose scripting. When you need it, you get OS-specific behaviour by invoking OS shell commands.

但是,如果您更加强调平台独立性,那么我一直在考虑使用 groovy(一种对 Java 更友好的 ruby​​ 亲戚)进行通用脚本编写的想法。当您需要它时,您可以通过调用 OS shell 命令获得特定于操作系统的行为。

My motivation is a bit different: I find groovy code to be more robust than that of bash, although I too will need a good multi-platform scripting tool for a project I'm developing.

我的动机有点不同:我发现 groovy 代码比 bash 更健壮,尽管我也需要一个好的多平台脚本工具来开发我正在开发的项目。

回答by Norman Ramsey

I'm a huge fan of Lua:

我是Lua 的忠实粉丝:

  • Syntax is vaguely Pascal-like and works well in scripts.

  • Superb power-to-weight ratio. Superb engineering. Very good design.

  • Extremely portable to any platform with an ANSI C compiler.

  • GUI support through wxLua and other bindings

  • Some support for hiding OS differences in common tasks, e.g., the Lua File System add-on

  • The core system and libraries are simple enough that you can understand allof what you're using, but still have excellent leverage compared to bash/bat. Expressive power is comparable to Python or Ruby.

  • You're not overwhelmed with libraries and frameworks, which can be a plus or a minus.

  • There is an excellentbook: Roberto Ierusalimschy's Programming in Lua; you can get the previous edition free online.

  • Performance beats tcl, perl, python, ruby

  • For even faster performance on x86 hardware, there is LuaJIT.

  • Finally, and this is the ace in the hole: if you run into any kind of platform-specific problem, it is easyto write platform-specific C code and load it into a Lua script dynamically. Lua was designed with this task in mind and does it extremely well. You can also easily dip into C for performance (e.g., compute MD5 checksum).

  • 语法有点像 Pascal 并且在脚本中运行良好。

  • 出色的功率重量比。精湛的工程。非常好的设计。

  • 可移植到任何带有 ANSI C 编译器的平台。

  • 通过 wxLua 和其他绑定支持 GUI

  • 一些支持隐藏常见任务中的操作系统差异,例如 Lua 文件系统附加组件

  • 核心系统和库足够简单,您可以理解您使用的所有内容,但与 bash/bat 相比仍然具有出色的影响力。表达能力可与 Python 或 Ruby 相媲美。

  • 您不会被库和框架所淹没,这可能是有利的,也可能是不利的。

  • 有一本很棒的书:Roberto Ierusalimschy 的 Programming in Lua;您可以在线免费获得以前的版本

  • 性能胜过 tcl、perl、python、ruby

  • 为了在 x86 硬件上获得更快的性能,有 LuaJIT。

  • 最后,这是洞中的王牌:如果您遇到任何特定于平台的问题,很容易编写特定于平台的 C 代码并将其动态加载到 Lua 脚本中。Lua 在设计时就考虑到了这个任务,并且做得非常好。您还可以轻松地使用 C 来提高性能(例如,计算 MD5 校验和)。

Over the last 3 to 5 years, I have been gradually migrating scripts from bash/ksh/awk/sed/grep/perl into Lua. I have been veryhappy with the results.

在过去的 3 到 5 年里,我逐渐将脚本从 bash/ksh/awk/sed/grep/perl 迁移到 Lua。我对结果非常满意。

回答by PiedPiper

Perl and Python are both available on almost every platform

Perl 和 Python 几乎在所有平台上都可用

回答by Simon Hartcher

You could try Batsh

你可以试试Batsh

Batsh is a simple programming language that compiles to Bash and Windows Batch. It enables you to write your script once runs on all platforms without any additional dependency.

Batsh 是一种简单的编程语言,可编译为 Bash 和 Windows Batch。它使您可以编写在所有平台上运行一次的脚本,而无需任何额外的依赖。

回答by Tiberiu Ana

You could write your scripts in Tcl.

您可以在Tcl 中编写脚本。

  • the syntax is simple and closer to what you'd expect from a script;

  • it is cross-platform, and will run on all major platforms;

  • you can easily create simple GUIs for your scripts in Tk, which will also work everywhere and use native controls;

  • for the Windows-specific functions, you can use Twapi(Win32 API bindings).

  • you can install a Tclkit, which is a single file that is the whole Tcl distribution. There's no lengthy install process or hidden files or mysterious directories;

    • you can easily put a linux, windows and mac runtime on a single flash drive so you always have an interpreter handy even if there's not one installed locally.
  • 语法很简单,更接近你对脚本的期望;

  • 它是跨平台的,将在所有主要平台上运行;

  • 您可以轻松地在 Tk 中为您的脚本创建简单的 GUI,它也可以在任何地方使用并使用本机控件;

  • 对于 Windows 特定的功能,您可以使用Twapi(Win32 API 绑定)。

  • 你可以安装一个 Tclkit,它是一个单独的文件,它是整个 Tcl 发行版。没有冗长的安装过程或隐藏文件或神秘目录;

    • 您可以轻松地将 linux、windows 和 mac 运行时放在一个闪存驱动器上,这样即使本地没有安装解释器,您也始终可以方便地使用解释器。

回答by Jonas Elfstr?m

Like e.g. Ruby driving some Windows-specific stuff

就像 Ruby 驱动一些特定于 Windows 的东西

It certainly canand on the Ruby on Windowsblog you can find lots of examples also there's a chapter in the Pickaxe book and in the humbleone.

它当然可以,并且在Ruby on Windows博客上,您可以找到很多示例,在 Pickaxe 书中也有一章和简陋的一章。

回答by Simeon

I would use C# with Mono.

我会将 C# 与 Mono 一起使用。

回答by vlp

there is possibility for UNIX and UNIX like platforms in shell, but I don`t think that this what you are asking for is possible in any scripting language because of windows.

有可能在 shell 中使用 UNIX 和 UNIX 之类的平台,但我认为由于 Windows,您所要求的在任何脚本语言中都不可能。

For UNIX systems you can use this:

对于 UNIX 系统,您可以使用它:

#!/bin/sh

TYPE=`uname`;
echo 'this is ' ${TYPE};

if [ ${TYPE} = HP-UX ]
   then bdf /var;
elif [ ${TYPE} = Linux ]
   then df -h /var;
elif [ ${TYPE} = FreeBSD ]
   then df -k /var;
else echo "Unsupported OS - ${TYPE}"
fi

I hope it will help you!

我希望它会帮助你!

source

来源

回答by Nosk

Not sure if you still need it, but if so, try ant ( http://ant.apache.org/). It's a cross platform "script language". Basically, a ant file is a xml file interpreted by a JVM programm.

不确定您是否仍然需要它,但如果需要,请尝试 ant ( http://ant.apache.org/)。它是一种跨平台的“脚本语言”。基本上,ant 文件是由 JVM 程序解释的 xml 文件。