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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct Permission Setting Procedure

Status
Not open for further replies.

vego

Programmer
Jun 26, 2004
47
US
Hi, I'm confused on the correct way to set permissions to files/directories in a script. With windows, it's a no-brainer, but on a Debian/Linux server, I try to set permissions (automatically) from within the script like this and it won't work.
Code:
if (!open(FH, $file)) {
    open(FH, ">$file") || die $!;
    chmod(0666, $file) || die $!;
    close(FH) || die $!;
}
...is this why it doesn't accept the proper permissions? Should I set the permissions to something else? If I directly create this blank file and set the permissions to 0666, I have no problems. Can anyone set the record straight for me?

Thanks,
 
It seems that opening the file puts lock on it.
Code:
$file = 'mbox' ;
if (-e $file)
{
 chmod(0666, $file) || die $!;
}

But I wonder if checking the file existence is really what you meant.

Regards,
Zephan
 
Maybe you'd find it helpful to look at this?
perldoc -f -x
 
Thank you both for your input...

Zephan, I have tried the existence format, and there is no change in the outcome.

Mike, I'm lost in checking out your reply. What exactly should I look for?

Just for the record, I have used my above-stated routine for creating a new file (from within a script) on other Linux (BSD) machines with no problems.

Any further insight appreciated.
 
I tried your initial script, and I noticed as well as you that permission remain unchanged. I removed the if (!open(FH, $file)) condition and everything worked right.
What I'm saying is not checking for existence of the file solves the problem, but try to change the permissions before openning the file.
Try your script this way :
Code:
if (!open(FH, $file)) {
[b]    close(FH) || die $!;
    chmod(0666, $file) || die $!;[/b]
    open(FH, ">$file") || die $!;
    close(FH) || die $!;
}

And let me know if it works.

Regards,
Zephan
 
...well, I don't know what was happening before, but now this "SAME CODE" worx just fine...???


Code:
if (!open(FH, $file)) {
    open(FH, ">$file") || die $!;
    chmod(0666, $file) || die $!;
    close(FH) || die $!;
}

...yours does as well...thanx!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top