Re: Missing documentation "-bash_input"

2023-11-28 Thread Klaus Frank

Hi,

thanks for the explanation. Esp. the parsing one at the bottom, that 
explains why my tests were false positive.


One thing though, I probably should already know that, but why is a $0 
needed even though a command was already specified? Shouldn't the 
command itself be $0?




On 2023-11-29 01:27:17, Lawrence Velázquez wrote:

On Tue, Nov 28, 2023, at 5:33 PM, Klaus Frank wrote:

sorry, but this is not true

It is true.



I can clearly see that it exists. It may be
an distro addition though. Is it specific to ArchLinux? Because I can
see it being used and when I try to use it on my system it also clearly
works.

You see it being used without causing errors, but that doesn't mean
it's doing what you think it's doing.  Observe that

% bash -c 'printf %s\\n "$@"' -bash 1 2 3

and

% bash -c 'printf %s\\n "$@"' --no_such_option 1 2 3

appear to behave identically.



But against it just being a distro specific thing is that I also
can see it within the bash source code mirror on GitHub. Where does it
come from if it is not supposed to exist? Sorry, but something is really
confusing here.


Example usage:
https://gitlab.archlinux.org/archlinux/devtools/-/blob/master/src/makechrootpkg.in?ref_type=heads#L152

arch-nspawn "$copydir" "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
      bash -c 'yes y | pacman -U -- "$@"' -bash "${pkgnames[@]/#//root/}"

You're misinterpreting that command line.  As Andreas already
explained, when you run something like

bash -c 'command string here' a b c d

the first argument after the command string (in my case "a", and
in your case "-bash") is used as $0 while executing that command
string, and the remaining arguments are used as the positional
parameters.  This happens even if an argument looks like an option,
so options are not recognized as such if they follow -c [*]:

% bash -x -c 'printf %s\\n "$0"'
+ printf '%s\n' bash
bash
% bash -c 'printf %s\\n "$0"' -x
-x

In your example, bash executes the command string 'yes ... "$@"'
with "-bash" as $0 and the (possibly multiword) expansion of
"${pkgnames[@]/#//root/}" as the positional parameter(s).  I don't
know why they are setting $0 to "-bash".  Doing so makes the shell
look like a login shell at a quick glance, but that doesn't make
it one:

% bash -c 'printf %s\\n "$0"; shopt login_shell' -bash
-bash
login_shell off

---

[*]: Hypothetical "-bash" and "-bash_input" options would have to
come before -c to take effect.  However, bash does not use Tcl-style
options, so "bash -bash" would be equivalent to "bash -b -a -s -h",
and "bash -bash_input" to "bash -b -a -s -h -_ -i -n -p -u -t".
These options all exist already except for "-_", which is why Greg's
demonstration yielded the error "-_: invalid option".



smime.p7s
Description: S/MIME Cryptographic Signature


Re: Missing documentation "-bash_input"

2023-11-28 Thread Klaus Frank

Hi,

sorry, but this is not true, I can clearly see that it exists. It may be 
an distro addition though. Is it specific to ArchLinux? Because I can 
see it being used and when I try to use it on my system it also clearly 
works. But against it just being a distro specific thing is that I also 
can see it within the bash source code mirror on GitHub. Where does it 
come from if it is not supposed to exist? Sorry, but something is really 
confusing here.



Example usage: 
https://gitlab.archlinux.org/archlinux/devtools/-/blob/master/src/makechrootpkg.in?ref_type=heads#L152


arch-nspawn "$copydir" "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
    bash -c 'yes y | pacman -U -- "$@"' -bash "${pkgnames[@]/#//root/}"


Bash source code on GitHub mirror: 
https://github.com/bminor/bash/blob/master/y.tab.c#L3967


"(--bash_input.location.string) = c;"


Yours sincerely,
Klaus Frank

On 2023-11-28 15:30:20, Andreas Schwab wrote:

On Nov 28 2023, Klaus Frank wrote:


I just noticed that the man pages are missing documentation for the
"-bash" (or better "-bash_input") parameter.

There is no such thing as a -bash or -bash_input parameter.


be better readable when being passed as an argument itself to e.g. nspawn
or docker: `echo test1234 | bash -c "echo $@"`, `bash -c "echo $@" -bash
"test1234"`, `bash -c "echo $@" -bash_input "test1234"`.

The first argument passed after -c ... is just a placeholder here, it is
used to set the $0 special parameter.

$ bash -c 'echo $0 $2 $1' foo bar mumble
foo mumble bar



smime.p7s
Description: S/MIME Cryptographic Signature


Missing documentation "-bash_input"

2023-11-28 Thread Klaus Frank

Hi,

I just noticed that the man pages are missing documentation for the 
"-bash" (or better "-bash_input") parameter.


I just found the "-bash" parameter in a script and couldn't find any 
documentation about it, after checking out the source code I found 
"-bash_input" which after some testing is the code path "-bash" is 
somehow also pointing to. Could some documentation about this parameter 
be added to the man pages?


It is defined in y.tab.c in line 3945 as part of yy_string_get and in 
code it is documented with "Let input come from STRING. STRING is zero 
terminated." meaning it uses the provided argument instead of stdin. 
Therefore these are equivalent but with the bash parameter it may be 
better readable when being passed as an argument itself to e.g. nspawn 
or docker: `echo test1234 | bash -c "echo $@"`, `bash -c "echo $@" -bash 
"test1234"`, `bash -c "echo $@" -bash_input "test1234"`.


The section in the docs could be something like:

```
-bash_input, -bash: Can be used in scripts to provide pipe input to bash 
itself.
This is especially useful when calling bash has to be wrapped within 
another command like chroot

E.g. `chroot /path /bin/bash -c "echo $@" -bash "pipe input"`
```

Yours sincerely,
Klaus Frank



smime.p7s
Description: S/MIME Cryptographic Signature