LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2008, 04:57 PM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Perl CGI -- Edit sendmail aliases


Hello,


I am trying to write a small CGI program to edit one line of a very long aliases file. The one line consists of the name: pageoncall and a pager number in the form of: 1234567890@pager.com. The pager number listed within that aliases could be in several other locations within the file. I have written most everything with the exception of this. I cannot figure out how to edit just that one line within the file. Here is the code that i have so far:


Code:
#!/usr/bin/perl


use CGI qw(:standard);
open(FILE, "/test/aliases") or die "Couldn't open $0";
    
        print header('text/html'),
                start_html('Answer the call!'),
                h1('Select your pager number and click submit:'),
                start_form,
                popup_menu(-name=>'pager',
                           -values=>['','1234567890@messaging.sprintpcs.com',
                                     '1234567890@archwireless.net',
                                     '0987654321@archwireless.net']),p,
                submit,
                end_form;

        if (param() )
        {   
                my $pager       = param('pager');

                if($pager == '') 
                {   
                        print "You selected an invalid number, please try again.";
                }   

                while(<FILE>)
                {   
                        if(/^pageoncall:/)
                        {   

                                print FILE "pageoncall: $pager";
                        }   
                }   
    
        }

Any help would be greatly appreciated.

Last edited by 0.o; 01-06-2008 at 08:38 PM.
 
Old 01-08-2008, 01:21 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If the file is not HUGE, I like to do this sort of thing by reading the entire file into an array, and then iterating over the array, performing modifications/edits on each iteration. Finally, write the array back out to the original file, or a new file.
(untested)
Code:
    open( FILE, "$filename" ) || die "No can do... $!\n";
    my @fileData = <FILE>;
    close FILE;
    foreach my $line ( @fileData ){
        if( $line =~ m/^pageoncall:/ ){
             # Modify $line according to your requirements here...
        }
    }
    open( FILE, ">$filename" ) || die "No can do... $!\n";
    print FILE @fileData;
    close FILE;
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl CGI:Can't locate CGI.pm supermyself Programming 13 09-10-2007 06:22 AM
sendmail and NIS databases (aliases, mail.aliases) - what kind of databases? cotton213 Linux - Software 0 03-14-2006 05:57 PM
sendmail aliases guduke Linux - Software 3 03-14-2006 06:11 AM
Of Perl CGI scripts and Sendmail Chrax Programming 2 06-06-2005 04:21 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration