是否有任何语言可以编译为 Bash?

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

Are there any languages that compile to Bash?

bashshellscriptingcoffeescript

提问by Aseem Kishore

I both love and hate writing Bash. I love that it's sostreamlined for operating on files and working with processes (I agree with this popular questionthat it's way better in this regard than Python, Ruby, etc.), but I hatethe syntax, particularly around conditionals, loops, etc.

我既喜欢又讨厌编写 Bash。我喜欢它在操作文件和处理进程方面如此简化(我同意这个流行的问题,它在这方面比 Python、Ruby 等要好得多),但我讨厌语法,尤其是在条件、循环等方面.

(This is subjective, but I find it both confusing and annoying. E.g. $varwhen reading, but varwhen writing; writes silently fail if there are spaces around =; the double brackets in ifs when using regexp; double semicolons sometimes and single semicolons others; etc.)

(这是主观的,但我觉得它既令人困惑又令人讨厌。例如,$var在阅读时,但var在写作时;如果周围有空格,则写入失败=;使用正则表达式时 ifs 中的双括号;有时使用双分号,其他使用单分号;等等。 )

As a huge fan of CoffeeScript, which compiles to JS, I've been wondering: are there any languages that have the aesthetic/syntax of languages like Python/Ruby/CoffeeScript but which compile and run as Bash instead of one of those other runtimes?

作为编译为 JS 的 CoffeeScript 的忠实粉丝,我一直想知道:是否有任何语言具有 Python/Ruby/CoffeeScript 等语言的美学/语法,但作为 Bash 编译和运行而不是其他运行时之一?

E.g. I'd love to be able to write mostly-Bash with just a bit simpler syntax:

例如,我希望能够使用更简单的语法编写主要是 Bash:

$AGGREGATE_FILENAME = 'allfiles.txt'

if not exists $AGGREGATE_FILENAME
    touch $AGGREGATE_FILENAME

