I review shell scripts from beginner ops people. I would not approve any of this stuff. Once you need this complexity in shell, you need other things you should be getting from the language's stdlib. So I'd ask them to switch to Python or Go.
Do not fall into the trap of big complex shell scripts.
I was about to comment with my usual 'why not PowerShell', but it seems the author acknowledges this anyway at the end:
> I’ll quote Rich’s sh (POSIX shell) tricks to end this:
> I am a strong believer that Bourne-derived languages are extremely bad, on the same order of badness as Perl, for programming, and consider programming sh for any purpose other than as a super-portable, lowest-common-denominator platform for build or bootstrap scripts and the like, as an extremely misguided endeavor
let’s implement split-by-double-dash, a function (or a program) that would return two lists: args that come before -- and ones that come after.
split-by-double-dash a b c -- d e f should return the lists [a, b, c] and [d, e, f]
FWIW in YSH (https://oils.pub/ysh.html), you can do this in a style that's like Python and JavaScript, but you can also combine it with shell idioms.
First create it and pretty print it:
ysh-0.34$ var li = :| a b c -- d e f | # shell word style, ['a', 'b'] style is also accepted
ysh-0.34$ = li # pretty print with =
(List) ['a', 'b', 'c', '--', 'd', 'e', 'f']
Then test out the indexOf() method on strings:
ysh-0.34$ = li.indexOf('--')
(Int) 3
Then write the function:
ysh-0.34$ func splitBy(li) {
> var i = li.indexOf('--')
> assert [i !== -1]
> return ( [li[ : i], li[i+1 : ]] ) # same slicing as Python
> }
Call it and unpack it
ysh-0.34$ var front, back = splitBy(li)
ysh-0.34$ = front
(List) ['a', 'b', 'c']
5 comments
[ 2.9 ms ] story [ 31.1 ms ] threadDo not fall into the trap of big complex shell scripts.
> I’ll quote Rich’s sh (POSIX shell) tricks to end this:
> I am a strong believer that Bourne-derived languages are extremely bad, on the same order of badness as Perl, for programming, and consider programming sh for any purpose other than as a super-portable, lowest-common-denominator platform for build or bootstrap scripts and the like, as an extremely misguided endeavor
split-by-double-dash a b c -- d e f should return the lists [a, b, c] and [d, e, f]
FWIW in YSH (https://oils.pub/ysh.html), you can do this in a style that's like Python and JavaScript, but you can also combine it with shell idioms.
First create it and pretty print it:
Then test out the indexOf() method on strings: Then write the function: Call it and unpack it Use it in shell argv, with @myarray as splicing: