tags:

views:

168

answers:

2

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";

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.

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?

+1  A: 

Putting + infront of < or > allows you to open the file in both read and write mode.

In you case you can chmod the newly created file.

codaddict
+5  A: 

You will want to chmod the file like this.

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

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

Leon Timmermans
Excellent. Both options work great. I got `sysopen` to work like so: `use Fcntl; sysopen (FILEOUT, "rename.sh", O_RDWR|O_EXCL|O_CREAT, 0755);`
Andrew