Re: zsh Re: type checking/signalling shell and utilities?

2021-11-19 Thread Andreas Kusalananda Kähäri
On Fri, Nov 19, 2021 at 03:10:42PM +0100, Kusalananda Kähäri wrote:
> On Fri, Nov 19, 2021 at 08:58:00PM +1100, Reuben ua Bríġ wrote:
[cut]
> > f=* sed -f$f
> 
> In sh (inserts -f as a separate argument before each name that * expands
> to):
> 
>   set -- *
>   for name do
>   set -- "$@" -f "$name"
>   shift
>   done
> 
>   sed "$@"
[cut]

Or if you feel rebellious,

cat * | sed -f /dev/stdin

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



Re: zsh Re: type checking/signalling shell and utilities?

2021-11-19 Thread Andreas Kusalananda Kähäri
On Fri, Nov 19, 2021 at 08:58:00PM +1100, Reuben ua Bríġ wrote:
> > Date: Fri, 19 Nov 2021 10:23:51 +0100
> > From: Andreas Kusalananda Kähäri 
> > 
> > What was the thing about "/" and "ti"?
> 
> I might a lot of typos. by "ti" I meant "2". I had the glob "2/*" which
> includes a "/". My point was this does not generally preclude special
> interpretation (someone suggested including a "/" for globs with "rm").

