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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With