zsh-users
 help / color / mirror / code / Atom feed
From: Jim <linux.tech.guy@gmail.com>
To: zsh <zsh-users@zsh.org>
Subject: Mix and match parameter expansion flags and sub-scripting flags + quoting
Date: Fri, 29 Dec 2017 09:29:12 -0600	[thread overview]
Message-ID: <CA+rB6GLSgUkWiBRMmNQ_gc-A2pABpPJVnR8xf9tpYaK3e8JMyQ@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 2570 bytes --]

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 #1.2: Type: text/html, Size: 6650 bytes --]

[-- Attachment #2: testscript --]
[-- Type: application/octet-stream, Size: 3726 bytes --]


#TestFile=${${TestFile:-$1}:-${HOME}/test/files/pw}
TestFile=${${TestFile:-$1}:-/etc/passwd}

  print "Group 1 with outer quotes"
  shell="${${${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[(ws/:/)7]}" ; print ${shell}
  shell="${${${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[(ws/:/)-1]}" ; print ${shell}
  print "Group 1 w/o outer quotes"
  shell=${${${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[(ws/:/)7]} ; print ${shell}
  shell=${${${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[(ws/:/)-1]} ; print ${shell}
  print "Group 2 with outer quotes"
  shell="${${${(f)"$(<${TestFile})"}[(r)${USER}:*]}[(ws/:/)7]}" ; print ${shell}
  shell="${${${(f)"$(<${TestFile})"}[(r)${USER}:*]}[(ws/:/)-1]}" ; print ${shell}
  print "Group 2 w/o outer quotes"
  shell=${${${(f)"$(<${TestFile})"}[(r)${USER}:*]}[(ws/:/)7]} ; print ${shell}
  shell=${${${(f)"$(<${TestFile})"}[(r)${USER}:*]}[(ws/:/)-1]} ; print ${shell}
# ------------------------------------------------------------------------------
  print "Group 3 with outer quotes"
  shell="${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}}[7]}" ; print ${shell}
  shell="${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}}[-1]}" ; print ${shell}
  print "Group 3 w/o outer quotes"
  shell=${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}}[7]} ; print ${shell}
  shell=${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}}[-1]} ; print ${shell}
  print "Group 4 with outer quotes"
  shell="${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#${USER}:*}}[7]}" ; print ${shell}
  shell="${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#${USER}:*}}[-1]}" ; print ${shell}
  print "Group 4 w/o outer quotes"
  shell=${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#${USER}:*}}[7]} ; print ${shell}
  shell=${${(s/:/)${(M)${(f)"$(<${TestFile})"}:#${USER}:*}}[-1]} ; print ${shell}
# ------------------------------------------------------------------------------
  print "Group 5 with outer quotes"
  shell="${${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}[(ws/:/)7]}" ; print ${shell}
  shell="${${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}[(ws/:/)-1]}" ; print ${shell}
  print "Group 5 w/o outer quotes"
  shell=${${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}[(ws/:/)7]} ; print ${shell}
  shell=${${(M)${(f)"$(<${TestFile})"}:#*:${UID}:${GID}:*}[(ws/:/)-1]} ; print ${shell}
  print "Group 6 with outer quotes"
  shell="${${(M)${(f)"$(<${TestFile})"}:#${USER}:*}[(ws/:/)7]}" ; print ${shell}
  shell="${${(M)${(f)"$(<${TestFile})"}:#${USER}:*}[(ws/:/)-1]}" ; print ${shell}
  print "Group 6 w/o outer quotes"
  shell=${${(M)${(f)"$(<${TestFile})"}:#${USER}:*}[(ws/:/)7]} ; print ${shell}
  shell=${${(M)${(f)"$(<${TestFile})"}:#${USER}:*}[(ws/:/)-1]} ; print ${shell}
# ------------------------------------------------------------------------------
  print "Group 7 with outer quotes"
  shell="${${(s/:/)${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[7]}" ; print ${shell}
  shell="${${(s/:/)${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[-1]}" ; print ${shell}
  print "Group 7 w/o outer quotes"
  shell=${${(s/:/)${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[7]} ; print ${shell}
  shell=${${(s/:/)${(f)"$(<${TestFile})"}[(r)*:${UID}:${GID}:*]}[-1]} ; print ${shell}
  print "Group 8 with outer quotes"
  shell="${${(s/:/)${(f)"$(<${TestFile})"}[(r)${USER}:*]}[7]}" ; print ${shell}
  shell="${${(s/:/)${(f)"$(<${TestFile})"}[(r)${USER}:*]}[-1]}" ; print ${shell}
  print "Group 8 w/o outer quotes"
  shell=${${(s/:/)${(f)"$(<${TestFile})"}[(r)${USER}:*]}[7]} ; print ${shell}
  shell=${${(s/:/)${(f)"$(<${TestFile})"}[(r)${USER}:*]}[-1]} ; print ${shell}
# ------------------------------------------------------------------------------

  print ""
  unset shell


             reply	other threads:[~2017-12-29 15:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-29 15:29 Jim [this message]
2017-12-29 18:43 ` Bart Schaefer
2017-12-31 19:01   ` Jim
2018-01-01  8:27     ` dana

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+rB6GLSgUkWiBRMmNQ_gc-A2pABpPJVnR8xf9tpYaK3e8JMyQ@mail.gmail.com \
    --to=linux.tech.guy@gmail.com \
    --cc=linuxtechguy@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).