有什么语言可以编译成 Bash 吗?

我既喜欢又讨厌写巴斯。我喜欢 所以在操作文件和处理进程方面的简化(我同意 这个流行的问题在这方面比 Python、 Ruby 等要好得多) ,但是我喜欢 仇恨的语法,特别是在条件句、循环等方面。

(这是主观的,但我觉得它既令人困惑又令人厌烦。例如,读取时使用 $var,写入时使用 var; 如果在 =周围有空格,那么写入无声地失败; 使用 regexp 时使用 ifs 中的双括号; 有时使用双分号,有时使用单分号,等等

作为 CoffeeScript 的忠实粉丝,它可以编译成 JS,我一直想知道: 有没有哪种语言具有 Python/Ruby/CoffeeScript 这样的语言的美学/语法,但是它是以 Bash 而不是其他运行时的形式编译和运行的?

例如,我希望能够用更简单的语法编写大部分 -Bash:

$AGGREGATE_FILENAME = 'allfiles.txt'


if not exists $AGGREGATE_FILENAME
touch $AGGREGATE_FILENAME


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


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

这是一个超级人为设计的示例,语法是一个草稿人(大部分灵感来自 CoffeeScript,但保留了一级命令的基本 Bash 概念,与变量分离,以及松散的类型)。

不管怎样,只是一个问题和思考的食物。我希望 能够比 Bash 更好地编写我的脚本。=)谢谢!

10971 次浏览

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.

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.

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

http://www.zsh.org

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.

Plumbum is implemented in Python and looks pretty solid:

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

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

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!

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

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.

Bish is another option:

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).

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

Differences powscript vs the tools above

  • 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

I recently developed a language called BashClass which is Object Oriented, has type checking and allow multi-dimensional arrays. The language syntax is inspired by different programming languages.

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

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.