this post was submitted on 21 May 2024
201 points (96.3% liked)

Programming

17558 readers
518 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

New favorite tool 😍

top 50 comments
sorted by: hot top controversial new old
[–] sudo42 114 points 6 months ago (2 children)

One day, someone's going to have to debug machine-generated Bash.

[–] [email protected] 60 points 6 months ago

You can do that today. Just ask Chat-GPT to write you a bash script for something non-obvious, and then debug what it gives you.

[–] seaQueue 10 points 6 months ago

For maximum efficiency we'd better delegate that task to an intern or newly hired jr dev

[–] [email protected] 47 points 6 months ago (3 children)

Looking at the example

Why does the generated bash look like that? Is this more safe somehow than a more straighforward bash if or does it just generate needlessly complicated bash?

[–] dyc3 35 points 6 months ago

Yeah that shit is completely unreadable

[–] sxt 29 points 6 months ago* (last edited 6 months ago)

I doubt the goal is to produce easily understood bash, otherwise you'd just write bash to begin with. It's probably more similar to a typescript transpiler that takes in a language with different goals and outputs something the interpreter can execute quickly (no comment on how optimized this thing is).

[–] [email protected] 13 points 6 months ago

So many forks for something that can be solved entirely with bash inbuilts

[–] [email protected] 39 points 6 months ago* (last edited 6 months ago)

The language idea is good, but: THREE.WebGLRenderer: A WebGL context could not be created. Reason: WebGL is currently disabled.

Seriously? Why do I need WebGL to read TEXT in docs? :/

[–] [email protected] 32 points 6 months ago* (last edited 6 months ago) (1 children)

As someone who has done way too much shell scripting, the example on their website just looks bad if i'm being honest.

I wrote a simple test script that compares the example output from this script to how i would write the same if statement but with pure bash.

here's the script:

#!/bin/bash

age=3

[ "$(printf "%s < 18\n" "$age" | bc -l | sed '/\./ s/\.\{0,1\} 0\{1,\}$//')" != 0  ] && echo hi

# (( "$age" < 18 )) && echo hi

Comment out the line you dont want to test then run hyperfine ./script

I found that using the amber version takes ~2ms per run while my version takes 800microseconds, meaning the amber version is about twice as slow.

The reason the amber version is so slow is because: a) it uses 4 subshells, (3 for the pipes, and 1 for the $() syntax) b) it uses external programs (bc, sed) as opposed to using builtins (such as the (( )), [[ ]], or [ ] builtins)

I decided to download amber and try out some programs myself.

I wrote this simple amber program

let x = [1, 2, 3, 4]
echo x[0]

it compiled to:

__AMBER_ARRAY_0=(1 2 3 4);
__0_x=("${__AMBER_ARRAY_0[@]}");
echo "${__0_x[0]}"

and i actually facepalmed because instead of directly accessing the first item, it first creates a new array then accesses the first item in that array, maybe there's a reason for this, but i don't know what that reason would be.

I decided to modify this script a little into:

__AMBER_ARRAY_0=($(seq 1 1000));
__0_x=("${__AMBER_ARRAY_0[@]}");
echo "${__0_x[0]}"

so now we have 1000 items in our array, I bench marked this, and a version where it doesn't create a new array. not creating a new array is 600ms faster (1.7ms for the amber version, 1.1ms for my version).

I wrote another simple amber program that sums the items in a list

let items = [1, 2, 3, 10]
let x = 0
loop i in items {
    x += i
}

echo x

which compiles to

__AMBER_ARRAY_0=(1 2 3 10);
__0_items=("${__AMBER_ARRAY_0[@]}");
__1_x=0;
for i in "${__0_items[@]}"
do
    __1_x=$(echo ${__1_x} '+' ${i} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')
done;
echo ${__1_x}

This compiled version takes about 5.7ms to run, so i wrote my version

arr=(1 2 3 10)
x=0
for i in "${arr[@]}"; do
    x=$((x+${arr[i]}))
done
printf "%s\n" "$x"

This version takes about 900 microseconds to run, making the amber version about 5.7x slower.

Amber does support 1 thing that bash doesn't though (which is probably the cause for making all these slow versions of stuff), it supports float arithmetic, which is pretty cool. However if I'm being honest I rarely use float arithmetic in bash, and when i do i just call out to bc which is good enough. (and which is what amber does, but also for integers)

I dont get the point of this language, in my opinion there are only a couple of reasons that bash should be chosen for something a) if you're just gonna hack some short script together quickly. or b) something that uses lots of external programs, such as a build or install script.

for the latter case, amber might be useful, but it will make your install/build script hard to read and slower.

Lastly, I don't think amber will make anything easier until they have a standard library of functions.

The power of bash comes from the fact that it's easy to pipe text from one text manipulation tool to another, the difficulty comes from learning how each of those individual tools works, and how to chain them together effectively. Until amber has a good standard library, with good data/text manipulation tools, amber doesn't solve that.

[–] [email protected] 13 points 6 months ago

This is the complete review write up I love to see, let's not get into the buzzword bingo and just give me real world examples and comparisons. Thanks for doing the real work πŸ™‚

[–] [email protected] 26 points 6 months ago (2 children)

Compiling to bash seems awesome, but on the other hand I don't think anyone other than the person who wrote it in amber will run a bash file that looks like machine-generated gibberish on their machine.

[–] [email protected] 23 points 6 months ago

I disagree. People run Bash scripts they haven't read all the time.

Hell some installers are technically Bash scripts with a zip embedded in them.

[–] [email protected] 14 points 6 months ago

Compiling to bash seems awesome

See, i disagree because

I don't think anyone other than the person who wrote it in amber will run a bash file that looks like machine-generated gibberish on their machine.

