4 ms·
If you're into these kind of tricks, here's a Bash-script IRC client in under 130 lines! https://github.com/halhen/shic/blob/master/shic https://github.com/halh
by sbjs 8y ago
If you're into these kind of tricks, here's a Bash-script IRC client in under 130 lines! https://github.com/halhen/shic/blob/master/shic https://github.com/halhen/shic/blob/master/shic The kind of things people can do with simple tools is just amazing. Makes me wonder if we're over-engineering things when we write them with hundreds of thousands of lines of code.
- Avshalom 8y agoBash is no more a simple tool than c or perl or lua or whatever-programming-language
- sbjs 8y agoBash is "simpler" in the sense that it doesn't have proper data structures. Recent versions have something like an array, but it's still not quite an array afaik. Bash is ultimately still just a string concatenation language.
- wahern 8y agoIt's trivial to implement an array in pure shell code without extensions. For example: # append ARRAYNAME VALUE append() { eval N=\"\${${1}_N:-0}\" # read [ARRAYNAME]_N N=$(($N + 1)) eval ${1}_${N}=\"\${2}\" # set [ARRAYNAME]_[N] eval ${1}_N=\"\${N}\" # store [ARRAYNAME]_N } It's rather straightforward to implement whatever data structure you want this way.
- dagenix 8y agoWhile I understand your point, looking at that code, the first word into my mind isn't "trivial".
- therein 8y agoMakes me think of this: https://i.imgur.com/1LUysfW.png https://i.imgur.com/1LUysfW.png
- Spivak 8y agoIt actually is once you strip all the bash-isms. Here's what this code is really doing. def append(ARRAYNAME, VALUE): # Store the length of the array as a variable ARRAYNAME_LEN # Store the values of the array as variables ARRAYNAME_0, ARRAYNAME_1,... len = globals.get('{{ ARRAYNAME }}_LEN') || 0 len += 1 # Update the length. globals.set('{{ ARRAYNAME }}_LEN', len) # Add the value. globals.set('{{ ARRAYNAME }}_{{ len }}', VALUE) return Nobody said that bash has pretty syntax but this kind of thing wont even phase you once you're used to all the escaping and bracket substitution rules.
- LukeShu 8y agoBash has had real arrays since 2.0 (1996). Though, the `+=` operator to append to an array wasn't added until 3.1 (2005); before that you had to do `array[${#array[*]}]=word` to append. Since 4.0 (2009) it's also had associative arrays.
- deleted 8y ago[deleted]