1

I am writing a script and I need to be able to write additional configuration information to a .conf file.

The Issue:

The issue is I am unable to write additional data to a file due to a permission issue. Here is the code I am using:

my $conf = "/etc/lighttpd/lighttpd.conf";
open my $handle, ">>", $conf or die $!;
# spacing counts, hence the indentation
print $handle 'fastcgi.server = (
    ".php" => ((
        "bin-path" => "/usr/bin/php-cgi",
        "socket" => "/tmp/php.socket"
    ))
)'."\n";
close $handle;

Since the lighttpd.conf file is under the umbrella of root:root, I'm unable to write it without doing something along the lines of:

system("sudo chown $USER:$USER /etc/lighttpd/lighttpd.conf");
# write to file...
system("sudo chown root:root /etc/lighttpd/lighttpd.conf");

The above seems rather hackish to me and I'd rather avoid this.

The Question

Are there more elegant ways to go about writing this information to the file? Perhaps a BASH command I am unaware of?

4

2 回答 2

0

解决了

根据shawncorey 的建议,我删除了我拥有的所有实例

 system("sudo [command]");

并按如下方式运行脚本:

 sudo ./test.pl

不用说它工作得很好!

于 2013-07-13T18:26:27.280 回答
-2

您可以运行“chown”来更改 perl 中文件列表的所有者(和组)。

例子:

#!/usr/bin/perl

$cnt = chown $uid, $gid, 'foo', 'bar';
chown $uid, $gid, @filenames;
于 2013-07-13T11:06:24.630 回答