Are there any languages that compile to Bash?

2019-03-07 23:23发布

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

(This is subjective, but I find it both confusing and annoying. E.g. $var when reading, but var when 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.)

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?

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

$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

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

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

8条回答
ゆ 、 Hurt°
2楼-- · 2019-03-07 23:39

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

http://www.zsh.org

查看更多
The star\"
3楼-- · 2019-03-07 23:41

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

查看更多
干净又极端
4楼-- · 2019-03-07 23:43

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.

查看更多
5楼-- · 2019-03-07 23:50

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

查看更多
beautiful°
6楼-- · 2019-03-07 23:51

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.

查看更多
祖国的老花朵
7楼-- · 2019-03-07 23:54

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.

查看更多
登录 后发表回答