You seem to want to count the number of names matching *patternX*
and then test whether that is greater than or equal to three.
This is best done like so:
shopt -s nullglob
set -- patternX
if [ "$#" -ge 3 ]; then
echo 'More than 2 names match the pattern'
fi
This is setting the positional parameters to the names matching the pattern. The number of matching names would be kept in $#
. The nullglob
option is set so that if there are no names matching, the pattern is removed completely rather than kept unexpanded.
You could also use a named array to store the matching names:
shopt -s nullglob
names=(patternX)
if [ "${#names[@]}" -ge 3 ]; then
echo 'More than 2 names match the pattern'
fi
See also Why *not* parse `ls` (and what to do instead)?
Without the words if
and then
:
shopt -s nullglob
set -- patternX
[ "$#" -ge 3 ] && echo 'More than 2 names match the pattern'
A similar approach using awk
:
shopt -s nullglob
awk 'BEGIN { if (ARGC > 3) print "more than 2" }' patternX
Note that ARGC
in awk
also accounts for the command name (what would be $0
in the shell).
if
), as your requirement itself has an "if". You can save the wc output to a shell variable, but without knowing more about your goal, that may be useless. (I.E., let's say you can[ $result -gt 3 ]
... but what do you want to do with the comparison? it will just be a true/false boolean value.) – C. M. Apr 25 '21 at 05:36