Solved

simple script

Posted on 2000-03-02
5
186 Views
Last Modified: 2010-03-05
I must be a retard since I can't even get this easy script to work:

With perl, I need to open the file "/etc/httpd/zipzap".  There will be two words in "zipzap" file that I need to save as variables.  The zipzap file has the format:

----------(beginning of file)
word1
word2
----------(end of file)

I need to then print them to the shell. (I will be doing other things with them which I already know how to do, I just want to print them to the shell to make sure the script is working.)

I want all the path information and everything else to be in the perl file.  I don't want to have to designate the path and filename of "zipzap" at the shell.  Thanks!  (feeling stupid)

Tim
0
Comment
Question by:GorGor1
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
builder110697 earned 20 total points
ID: 2578948
 #!/bin/perl
  my $fname = "/etc/httpd/zipzap";
  open( TTT, $fname ) || die "Could not open file $fname - $!";
  @item = <TTT>;
  close TTT;
  print $item[0];
  print $item[1];

However, you might need the values without the inherent linefeeds so you need to add something like:

  chomp $item[0];
0
 
LVL 1

Author Comment

by:GorGor1
ID: 2579020
If i wanted to print them to a webpage, would this work?

print "Content-type: text/html\n\n";
print "<HTML><body>";
print "$item[0], $item[1]";
print "</body></html>";

Thanks again  :o)


0
 
LVL 1

Expert Comment

by:builder110697
ID: 2580511
Seems right, but I would use as few print statements as I could.  Saves execution  time.

  print "Content-type: text/html\n\n<HTML><body>\n$item[0], $item[1]\n</body></html>\n";

or

  print "Content-type: text/html\n\n"
    . "<HTML><body>\n"
    . "$item[0], $item[1]\n"
    . "</body></html>\n";
0
 
LVL 1

Author Comment

by:GorGor1
ID: 2580574
One last question, could you slightly modify the script for the case that the file "zipzap" has the format of:

word1:word2

This way I can see the pattern in how to read individual variables from a text file.  Thank you much and the points are yours upon your reply  :o)

Thanks again,

Tim
0
 
LVL 1

Expert Comment

by:builder110697
ID: 2580640
 #!/bin/perl
  my $fname = "/etc/httpd/zipzap";
  print "Content-type: text/html\n\n<HTML><body>\n";
  open( TTT, "<$fname" ) || die "Could not open file $fname - $!";
  foreach ( <TTT> ) {
    chomp;
    my @array = split( /:/, $_ );
    foreach ( @array ) {
      print "<p>$_\n";
    }
  }
  close TTT;
  print "</body></html>\n";

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now