Someone probably suggested using ./ as a prefix to any glob that
otherwise may result in words possibly starting with a dash, such as *
or *.c would possibly do.  Using ./* or ./*.c or similar in a loop would
ensure that each generated word is starting with ./ rather than possibly
a dash.

for name in ./*; do
# Will not mistake "$name" as a set of options.
# Because "$name" always starts with ./
utility "$name"
done

or, using -- instead:

for name in *; do
# Will not mistake "$name" as a set of options.
# Because -- disables option parsing.
utility -- "$name"
done

> 
> 
> > Are you talking about zsh here? Or do you know something about ksh?
> > I know -a{1,2,3} -> -a1 -a2 -a3 but nothing like -a{*} -> -a*1 -a*2
> > ...  
> 
> > I'm not sure I understand what you want -a{*} to do.  Are "1", "2",
> > etc. files?  If so, *(P[-a]) in zsh, as already mentioned.
> 
> The point is ksh expands "-a{1,2,3}" to "-a1 a2 a3". I had hoped it

The ksh shell would expand -a{1,2,3} to the three strings -a1, -a2, and
-a3.

> might expand "-a{*}" to "-a1 -a2 -a3" in a directory containing the
> files "a", "b", "c".

That makes no sense.  You could get -a a -a b -a c from *(P[-a]) in zsh.

> > The -c option to cc does not take arguments.  If you just want to list
> > all C files on the command line:
> 
> This was a rather bad toy example. On OpenBSD I overuse a default
> make(1) file so I have forgotten how to actually call cc(1).
> Maybe this will help you see the point I was making:
> 
>   f=* sed -f$f

In sh (inserts -f as a separate argument before each name that * expands
to):

set -- *
for name do
set -- "$@" -f "$name"
shift
done

sed "$@"

In OpenBSD ksh (same result as above):

unset -v args
for name in *; do
set -A args -- "${args[@]}" -f "$name"
done

sed "${args[@]}"

In bash (prepends the string -f directly to each name):

names=(*); sed "${names[@]/#/-f}"

In zsh (as sh and ksh):

sed *(P[-f])

> 
> I think sed(1) might actually be what first gave me the idea. I had a
> bunch of scripts and a bunch of inputs, and some expressions, and I
> thought, "wouldnt it be neat if I could do it this way..."
> 
> > The es shell is available for OpenBSD.  It's not quite rc, but may be
> > close enough.
> 
> Strangely enough I have an "rc" package installd, and a ports/plan9/rc
> directory. I havent upgraded and also have a "plan9port" package
> installd, so maybe that has something to do with that. I also have
> butchered my install somewhat, so maybe it is that.

If you have plan9port installed, just prefix commands by 9 to get the
Plan9 variant, e.g. use "9 rc" to start the rc shell, or use "9 sed" to
use Plan9 sed.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



Re: zsh Re: type checking/signalling shell and utilities?

2021-11-19 Thread Andreas Kusalananda Kähäri
On Fri, Nov 19, 2021 at 07:34:58PM +1059, Reuben ua Bríġ wrote:
> > Date: Fri, 19 Nov 2021 08:30:44 +0100
> > From: Andreas Kusalananda Kähäri 
> > 
> > Ah, so you are talking about options that takes multiple
> > option-arguments.
> 
> That roughly correct for what I was saying about lists, but technically
> the list would be a single argument. It is exactly correct for what I
> meant about signalling globs, that is that a parameter (in the form
> of an array of bits) should be past signalling which arguments are
> expanded from a pattern and therefore definitely filenames.
> 
> > That's not how options usually work on Unix, at least not with
> > utilities that uses standard command line parsing.
> 
> I was thinking of a glob as a sort of pseudo-single-argument.
> find(1) does have for example
> 
>   find ! ( -cnewer file1 -cnewer file2 )
> 
> which is very analogous if not exactly that.

The predicates of find defines a tiny mini-language.  The -cnewer string
(as well as -type, -name, -exec, -ls, etc.) are not options.  They make
up the find expression, a set of tests to be carried out on each found
pathname.  The find utility has options, but these are given before
the search path(s) on the command line (options always comes before
non-options).


[cut] 
> > > to work without first checking the names in '.', and even 'ti' if
> > > compile takes arguments with '/' (if you cant think of any UNIX
> > > utilities that do that, shame on you).  
> > 
> > I'm not seeing you giving an example.
> 
>   sed /home/d

The sed utility has two basic ways of being used, just like e.g. the
grep utility (which the OpenBSD manual fails to make clear IMHO; you
can't mix them).  The first passes the sed editing expression as the
first non-option argument.  The second gives the sed editing expression
as one or a series of -e options with arguments.  In each case, quoting
the editing expression(s) is geneally a good idea, unless you want
the shell to perform field splitting and filename generation on the
argument(s).

sed 'expression'
sed -e 'expression' -e 'expression' -e 'expression'

grep 'expression'
grep -e 'expression' -e 'expression' -e 'expression'

What was the thing about "/" and "ti"?  Using / unquoted on the command
line is not an issue, neither is using the string "ti".  The reason
you want to quote the expressions used with sed and grep is that these
usually contain regular expressions and/or spaces.  The shell would
splitt the expression on the spaces if they are unquoted, to generate
multiple arguments.  The regular expressions may be intpereted as
globbing patterns.

> Are you talking about zsh here? Or do you know something about ksh?
> I know -a{1,2,3} -> -a1 -a2 -a3 but nothing like -a{*} -> -a*1 -a*2 ...

I'm not sure I understand what you want -a{*} to do.  Are "1", "2", etc.
files?  If so, *(P[-a]) in zsh, as already mentioned.

> 
> > It's already been done by the people developing the zsh shell.
> > 
> > compile dir1/*(P[-type1]) dir2/*(P[-type2])
> > 
> > This would append "-type1" before each name that dir1/* expands to,
> > and similarly for the second globbing pattern.  I'm assuming this is
> > what you were looking for.
> > 
> > If you want to match only regular files, include hidden names, and
> > remove the pattern if there are no matches,
> > 
> > compile dir1/*(.DNP[-type1]) dir2/*(.DNP[-type2])
> 
> Oh thats great to know. Thank you for that. I already knew you can do
> 
>   c=* cc -c$c

The -c option to cc does not take arguments.  If you just want to list
all C files on the command line:

cc -c -- *.c

> in rc(1),

The es shell is available for OpenBSD.  It's not quite rc, but may be
close enough.

>but the zsh(1) example you provided here has the nice
> advantage that the flag and its argument are field separated and the
> glob disappears if unmatched. I think this is definitely the best that
> can be done in the shell alone, without modifying utilities to read
> extra parameters, but I am certainly not sold on the sugar (syntax),
> but I will take a look at it.
> 
> Cheers for that last one.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.