this post was submitted on 15 Jul 2023
27 points (100.0% liked)

Shell Scripting

1367 readers
1 users here now

From Ash, Bash and Csh to Xonsh, Ysh and Zsh; all shell languages are welcome here!

Rules:
  1. Follow Lemmy rules!
  2. Posts must relate to shell scripting. (See bottom of sidebar for more information.)
  3. Only make helpful replies to questions. This is not the place for low effort joke answers.
  4. No discussion about piracy or hacking.
  5. If you find a solution to your problem by other means, please take your time to write down the steps you used to solve your problem in the original post. You can potentially help others having the same problem!
  6. These rules will change as the community grows.

Keep posts about shell scripting! Here are some guidelines to help:


In general, if your submission text is primarily shell code, then it is welcome here!

founded 2 years ago
MODERATORS
 

I'm sure some of you have absolute monstrosities of sigils (I know I do, in my .zshrc alone). Post them without context, and try and guess what other users's lines are. If you want to provide context or guess, use the markdown editor to spoiler-tag your guesses and explanations!

you are viewing a single comment's thread
view the rest of the comments
[โ€“] varsock 1 points 1 year ago (1 children)

[ "${(Oa@)argv[1,-2]}" "${argv[-1]//(#m)[[]]/${(#)$((6 ^ #MATCH))}}" # this one is definitely not useful

spoiler

The given shell script appears to be written in the Zsh shell syntax. Let's break it down step by step:

  1. [ "${(Oa@)argv[1,-2]}" "${argv[-1]//(#m)[\[\]]/${(#)$((6 ^ #MATCH))}}" ]: This line encloses the entire script within square brackets [ ]. In Zsh, square brackets are commonly used for conditional expressions.

  2. "${(Oa@)argv[1,-2]}": This part refers to an expansion of the argv array, which typically holds command-line arguments passed to the script. Here's what the individual components mean:

    • ${(Oa@)}: This is a parameter expansion flag in Zsh that sorts the elements of the array in ascending order and expands each element as separate words. The @ symbol is used to indicate the array variable argv.
    • argv[1,-2]: This is array slicing syntax that extracts a sub-array of elements from index 1 to the second-to-last element (-2). It excludes the last element, which is assumed to be the final argument.

    In summary, this part expands and sorts the elements of the argv array, excluding the last argument.

  3. "${argv[-1]//(#m)[\[\]]/${(#)$((6 ^ #MATCH))}}": This part refers to another expansion of the argv array, specifically targeting the last element (argv[-1]). Here's what the individual components mean:

    • "${argv[-1]//pattern/replacement}": This is a parameter expansion that performs pattern substitution within the last element of the argv array.
    • (#m): This is an extended globbing flag in Zsh that enables multiline mode for pattern matching. It allows patterns to match across multiple lines.
    • [\[\]]: This is the pattern to match. It matches any occurrence of square brackets ([ or ]).
    • /${(#)$((6 ^ #MATCH))}}: This is the replacement part of the substitution. It calculates the bitwise XOR (^) of 6 and the matched pattern (#MATCH), and uses (()) to perform arithmetic expansion. The (#) flag is used to indicate that the result should be expanded.

    In summary, this part performs a substitution on the last element of the argv array, replacing any occurrence of square brackets with the result of a bitwise XOR operation between 6 and the matched pattern.

Overall, the script appears to process command-line arguments, sort and manipulate them, and then enclose the result in a conditional expression for further evaluation or use. The exact purpose or context of the script can only be determined by understanding its broader context and usage.

[โ€“] gamma 1 points 1 year ago

Corrections

ascending order

O is descending order. o is ascending order. In particular (Oa) keeps the array order, but flips it.

The @ symbol is used to indicate the array vriable argv.

Not here. As a PE flag, @ puts array elements in separate words, even if the parameter is enclosed in quotes.

The #MATCH gives the codepoint of the (first) character in $MATCH. The (#) flag turns codepoints into characters.

Full contextThis is in a joke function called ], which is like [ but you have to specify the elements in reverse order and end it with a [. This uses bit-fiddling to swap [ and ] in the last parameter because I'm a masochist.