I'm currently trying create a function that retrieves a files owner name whit the following function:
OWNER_IDX=3
fileowner=""
#Gives file owner name. Takes the file name as an argument
function getfileowner()
{
fileowner=`ls -l | grep $1 | cut -f $OWNER_IDX -d " "`
echo $fileowner
}
To verify what my function does exactly, I created a test function:
function testgetfileowner()
{
for file in `ls`
do
getfileowner $file
echo "$file belongs to $fileowner"
done
}
But when I execute my script, I get this output:
exemple_gz.gz belongs to
exemple_tar.tar belongs to
exemple_tgz.tgz belongs to
exercice_10.sh belongs to
exercice_8.sh belongs to
INFOH304 belongs to
lipap.sh belongs to
I test the line
fileowner=`ls -l | grep $1 | cut -f $OWNER_IDX -d " "`
on a terminal as follows:
ls -l | grep exemple_gz.gz | cut -f 3 -d " "
and it works fine. What did I do wrong? Thanks in advance.