Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting user id (owner) of a file / directory 1

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
hi gurus,

I can get the user id of a file / directory in perl using the below snippet.

$uid = (stat("$pgm_name"))[4];
chomp($userid = qx!cat /etc/passwd | cut -f 1-4 -d ":" | grep -i ":$uid:" | cut -f 1 -d ":"!);
printf "%s\n", $userid;


I need to get the user id in a kshell script. Something like the below
USERID=`perl -e "XXXXXX"`

Any suggestions / alternatives ?
Thanks
philipose
 
I got the below to work but still think that the perl soln was more elegant

ls -ld $DIR} | cut -c15-24 | sed -e 's/ //g'
 

It's more efficient to use cut or awk on the 3rd field:

Code:
DIR=./mydir

USERID=$(ls -ld "$DIR"|cut -d' ' -f3)
echo $USERID

Some versions of Unix (like Solaris 9) has problems with cut-s -f option. I would use awk:

Code:
USERID=$(ls -ld "$DIR"|awk ' { print $3 } ')
echo $USERID
 
Hi olded,
I had also tried to cut the third field but did not work. hence i resorted to cutting the character. the solution you provided with awk works as advertised.
thanks
philipose
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top