Sujet : Re: Which shell and how to get started handling arguments
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.unix.shellDate : 17. Apr 2024, 02:52:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uvn6ga$17j5g$3@dont-email.me>
References : 1 2 3 4
User-Agent : Pan/0.155 (Kherson; fc5a80b8)
On Tue, 16 Apr 2024 11:11:16 -0000 (UTC), Christian Weisgerber wrote:
On 2024-04-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
There is a tool ...
Or just use a basic POSIX shell. Such things exist, you know.
... ShellCheck that among other things can be used to warn
about unportable code in shell scripts. https://www.shellcheck.net/
I don’t see any mention of “unportability”, only about “bugs”.
Just for fun, I tried this:
#!/bin/bash
collect_expand()
{
local -n arr="$2"
arr=()
coproc expander { find . -maxdepth 1 -name "$1" -print0; }
# must ensure while-loop runs in this process
while read -u ${expander[0]} -rd '' line; do
arr[${#arr[*]}]="$line"
done
wait $expander_PID
} # collect_expand
#+
# Mainline
#-
test_filenames=('file 1.dat' $'file number\n2.dat' $'file\t3 dat')
# test multiple spaces in a row and newlines, among any other odd
# things you can think of!
tmpdir=$(mktemp -d -p '' collect-work.XXXXXXXXXX)
echo "tmpdir =" $(printf %q "$tmpdir")
cd "$tmpdir"
for f in "${test_filenames[@]}"; do
echo "create" $(printf %q "$f")
touch "$f"
done
collect_expand '*dat' found_filenames
for f in "${found_filenames[@]}"; do
echo "found" $(printf %q "$f")
done
cd
rm -rfv "$tmpdir"
and it came back with
Line 9:
while read -u ${expander[0]} -rd '' line; do
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean: (apply this, apply all SC2086)
while read -u "${expander[0]}" -rd '' line; do
Line 12:
wait $expander_PID
^-- SC2154 (warning): expander_PID is referenced but not assigned.
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean: (apply this, apply all SC2086)
wait "$expander_PID"
Line 24:
echo "tmpdir =" $(printf %q "$tmpdir")
^-- SC2046 (warning): Quote this to prevent word splitting.
Line 26:
cd "$tmpdir"
^-- SC2164 (warning): Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
Did you mean: (apply this, apply all SC2164)
cd "$tmpdir" || exit
Line 28:
echo "create" $(printf %q "$f")
^-- SC2046 (warning): Quote this to prevent word splitting.
Line 33:
for f in "${found_filenames[@]}"; do
^-- SC2154 (warning): found_filenames is referenced but not assigned.
Line 34:
echo "found" $(printf %q "$f")
^-- SC2046 (warning): Quote this to prevent word splitting.
Line 37:
cd
^-- SC2164 (warning): Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
Did you mean: (apply this, apply all SC2164)
cd || exit
I would have to count every single one of those messages as spurious.