Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check file owner in linux

Tags:

linux

bash

shell

How to check file owner in linux

i am trying to run this bash file

#!/bin/bash
uname2=$(ls -l $1 | awk '{print $3}');
if [ $uname2 == $USER ]
then echo owner
else echo no owner
fi

it gives error ==' unary operator expected. what is wrong? ubuntu server 10.04.

like image 894
Ilqar Rasulov Avatar asked Mar 13 '14 14:03

Ilqar Rasulov


1 Answers

Use = not == for comparison. The test(1) man page says:

STRING1 = STRING2
       the strings are equal

I'd also recommend using stat to find out the owner instead of some ls hacks. Some double quotes and an extra x would also be nice.

#!/bin/bash
uname2="$(stat --format '%U' "$1")"
if [ "x${uname2}" = "x${USER}" ]; then
    echo owner
else
    echo no owner
fi
like image 181
Cristian Ciupitu Avatar answered Sep 30 '22 08:09

Cristian Ciupitu