如何从 Perl 设置文件权限? [英] How can I set file permissions from Perl?

查看:49
本文介绍了如何从 Perl 设置文件权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个生成 Bash 脚本的 Perl 脚本.我使用 open()> 模式将所有内容输出到新文件.标准的东西:

I'm writing a Perl script that generates a Bash script. I'm using open() with a mode of > to output everything to a new file. Standard stuff:

open (FILEOUT, ">", "rename.sh") or die "Can't create rename.sh";

生成的 .sh 文件是只读的,八进制值为 444.在 perldoc 中,它说我可以将 + 添加到 > (open (FILEOUT, "+>", "rename.sh")) 使新创建的文件可读可写,或666.

The resultant .sh file is read only, with an octal value of 444. In perldoc it says I can add a + to the > (open (FILEOUT, "+>", "rename.sh")) to make the newly created file readable and writable, or 666.

有没有办法使用 open() 使新文件可执行(755 或其他任何文件)?如果没有,为新文件设置文件权限的最佳方法是什么?

Is there a way to make the new file executable (755 or anything else) using open()? If not, what's the best way to set file permissions for the new file?

推荐答案

您需要chmod 像这样的文件.

You will want to chmod the file like this.

chmod 0755, $filename;
#or
chmod 0755, $fh;

或者,如果您使用 sysopen 并设置 umask 适当地,你可以不用 chmod.

Alternatively, if you use sysopen and set the umask appropriately, you can do without chmod.

这篇关于如何从 Perl 设置文件权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