[PD] [command] can't execute "ls | wc -l"
IOhannes m zmoelnig
zmoelnig at iem.at
Wed Jun 8 11:17:08 CEST 2022
On 6/8/22 09:43, Alexandros wrote:
> While [ggee/shell] outputs the correct number of files in a directory
> with the message "ls | wc -l", [command] outputs an error code 2 with
> "exec ls | wc -l" and doesn't output anything.
>
> Is this intentional? If so, how can this command be executed by [command]?
probably by chaining up multiple [command]s?
but more importantly: "ls | wc -l" is an anti-pattern: you shouldn't
parse the output of `ls` [1]
e.g. your script breaks as soon as your directory contains files with a
newline in their name.
a safer way would be to run
> find * -maxdepth 0 -type f -exec printf . ";" | wc -c
but this is absymally slow (and still has the problem with the pipe).
a faster way would be to run this simple shell-script:
```sh
i=0
for f in *; do
i=$((i+1))
done
echo $i
```
you can run this by creating a single symbol (without the quotes)
"i=0; for f in *; do i=$((i+1)); done; echo $i"
and then send a message "exec bash -c $1" (with $1 replaced by the
symbol) to [command].
this will launch a full-fledged bash that will then interpret your
little script.
an even better way would be to just use [file glob]
fmgasdr
IOhannes
[1] http://mywiki.wooledge.org/ParsingLs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20220608/b706a581/attachment-0001.sig>
More information about the Pd-list
mailing list