Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Mix and match parameter expansion flags and sub-scripting flags + quoting



Hello,

The intent is, using only "native zsh" tools, return the 'shell'
as set in /etc/passwd for my own $USER without using any external
utilities, gnu or otherwise. As in UNIX/Linux, zsh also has more then
one way of doing the same thing. So as a training exercise I decided
to use as many variants as I could to do the same thing. In the process
I found that what I though would work, did not return the intended
result. A number of attempts to fix, ended up producing, if nothing
else, syntax errors.

Some of my early trial and error, was not so successful, so I ended up
testing quite a few variations of quoting, patterns, etc... in an
attempt to determine what was going on. From the examples below you
will see a couple of different patterns and subscript numbers and
quoting around '${(f)$(<TestFile)}'. As a note all three variations of
quoting of the fore mentioned code appear to return the same result.

TestFile=${${TestFile:-$1}:-/etc/passwd} # set default file

The following works with or w/o the "external" quotes:
shell="${${${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[(ws/:/)7]}"
as does:
shell="${${(s/:/)"${(f)"$(<${TestFile})"}"[(r)${USER}:*]}[-1]}"

The following returns the correct result, only if there are no
"external" quotes(as shown):
shell=${${(s/:/)${(M)"${(f)$(<${TestFile})}":#${USER}:*}}[-1]}

Would like to understand what is going on here when "external"
quotes a present.

The following does not produce any errors, but it also doesn't return
the intended output, with or w/o "external" quotes:
shell="${${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}[(ws/:/)-1]}"

I thought this was the proper use of zsh tools, but since it isn't
returning the intended results, it must be incorrect.  Any thoughts
on what I am doing wrong and or assumptions made?

Also, depending on patterns, subscripts, and in one known case, quoting;
the results returned are all over the place. It could be a "blank line",
or the last field of the last line of the passwd file, or the last field
of the first line + the first field of the second line, or the entire
line for $USER.

Note, if your USER entry is the last line of the passwd file, you can
get the correct results, even though the code is wrong. Found this the
hard way.

Any pointers as to how to proceed to understand what is going on would
be appreciated. Thanks in advance.

My distributions currently distributes ZSH_VERSION 5.3.1.

Sincerely,

Jim

P.S. The attached script shows the variations I've tried.
   I don't expect anyone to fix any incorrect scripting,
   that's on me.

Attachment: testscript
Description: Binary data



Messages sorted by: Reverse Date, Date, Thread, Author