Change a file's permissions? : chmod « File « Perl

Home
Perl
1.Array
2.CGI
3.Class
4.Data Type
5.Database
6.File
7.GUI
8.Hash
9.Language Basics
10.Network
11.Regular Expression
12.Report
13.Statement
14.String
15.Subroutine
16.System Functions
17.Win32
18.XML
Perl » File » chmod 




Change a file's permissions?
      
    #!/usr/local/bin/perl -w

    use Getopt::Long;

    my $ret = GetOptions ("f|filename:s""p|permission:s");
    my $filename = $opt_f || die "Usage: $0 -f filename -p Permission\n";
    my $newPerm = $opt_p || die "Usage: $0 -f filename -p Permission\n";

    # Does the file exist?
    if (! -e $filename)
    {
       print "The file $filename does not exist.\n";
       exit;
    }

    # Translate the string mode to an octal value
    my $mode = oct($newPerm);

    # Change the permissions of the file.
    if ((chmod $mode, $filename!= 1)
    {
       print "Error: Could not change permissions on $filename : $!\n";
    }

   
    
    
    
    
    
  














Related examples in the same category
1.The chmod function modifies the read, write, and execute permissions on a file.
2.Apply chomp to all elements of an array
3.The chmod Function (UNIX)
4.The chmod Function (Windows)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.