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:
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