for $file in files/*
    cat $file >> $AGGREGATE_FILENAME

switch 
    case 'test'
        run-tests
        echo 'Tests finished!'
    case 'deploy'
        echo 'Packaging...'
        mv foo bar/
        deploy-bar

This is a super contrived example, and the syntax is a strawman (mostly inspired from CoffeeScript but keeping the essential Bash notions of first-class commands, separated from variables, and loose typing).

这是一个超级人为的例子,语法是一个稻草人(主要受 CoffeeScript 的启发,但保留了一流命令的基本 Bash 概念,与变量分开,和松散类型)。

Anyway, just a question and food for thought. I'd loveto be able to write my scripts in something nicer than Bash. =) Thanks!

无论如何,只是一个问题和思考的食物。我很喜欢到能写我的剧本在更好的东西比猛砸。=) 谢谢!

采纳答案by Aseem Kishore

Since I originally asked this question, two projects have been released which attack this problem and do a pretty good job. Both reimplement many/most Unix tools in more programming-friendly runtimes.

自从我最初提出这个问题以来,已经发布了两个项目,它们解决了这个问题并且做得很好。两者都在更易于编程的运行时中重新实现了许多/大多数 Unix 工具。

Plumbum is implemented in Python and looks pretty solid:

Plumbum 是用 Python 实现的,看起来非常可靠:

http://plumbum.readthedocs.org/en/latest/index.html

http://plumbum.readthedocs.org/en/latest/index.html

ShellJS is implemented on Node.js and also looks pretty good:

ShellJS 是在 Node.js 上实现的,看起来也很不错:

https://github.com/arturadib/shelljs

https://github.com/arturadib/shelljs

Exciting developments! I'm looking forward to trying them out. If you already have, it'd be great to hear your experiences in the comments. Thanks!

令人兴奋的发展!我期待着尝试它们。如果你已经有了,很高兴在评论中听到你的经历。谢谢!

回答by BYVoid

You could also try Batsh, which is a DSL (Domain-Specific Language) that compiles a C-syntax language to Bash (and Windows Batch).

您还可以尝试 Batsh,它是一种 DSL(域特定语言),可将 C 语法语言编译为 Bash(和 Windows Batch)。

回答by masukomi

Bish is another option:

Bish 是另一种选择:

https://github.com/tdenniston/bish

https://github.com/tdenniston/bish

Shell scripting with a modern feel.

Bish is a lightweight language created to bring shell scripting into the 21st century. It gives programmers the comfort of modern syntax but compiles to Bash, resulting in good portability (in as much as Bash is portable).

具有现代感的 Shell 脚本。

Bish 是一种轻量级语言,旨在将 shell 脚本带入 21 世纪。它为程序员提供了现代语法的舒适性,但编译为 Bash,从而具有良好的可移植性(与 Bash 的可移植性一样)。

回答by coderofsalvation

I tried all of the above (results) and started powscript.

我尝试了以上所有(结果)并启动了powscript

Differences powscriptvs the tools above

powscript与上述工具的差异

  • extremely portable preprocessor (100% bash)
  • balances between coffeescript and bash
  • hasslefree portable all-in-one-file compiler/runtime, written in bash
  • loose transpiler: inline bash always possible
  • 极其便携的预处理器(100% bash)
  • 在 coffeescript 和 bash 之间取得平衡
  • 用 bash 编写的无障碍便携式一体化文件编译器/运行时
  • 松散的转译器:内联 bash 总是可能的

回答by Miguel Pérez

The problem is that the whole strings-based semantics of Bash is so horribly broken, it'd be pretty difficult to do something like CoffeeScript for Bash.

问题是 Bash 的整个基于字符串的语义都被严重破坏了,很难为 Bash 做像 CoffeeScript 这样的事情。

Since you probably don't need function-level interoperability to call functions that are written in Bash, you're better off using something entirely different. Perl is close to Bash in being nasty and full of shortcuts and weird syntax, but its semantics are mostly sound. Python is less comfortable for things such as launching processes but is far better for general systems programming, clean and easy to maintain. Python has great libraries and modules for everything; Perl even better.

因为您可能不需要函数级互操作性来调用用 Bash 编写的函数,所以最好使用完全不同的东西。Perl 与 Bash 很相似,因为它很讨厌,而且充满了快捷方式和奇怪的语法,但它的语义大多是合理的。Python 不太适合启动进程之类的事情,但对于一般系统编程、干净且易于维护要好得多。Python 拥有适用于所有事物的出色库和模块;Perl 甚至更好。

回答by CMPS

I recently developed a language called BashClasswhich is Object Oriented, has type checkingand allow multi-dimensionalarrays. The language syntax is inspired by different programming languages.

我最近开发了一种名为BashClass的语言,它是面向对象的,具有类型检查并允许多维数组。语言语法的灵感来自不同的编程语言。

Here's an example on how a Listclass is implemented (Full example here):

这是一个关于如何实现List类的示例(此处为完整示例):

class List extends Object {
    var Object[] data = new Object[];
    var int size = 0;
    constructor List(){
        super_constructor();
    }

    function void add(var Object object) {
        data[size] = object;
        size = size + 1;
    }

    function void pop() {
        if(size == 0) {
            exception("Cannot remove element from an empty list");
        }
        size = size - 1;
        data[size] = null;
    }

    function int size() {
        return size;
    }

    function Object get(var int index) {
        if(index < 0 || index >= size) {
            exception("Cannot access element out of bound");
        }
        return data[index];
    }
}

Classes and multi-dimensional arrays in BashClass are converted to Bash 4.4 associative arrays. The language is at its first release and is open source on Github. Feel free to contirbute and suggest features.

BashClass 中的类和多维数组被转换为 Bash 4.4 关联数组。该语言是它的第一个版本,并在Github 上开源。随意贡献和建议功能。

回答by Jaromil

You might want to give ZSh a try, it has a lot of improvements to make your shell script more readable.

您可能想尝试一下 ZSh,它有很多改进,使您的 shell 脚本更具可读性。

http://www.zsh.org

http://www.zsh.org

回答by mweststrate

You might want to take a look into nscript, in which you can write shell scripts using javascript. All the common bash constructions are in there, like exit codes, pipes, stream redirects, argument expansion, globbing, prompt etc.

您可能需要查看nscript,您可以在其中使用 javascript 编写 shell 脚本。所有常见的 bash 结构都在那里,如退出代码、管道、流重定向、参数扩展、通配符、提示等。