I am truly convinced that people who genuinely look at me dead in the eyes and tell me "I like bash/zsh/*ksh/*csh" are at the very list suffering from Stockholm syndrome. Bash is rivaled in how ugly it is only by Batch. It's incredible the length people go through to shit on Perl while still using Bash. I just saw someone's script do this (I had to research the syntax and nothing in the man page helped me):
# programs defined way above
for i in ${!programs[@]}; do
# something to do with i
done
let me just:
# Perl
my %programs = (
"key1" => "value1",
"key2" => "value2",
"key3" => "value3"
);
foreach my $key (keys %programs) {
print "Key: $key, Value: $programs{$key}\n";
}
# PowerShell
$programs = @{
"key1" = "value1"
"key2" = "value2"
"key3" = "value3"
}
foreach ($key in $programs.Keys) {
Write-Host "Key: $key, Value: $($programs[$key])"
}
# Tcl
set programs {
key1 value1
key2 value2
key3 value3
}
foreach key [array names programs] {
puts "Key: $key, Value: $programs($key)"
}
I'll die on this hill: PowerShell >>>> Bash. I mean, anything is better than Bash. Don't get me started on how the *sh poor people have to pipe into bc to do basic floating point math or printf or awk or... at this point, seriously go and use Awk as a scripting language (yes, Awk *scripts* exist too and they're pretty similar to your average scripting language).
@a13cui I hadn’t seen that array !syntax before.. That’s such an uh feature.
It reminds me of variable variables in php
@terminalgoblin me neither (and neither has explainshell.com, it wasn't helpful at all). I did *a lot* of digging at dictionaries. Essentially:
declare -A dict
dict[1]=X
dict[2]=Y
for key in "${!dict[@]}"; do
echo "$key ${dict[$key]}"
done
@a13cui ooh thanks for the example! I can now make my shell scripts a billion times more cryptic :D
I also kinda hate how it prints 2 Y
first,, like really?
@KaitlynEthylia it isn't quick to write, even being a jack of all trades would be better than the state Bash is in right now. I rarely meet problems that I genuinely can't solve using Perl or Tcl when it comes to scripting. Maybe the rare instance I have to use Python or Ruby. Bash used to be convenient to me too (I was actually restricting myself as much as possible to /bin/sh so it can be cross-platform), but... it's only for interactive use for me right now.
@KaitlynEthylia "If the only tool you have is a hammer, you tend to see every problem as a nail."
@KaitlynEthylia yes, but it used to be the only tool I had for relatively cross-platform scripting (Python doesn't count for me, I have a looong story with it). I used to hate Perl because I thought it was "ugly", "weird" and "why would anyone use it"... until I had a long thinking session as to why so many scripts are written in Perl and why Linux at least kinda depends on it (for example, on my openSUSE Tumbleweed I have important packages such as g/vim, w3m, wine, auto{make,conf}, git, groff, grub2, mc, yast2, even the default kernel package has a dependency on it)). I was thinking to myself: "oh lord, people sure are using Perl for a lot of things, let's check it out and see what the deal is" and since then it has replaced all of my scripts (while being more cross-platform than Bash since it can run under Windows too). Even for grepping/replacing I have perl -p -i -e at my disposal. I truly cannot justify Bash as anything other than a shell (even /bin/sh can do that job for me for all I care).
@KaitlynEthylia two tips:
- never rely on the system Perl, use plenv instead (so you can switch between versions easily)
- move to 5.38 (the latest version) and begin all your scripts with:
use v5.38;
you get `use strict`, `use warnings` and `signatures` by default, so you can safely write code like:
use v5.38;
sub animals ($cat, $dog, $lizard) {
say "The cat is $cat";
say "The dog is $dog";
say "The lizard is $lizard";
}
animals('Buster', 'Nikki', 'Godzilla');
(if you see `say` and `print` in the wild, `say` automatically adds a newline so you can think of it like a println, otherwise they're the same. you get that for v5.38 too.)
and also:
experimental OOP support! yeah, Perl has classes now. it's actually really exciting stuff
@KaitlynEthylia wait until you find out that COBOL is still getting updates as recently as this year and GnuCOBOL got a big release after 2+ years of hard work. OOP is of course optional, but it's nice to have.
You can also give Tcl/Tk a try, you'll find it absurdly easy to create GUIs and scripts. You also have the Expect utility which is essentially a testing framework but for anything: expect output, send input, interact with the program etc. Expect is what you use if you have to test a console program and you don't have control over its source code and it's heavily used by GNU and especially GCC in the form of DejaGnu. I guess you could use the Expect module from CPAN to get a feel for how to do it in Perl (which seems to be pretty similar to the OG Expect). Oh, SQLite began as a Tcl extension (the blue feather in SQLite's logo is *because* of Tcl) and still has it as a first-class scripting lang. Pretty cool for an one-man job made in '88, right?
@a13cui
Just because something else does it and you prefer that doesn't mean it's not also valid.
Shell scripts are great for simple macros, a step above alias. And you don't have to learn a whole other language to use them.
@shiri It feels very, VERY weird to call Bash scripts "macros", about on the same level as calling Python scripts for Xonsh "macros". No, they're scripts.
@a13cui a macro really is just a type of script, that's why disabling macros in documents you download is a safety mechanism
Shell scripts work well because it's just throwing the commands you would have typed and expect to repeat dozens of times into a re-usable file. Occasionally maybe throw some basic logic in them to vary certain use cases.
But then some people follow the "if all you have is a hammer..." and abuse the hell out of that basic logic to write full programs.
I'm actually far more comfortable with python than I am with bash... I still use bash for some things.
@shiri if you put it *that* way, then yes, you're completely right, it makes total sense. in my mind macros were more of a text processing thing with stuff like m4, the C preprocessor and vim/emacs macros. To be specific, I think of macros as sequences of events (such as keystrokes, mouse clicks, and delays) that can be played back to help with repetitive tasks. Our disagreement stemmed from whether writing commands counts as writing keystrokes or as a standalone thing.
You *can* write "serious" things in Bash, I saw entire interpreters written in it, but at that point it's torture. Each to their own.
@shiri at that point you're not just using a hammer, you're using a heavy rock
(is a rock more versatile than a hammer? who knows)