1835243 Members
2601 Online
110078 Solutions
New Discussion

PERL's chown

 
Chern Jian Leaw_1
Occasional Contributor

PERL's chown

HI,

I've inserted a PERL script which is suppose to chown the ownership of files/directories, also links.

The script works ok for ordinary files and directories, but it just map the old uid to the new uid.

Could someone help me in debugging the script?

Thanks.
3 REPLIES 3
Steve Steel
Honored Contributor

Re: PERL's chown

Hi

Look at man chown


You need -h for links


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
H.Merijn Brand (procura
Honored Contributor

Re: PERL's chown

%OldNew = qw ( #maps old user id to new user id
17190 0
);

is (one of your) your problem(s) :)

read again what qw() does. You've just created a hash that looks like

%OldNew = (
"#maps" => "old",
"user" => "id",
"to" => "new",
"user" => "id",
"17190" => 0,
);

probably not what you expected.
Now start adding

use warnings;
use strict;

and see what happens
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: PERL's chown

and BTW, you should take a look at the following perl functions

- stat
- lstat
- readlink
- unlink
- symlink

HP-UX inhibits changing the ownership of a symbolic link from the OS points of view (Steve already pointed you to the -h option of the chown command itself). If you have write permission to the directory, you can remove the link and recreate it (using symlink) as the new user. The symlink then gets the new user id. You can also achieve your goal using 'system "chown -h $newuser $file"'. Don't parse 'ls -ld' output, use stat/lstat/readlink instead, or - even better - the abs_path () function from the Cwd module, which will also 'do the right thing' if the symlink points to a symlink points to a symlink ...

NAME
Cwd - get pathname of current working directory

SYNOPSIS
use Cwd;
my $dir = getcwd;

use Cwd 'abs_path';
my $abs_path = abs_path($file);

DESCRIPTION
This module provides functions for determining the pathname of the
current working directory. It is recommended that getcwd (or another
*cwd() function) be used in all code to ensure portability.
Enjoy, Have FUN! H.Merijn