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

Shell Scripting

1349 readers
4 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 1 year 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
[–] [email protected] 1 points 1 year ago (1 children)

curious to see if AI can decipher? Certainly! bonus that we get so see potentially beneficial shell

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

Here's all the ones I was considering. I only posted the first two here. I'll add the explanations later.

ZDOTDIR="${${(%):-%x}:P:h}"                                              # posted 
reply=( ${(M)dirs:#*/$~pattern}(Noe['REPLY=${(l[3][0])#REPLY:t}'][1]) )  # posted
typeset -a $1=("${(@ps[$2])"${2:-"$(<&0)"}"}")
local d=("${(@s[/])${(%):-%~}}")
print -rn $termcap[sc]${termcap[ch]//(\%i|\%p1|\%d)}$termcap[ct]${(j::)s}$termcap[rc]
[ "${(Oa@)argv[1,-2]}" "${argv[-1]//(#m)[\[\]]/${(#)$((6 ^ #MATCH))}}"   # this one is definitely not useful
set -- "${@[0,eoo]:/(#m)^-*/${MATCH:a}}" "${@[eoo+1,-1]:a}"
[–] [email protected] 1 points 1 year ago* (last edited 1 year ago)

I posted interpretations to the remaining ones. I did not have time to massage the prompts or verify the answers. I assume you know what they do and can comment on the accuracy. I am rate limited and had to use a lower tier model, As free up more queries I can re-do some or have it expand on what it's purpose and usefulness might be.

[–] [email protected] 1 points 1 year ago (1 children)

typeset -a $1=("${(@ps[$2])"${2:-"$(<&0)"}"}")

spoilerSo, overall, this script reads input from $2 or stdin if $2 is not provided, splits it into an array based on the delimiter $2, and then assigns the array to the variable named $1. Note that this script only works in Zsh, not in Bash. Zsh has a more advanced parameter and array system than Bash, so this script can't be directly translated into Bash.

  1. typeset -a: This part declares an array variable.
    • -a flag specifies that the variable is an array.
  2. $1=("${(@ps[$2])"${2:-"$(<&0)"}"}"):
    • $1 refers to the first argument passed to the script.
    • "${2:-"$(<&0)"}" is an expansion that evaluates to the second argument passed to the script. If the second argument is not provided, it reads input from standard input (<&0 means read from stdin).
    • "${(@ps[$2])" is an expansion that performs parameter splitting.
    • @ specifies that the expansion should be split into separate array elements.
    • (ps[$2]) is a parameter expansion that performs word splitting on the second argument.
  3. The entire expression "${(@ps[$2])"${2:-"$(<&0)"}"}" is wrapped in parentheses and assigned to the variable specified by $1.

In summary, this script takes two arguments: $1 represents the name of the array variable, and $2 represents the values to be assigned to that array. It then assigns the array $2 to the variable named by $1 after performing parameter and word splitting on the second argument. If the second argument is not provided, it reads input from standard input.

[–] [email protected] 1 points 1 year ago

This one was impressively spot-on.

[–] [email protected] 1 points 1 year ago (1 children)

local d=("${(@s[/])${(%):-%~}}")

spoilerThe given shell script is written in the Zsh shell syntax. It initializes a local array variable d with the expansion of a string. Let's break it down step by step:

  1. local d=: This line declares a local variable d. The local keyword is used to define variables with local scope, meaning they are only accessible within the current scope, such as within a function or a block of code.

  2. "${(%):-%~}": This part of the script performs string expansion and substitution to populate the array d.

    • %~ is a special parameter expansion in Zsh that expands to the current working directory (tilde expansion).

    • (%): is another parameter expansion flag that performs splitting and globbing on the value obtained from %~. It splits the resulting string into separate elements based on the / delimiter and performs globbing (filename expansion) on each element.

      For example, if the current working directory is /path/to/some/directory, then %~ expands to /path/to/some/directory, and (%): splits it into individual components: path, to, some, and directory. If there are any glob patterns present, such as * or ?, they would be expanded as well.

  3. ("${(@s[/])${(%):-%~}}"): This part surrounds the expanded string with parentheses to create an array and stores it in the variable d. The (@s[/]) syntax is an array flag in Zsh that splits the resulting string into separate elements based on the / delimiter. Each element represents a directory component obtained from the %~ expansion.

In summary, this script initializes the local array variable d with the directories present in the current working directory's path. Each directory is stored as a separate element in the array d.

[–] [email protected] 1 points 1 year ago

It may have gotten some details wrong, but the summary is spot-on.

Corrections(%) enables prompt expansion. %~ is a string, which when prompt expanded, is $PWD with tilde expansion.

[–] [email protected] 1 points 1 year ago (1 children)

print -rn $termcap[sc]${termcap[ch]//(%i|%p1|%d)}$termcap[ct]${(j::)s}$termcap[rc]

spoilerThe given shell script appears to be written in Zsh, a popular Unix shell. Let's break down the script step by step:

  1. print -rn: This command is used to print the output without a newline character (-n) and without interpreting any escape sequences (-r).

  2. $termcap[sc]: It refers to a termcap variable sc, which stands for "save cursor position." Termcap is a database and library used to control the behavior of terminals. $termcap[sc] saves the current cursor position.

  3. ${termcap[ch]//(\%i|\%p1|\%d)}: This part performs a substitution on the value of the termcap variable ch. The double slashes (//) indicate a global substitution, and the pattern (\%i|\%p1|\%d) matches any of the three percent sequences: %i, %p1, or %d. It replaces all occurrences of these sequences with an empty string.

  4. $termcap[ct]: It refers to the termcap variable ct, which stands for "clear to end of line." It clears everything from the cursor position to the end of the current line.

  5. ${(j::)s}: This uses parameter expansion to join the array variable s using colons (:) as separators. The (j::) flag indicates that colons should be used as separators.

  6. $termcap[rc]: It refers to the termcap variable rc, which stands for "restore cursor position." It restores the saved cursor position.

In summary, this shell script saves the cursor position, performs a substitution on the value of ch termcap variable, clears the current line from the cursor position to the end, prints the value of array variable s joined by colons, and finally restores the cursor position. The purpose and context of this script can only be determined based on the larger context or the values of the termcap variables sc, ch, ct, s, and rc.

[–] [email protected] 1 points 1 year ago

CorrectionsFirst, a look at man termcap:

       ch   Move cursor horizontally only to column %1
       ct   Clear tabs

(j::) actually joins with no seperator. The character following the j is used to capture the separator. Nearly any character or paired bracket can be used, but colons are common. Other ways to write this which might have been more obvious: (j''), (j[]), (j<>). To actually join with colons, something like (j.:.) would have been used.

Full contextZsh agressively sets tabstops to every 8 characters. This is in a function which I trap to the WINCH signal, to set the termstops to 4 using the $termcap sequences rather than the external command tput:

.on_winch(){
	# tabs
	local -a s
	repeat COLUMNS/$1 s+=(${termcap[RI]/\%p1\%d/$1}$termcap[st])

	# final
	print -rn $termcap[sc]${termcap[ch]//(\%i|\%p1|\%d)}$termcap[ct]${(j::)s}$termcap[rc]
}
.on_winch 4
trap '.on_winch 4' WINCH

[–] [email protected] 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.

[–] [email protected] 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.

[–] [email protected] 1 points 1 year ago (1 children)

set -- "${@[0,eoo]:/(#m)^-*/${MATCH:a}}" "${@[eoo+1,-1]:a}"

spoiler

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

  1. set --: This command sets the positional parameters of the script. In Zsh, the -- option is used to signify the end of options and the beginning of positional parameters.

  2. "${@[0,eoo]:/(#m)^-*/${MATCH:a}}": This part refers to the expansion of the positional parameters, specifically targeting the elements from index 0 to the end of options offset (eoo). Here's what the individual components mean:

    • ${@[0,eoo]}: This refers to a sub-array of positional parameters from index 0 to eoo. It extracts the elements corresponding to command-line options.
    • /(#m)^-*: This is a pattern match within a substitution. It matches any occurrences of hyphens (-) at the beginning of each element. The (#m) flag enables multiline mode for pattern matching.
    • ${MATCH:a}: This is the replacement part of the substitution. It expands the matched pattern (${MATCH}) and applies the :a modifier, which removes leading hyphens from the matched string.

    In summary, this part removes leading hyphens from the command-line options.

  3. "${@[eoo+1,-1]:a}": This part refers to the expansion of the positional parameters, specifically targeting the elements from the end of options offset (eoo + 1) to the last element. Here's what the individual components mean:

    • ${@[eoo+1,-1]}: This refers to a sub-array of positional parameters from the end of options offset (eoo + 1) to the last element. It extracts the arguments that come after the command-line options.
    • :a: This applies the :a modifier, which removes leading hyphens from each element.

    In summary, this part removes leading hyphens from the command-line arguments that come after the options.

Overall, the script modifies the command-line options and arguments by removing leading hyphens. The modified values are then set as the new positional parameters using the set command. The exact purpose or context of the script can only be determined by understanding its broader context and usage.

[–] [email protected] 1 points 1 year ago

CorrectionsIt impressively identified exactly what eoo meant, and handled the array splicing, but it tripped over Zsh's globbing syntax.

/(#m)^-*: This is a pattern match within a substitution. It matches any occurrences of hyphens (-) at the beginning of each element.

  1. The : in ${foo:/pat/repl} requires pat to match the whole element
  2. The ^ actually negates the match, rather than anchors it to the start. ^-* matches any element not beginning with a hyphen.
  3. The (#m) flag captures the whole match in the parameter $MATCH.
  4. ${MATCH:a} transforms relative paths to absolute paths.

Full contextI use this as a wrapper around nvim --remote to make sure any non-options (i.e.: files) I pass have absolute paths rather than relative paths.