0

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.

Dixel
  • 516
  • 1
  • 4
  • 9
  • 4
    Why don't use simply use `stat -c %U $1` ? And even with your code, the `grep` phase is useless, you can directly do `ls -l $1` – Jaffa Nov 29 '18 at 11:02
  • 2
    Also instead of ```for file in `ls` ``` write `for file in *`. It is safer and shorter. – Socowi Nov 29 '18 at 11:40
  • @Geoffroy I'm not allowed to use any command I haven't learned yet. But thanks for the advice, it's much more easy. The reason I use the `grep` phase is because using `ls -l $1` does not work properly when $1 is a folder. – Dixel Nov 29 '18 at 12:25
  • 1
    @genkidesuka `ls -ld` is that case, so it lists the directory itself not its content – Jaffa Nov 29 '18 at 12:31
  • I question a class that can't provide appropriate assignments for the material presented so far. – chepner Nov 29 '18 at 13:51

2 Answers2

2

$fileowner is never set to any value in your test, you should return the value in getfileowner.

Also, as I said in the comments, you can directly use stat -c %U filename to get the username.

You have another problem in your function: ls -l | grep $1 can match several files and is redundant, whereas ls -dl $1 does what you want directly, the -d allowing to list a directory itself, not its content.

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • @Geoffoy I returned the value in getfileowner() with `echo "\`ls -ld $1 | cut -f $OWNER_IDX -d " "`"\`` and retrieved it in my test function as follows `echo "$file belongs to $(getfileowner $file)"` as you suggested. And it works. Thanks a lot. – Dixel Nov 29 '18 at 13:41
  • That's not a return value, you're just printing something on stdout and capturing it on the other side... But yeah it can work this way too, after all. – Jaffa Nov 29 '18 at 13:59
0

As shown in this post another solution is to print on stdout instead of returning the value and capture it with a command substitution.

#Gives owner name. Takes the file name as an argument
function getfileowner()
{
    echo "`ls -ld $1 | cut -f $OWNER_IDX -d " "`"
}
function testgetfileowner()
{
    for file in *
    do 
        echo "$file belongs to $(getfileowner $file)"
    done
}
Dixel
  • 516
  • 1
  • 4
  • 9
  • The `echo` is not needed in `getfileowner`, as the commands will print on stdout anyway. – Jaffa Dec 03 '18 at 09:53