First off, sorry for the title. I'm not sure of the correct terminology so if anyone can improve upon it that would be good.
I am trying to write a simple bash function that takes, as it's arguments, a number of files and/or directories. It should:
- Fully qualify the filenames.
- Sort them.
- Remove duplicates.
- Print all that actually exist.
- Return the number of non existant files.
I have a script that almost does what I want, but falls down on the sorting. The return value of the script as it stands is correct, but the output is not (unsorted and duplicates). If I uncomment the | sort -u statement as indicated, the output is correct but the return value is always 0.
N.B. Simpler solutions to solve the problem are welcome but the question is really about why is this occuring in the code I have. That is, why does adding the pipe seemingly stop the script incrementing the variable r?
Thankyou for your help. Here's the script:
function uniqfile
{
local r=0
for arg in "$@"
do
readlink -e "$arg" || (( ++r ))
done #| sort -u ## remove that comment
return $r
}

for arg in "$@"tofor arg. "If 'in WORDS ...;' is not present, then 'in "$@"' is assumed." - help for – manatwork Sep 30 '11 at 9:35