Lol I barely want to run (or read) human generated bash, machine generated bash sounds like a new fresh hell that I don't wanna touch with a ten foot pole.

[–] Thcdenton 18 points 6 months ago (2 children)

There's a joke here but I'm not clever enough to make it.

[–] [email protected] 13 points 6 months ago

Pretty cool bug. Looks like a surreal meme

[–] eager_eagle 5 points 6 months ago (1 children)

what browser are you using? It renders just fine on mobile and desktop to me

[–] Thcdenton 4 points 6 months ago

Whatever boost defaults to on a note9

[–] [email protected] 15 points 6 months ago

I'm very suspicious of the uses cases for this. If the compiled bash code is unreadable then what's the point of compiling to bash instead of machine code like normal? It might be nice if you're using it as your daily shell but if someone sent me "compiled" bash code I wouldn't touch it. My general philosophy is if your bash script gets too long, move it to python.

The only example I can think of is for generating massive install.sh

[–] [email protected] 12 points 6 months ago (1 children)

I like the idea in principle. For it to be worth using though, it needs to output readable Bash.

[–] Evotech 2 points 6 months ago (1 children)
[–] [email protected] 7 points 6 months ago

Because you still need to be able to understand what’s actually getting executed. There’s no debugger so you’ll still be debugging Bash.

[–] [email protected] 8 points 6 months ago (1 children)
[–] [email protected] 3 points 6 months ago (1 children)

I’m trying but I’m shooting my own foot all the time 😒

load more comments (1 replies)
[–] eager_eagle 7 points 6 months ago (1 children)

with no support for associative arrays (dicts / hashmaps) or custom data structs this looks very limited to me

[–] [email protected] 3 points 6 months ago (2 children)

Does Bash support those? I think the idea is that it's basically Bash, as if written by a sane person. So it supports the same features as Bash but without the army of footguns.

[–] [email protected] 5 points 6 months ago (3 children)

A language being compiled should be able to support higher-level language concepts than what the target supports natively. That's how compiling works in the first place.

load more comments (3 replies)
[–] eager_eagle 3 points 6 months ago* (last edited 6 months ago)

it does, well at least associative arrays

[–] ReluctantMuskrat 6 points 6 months ago (4 children)

As a long-time bash, awk and sed scripter who knows he'll probably get downvoted into oblivion for this my recommendation: learn PowerShell

It's open-source and completely cross-platform - I use it on Macs, Linux and Windows machines - and you don't know what you're missing until you try a fully objected-oriented scripting language and shell. No more parsing text, built-in support for scalars, arrays, hash maps/associative arrays, and more complex types like version numbers, IP addresses, synchronized dictionaries and basically anything available in .Net. Read and write csv, json and xml natively and simply. Built-in support for regular expressions throughout, web service calls, remote script execution, and parallel and asynchronous jobs and lots and lots of libraries for all kinds of things.

Seriously, I know its popular and often-deserved to hate on Microsoft but PowerShell is a kick-ass, cross-platform, open-source, modern shell done right, even if it does have a dumb name imo. Once you start learning it you won't want to go back to any other.

[–] [email protected] 7 points 6 months ago (1 children)

I appreciate you sharing your perspective. Mine runs counter to it.
The more PowerShell I learn, the more I dislike it.

[–] jelloeater85 3 points 6 months ago

As someone who spent 2 years learning and writing PowerShell for work... It's... Okay. Way easier to make stuff work then bash, and gets really powerful when you make libraries for it. But... I prefer Python and GoLang for building scripts and small apps.

load more comments (3 replies)
[–] [email protected] 5 points 6 months ago (2 children)

I'm a mathematician with very limited programming experience. Can someone explain the significance of this?

[–] [email protected] 14 points 6 months ago (1 children)

Bash is one of the most used shell language, it's installed on almost all Linux and Mac systems and can also be used on windows. Almost no one likes writing it as it is convoluted and really really hard to read and write. There are many replacement language's for it, but using them is troublesome, because of incompatibilities. Amber is compiled which will solve problems with compatibility and it seems that language itself is very readable. On top of that it has most futures that modern programmers need.

[–] [email protected] 3 points 6 months ago

Thank you, I think I understand now. πŸ™‚

[–] [email protected] 11 points 6 months ago

Basically dealing with abandoned-by-god syntax and limitations of bash. You can abstract them away!

[–] [email protected] 5 points 6 months ago (1 children)

Here's a language that does bash and Windows batch files: https://github.com/batsh-dev-team/Batsh

I haven't used either tool, so I can't recommend one over the other.

[–] felbane 5 points 6 months ago* (last edited 6 months ago)

If their official website isn't https://batsh.it I'm going to be very sad.

Edit: ☹️

[–] [email protected] 4 points 6 months ago

Why not write.. Bash?

[–] lawrence 4 points 6 months ago

This is glorious.

[–] [email protected] 3 points 6 months ago

Gotta try, the website seems amazing

[–] [email protected] 3 points 6 months ago

Late to the party. Idris had a bash backend (i.e. you could compile Idris to bash), and it's already bit rotted with new Idris versions.

I hope the language is at least as cool as Idris.

[–] [email protected] 3 points 6 months ago

Cool to see that after Cotowali was sadly abandoned due to lack of funding. Please, fund the FOSS projects you use!

[–] [email protected] 3 points 6 months ago

when people have too much free time

[–] [email protected] 3 points 6 months ago (1 children)

How is it using something like this vs just a bash alternative. Can you use this in the shell or only as a compiled language?

[–] [email protected] 3 points 6 months ago (2 children)

If you can use an alternative then do that. This is for situations where you can't use an alternative or don't want users to have to install anything else.

load more comments (2 replies)
load more comments
view more: next β€Ί