本文共 9666 字,大约阅读时间需要 32 分钟。
perl系统管理
您是否知道Perl是系统管理员的绝佳编程语言? Perl与平台无关,因此您可以在不同的操作系统上执行操作,而无需重写脚本。 Perl中的脚本编写既快捷又容易,其可移植性使您的脚本非常有用。 这里有一些例子,只是为了让您的创意源源不断!
假设您需要重命名目录中的一堆文件。 在这种情况下,我们有一个充满.xml
文件的目录,我们想将它们全部重命名为.html
。 十分简单!
#!/usr/bin/perl use strict ; use warnings ; foreach my $file ( "*.xml" ) { my $new = ( $file , 0 , - 3 ) . "html" ; $file , $new ; }
然后只需cd到需要进行更改的目录,然后运行脚本。 如果需要定期运行,可以将其放在cron作业中,并且可以轻松地增强它以接受参数。
说到接受参数,让我们看一个脚本。
假设您需要在系统上定期创建Linux用户帐户,并且用户名的格式为名字的名字/姓氏,这在许多企业中很常见。 (当然,这是一个好主意,直到您让John Smith和Jane Smith在同一家公司工作–或希望John在两个不同部门兼职时都拥有两个帐户。但是让我感到幽默,好吗? )每个用户帐户都必须根据其部门位于一个组中,并且主目录的格式为/ home / <部门> / <用户名> 。 让我们看一个脚本来做到这一点:
#!/usr/bin/env perl use strict ; use warnings ; my $adduser = '/usr/sbin/adduser' ; use Getopt :: Long ( GetOptions ) ; # If the user calls the script with no parameters, # give them help! if ( not @ ARGV ) { usage ( ) ; } # Gather our options; if they specify any undefined option, # they'll get sent some help! my %opts ; GetOptions ( \%opts , 'fname=s' , 'lname=s' , 'dept=s' , 'run' , ) or usage ( ) ; # Let's validate our inputs. All three parameters are # required, and must be alphabetic. # You could be clever, and do this with a foreach loop, # but let's keep it simple for now. if ( not $opts { fname } or $opts { fname } !~ /^[a-zA-Z]+$/ ) { usage ( "First name must be alphabetic" ) ; } if ( not $opts { lname } or $opts { lname } !~ /^[a-zA-Z]+$/ ) { usage ( "Last name must be alphabetic" ) ; } if ( not $opts { dept } or $opts { dept } !~ /^[a-zA-Z]+$/ ) { usage ( "Department must be alphabetic" ) ; } # Construct the username and home directory my $username = ( ( $opts { fname } , 0 , 1 ) . $opts { lname } ) ; my $home = "/home/$opts{dept}/$username" ; # Show them what we've got ready to go. "Name: $opts{fname} $opts{lname} \n " ; "Username: $username \n " ; "Department: $opts{dept} \n " ; "Home directory: $home \n \n " ; # use qq() here, so that the quotes in the --gecos flag # get carried into the command! my $cmd = ( $adduser -- home $home -- ingroup $opts { dept } \\ -- gecos "$opts{fname} $opts{lname}" $username ) ; "$cmd \n " ; if ( $opts { run } ) { $cmd ; } else { "You need to add the --run flag to actually execute \n " ; } sub usage { my ( $msg ) = @_ ; if ( $msg ) { "$msg \n \n " ; } "Usage: $0 --fname FirstName --lname LastName --dept Department --run \n " ; ; }
与以前的脚本一样,有很多增强的机会,但是,完成此任务可能只需要类似的内容。
还有一个,只是为了好玩!
现在,我们将尝试进行大规模编辑。 假设您有一个充满代码的目录,并且每个文件中都有一个版权声明。 (Rich Bowen写了一篇很棒的文章, 几年前,它讨论了开源代码中版权声明的智慧。这是一本不错的书,我强烈推荐它。但是我还是很幽默。 )您想要更改目录树中每个文件中的文本。 File::Find
和File::Slurp
是您的朋友!
#!/usr/bin/perl use strict ; use warnings ; use File :: Find ( find ) ; use File :: Slurp ( read_file write_file ) ; # If the user gives a directory name, use that. Otherwise, # use the current directory. my $dir = $ARGV [ 0 ] || '.' ; # File::Find::find is kind of dark-arts magic. # You give it a reference to some code, # and a directory to hunt in, and it will # execute that code on every file in the # directory, and all subdirectories. In this # case, \&change_file is the reference # to our code, a subroutine. You could, if # what you wanted to do was really short, # include it in a { } block instead. But doing # it this way is nice and readable. find ( \&change_file , $dir ) ; sub change_file { my $name = $_ ; # If the file is a directory, symlink, or other # non-regular file, don't do anything if ( not - f $name ) { ; } # If it's not Perl, don't do anything. if ( ( $name , - 3 ) ne ".pl" ) { ; } "$name \n " ; # Gobble up the file, complete with carriage # returns and everything. # Be wary of this if you have very large files # on a system with limited memory! my $data = read_file ( $name ) ; # Use a regex to make the change. If the string appears # more than once, this will change it everywhere! $data =~ s/Copyright Old/Copyright New/g ; # Let's not ruin our original files my $backup = "$name.bak" ; $name , $backup ; write_file ( $name , $data ) ; ; }
由于Perl的可移植性,您可以在Windows系统和Linux系统上使用此脚本-由于底层的Perl解释器代码,它可以正常工作。 在我们上面的创建帐户代码中,该代码不是可移植的,而是特定于Linux的,因为它使用Linux命令(例如adduser) 。
以我的经验,我发现将这些东西的Git存储库放置在某个地方,可以在正在使用的每个新系统上克隆,这很有用。 随着时间的推移,您会想到对代码进行更改以增强功能,或者添加新的脚本,而Git可以帮助您确保所有工具和技巧在所有系统上均可用。
我希望这些小脚本能给您一些想法,让您知道如何使用Perl来简化系统管理工作。 除了这些较长的脚本之外,还请查看的精彩列表 Mischa Peterson汇编的 。
翻译自:
perl系统管理
转载地址:http://sydzd.baihongyu.com/