You seem to be having difficulties to understand how pipes work. You cannot "natively" use the "result" (stdout) of a pipe (the left-hand side) as a variable on the right-hand side of a pipe, you either need to consume and read it into a variable, e.g.
printf "line1\nline2\n" | while read line; do_stuff_with "${line}"; done
or you need to use command substitution (and optionally assign it to a variable), e.g.
files=$(find "$1" -maxdepth 1 -type f -printf . | wc -c)
A few further notes:
$@ expands to all positional parameters, in case of multiple arguments your [ -d "$@" ] will fail.
- The
ls is completely superfluous
find works recursively, but I guess you only want the first directory level to be checked so this needs the maxdepth parameter
- This will break on weird paths with newlines which can be worked around by telling
find to print a character for each found directory/file and then count bytes instead of lines
In case you really don't want this to be recursive it might be easier to just use globbing to obtain the desired result:
$ cat t.sh
#!/bin/bash
for file in "${1-.}"/*; do
[ -d "${file}" ] && ((directories++))
[ -f "${file}" ] && ((files++))
done
echo "Number of files: ${files-0}"
echo "Number of directories: ${directories-0}"
.
$ ./t.sh
Number of files: 6
Number of directories: 1
$ ./t.sh /tmp
Number of files: 9
Number of directories: 3
You might want to check man test to tweak with regards to links to obtain your desired result.