I am trying to use find in a shell script to count the number of files I have matching a wildcard pattern, then to get the name of the file if there is only one. But I'm having trouble passing the wildcard pattern through to backtick expansion.
FINDCMD="find . -iname *DATA*.txt"
DATACOUNT=$($FINDCMD | wc -l)
if [ $DATACOUNT -eq 1 ]
then
use-data $($FINDCMD)
else
echo bugger
fi
That doesn't work: the shell expands DATA.txt at the time of calling find. I want the asterisks to be passed to find.
If I make it
FINDCMD="find . -iname '*DATA*.txt'"
Then the shell doesn't expand the asteriks, but find gets the single-quotes and matches nothing.