I amI'm trying to write a simple bash function that takes, as its 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-existent 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 occuringoccurring 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
}