LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-06-2009, 12:44 AM   #46
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
A Script That Acts as An Interactive Front Ent to An Emailer


i want to write a script that
-ask the user which files they want to sent
-tar all the files together
-Gzip the tarfile
-Uuencode the gsipped file (so more email programs can use it)
-ask user who to send email to, possibly more than one person
-I want it to be clear and easy to use and display what it is doing while an action is taken place.

I have no clue where to begin. I think perl would be the best option, but I am not sure, how would I make this able to run with am email program and I am unaware how to tar and gzip files or Uuencode the gsipped files.

Can someone help me get my feet wet so I can make this program? I have no clue where to begin.
 
Old 04-06-2009, 12:58 AM   #47
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by circuit_girl View Post
i want to write a script that
-ask the user which files they want to sent
<STDIN>, <>
Quote:
-tar all the files together
Archive::Tar
Quote:
-Gzip the tarfile
Perl gzip
Quote:
-Uuencode the gsipped file (so more email programs can use it)
Perl uuencode

Quote:
-I want it to be clear and easy to use and display what it is doing while an action is taken place.
am not going to do that for you. sorry..pls at least give it a try


Quote:
I have no clue where to begin.
yes you do. you already know Perl.
 
Old 04-06-2009, 01:11 AM   #48
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
That is just what I needed to get started. I just never know what would be the best language. Everyone says the one that gets it done but I have been working with bash and awk and they are way more easier to get a simple tasks done with one line as apposed to 50+ lines I was using with perl. One script I wrote in perl was more than50 lines and It took 2 lines to write using awk. that is why I am trying to learn these very useful tools.
 
Old 04-06-2009, 01:25 AM   #49
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by circuit_girl View Post
That is just what I needed to get started. I just never know what would be the best language. Everyone says the one that gets it done but I have been working with bash and awk and they are way more easier to get a simple tasks done with one line as apposed to 50+ lines I was using with perl. One script I wrote in perl was more than50 lines and It took 2 lines to write using awk. that is why I am trying to learn these very useful tools.
the things that you asked for, gzip, uuencode, sending of email , tarring of files, asking user for input all can be done with the shell. These are the commands you can use
1) gzip
2) tar
3) uuencode
4) read (bash) (ask for user input)
5) sendmail, mailx, mail (for sending email)

Just that you already have knowledge in Perl, and Perl provides various modules for you already (especially CPAN), that's why i never advise you to start with shell. I cannot "force" you to use Perl though, there fore if you are interested to do them in shell, use the above commands and check out the bash link in my sig for more info.
 
Old 04-06-2009, 02:12 AM   #50
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by circuit_girl View Post
That is just what I needed to get started. I just never know what would be the best language. Everyone says the one that gets it done but I have been working with bash and awk and they are way more easier to get a simple tasks done with one line as apposed to 50+ lines I was using with perl. One script I wrote in perl was more than50 lines and It took 2 lines to write using awk. that is why I am trying to learn these very useful tools.
Just wondering what it was ? Could you please post both examples ?
 
Old 04-06-2009, 12:02 PM   #51
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,121

Rep: Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451
Quote:
Originally Posted by circuit_girl View Post
That is just what I needed to get started. I just never know what would be the best language. Everyone says the one that gets it done but I have been working with bash and awk and they are way more easier to get a simple tasks done with one line as apposed to 50+ lines I was using with perl. One script I wrote in perl was more than50 lines and It took 2 lines to write using awk. that is why I am trying to learn these very useful tools.
Yeah, but "best" is a relative thing. If you're very 'fluent' in Perl, it'll take you no time to knock something out, where you'd spend HOURS trying to duplicate it in BASH, if you didn't know it well.

IMHO, don't focus on one, but rather use them ALL. If it takes you a long time to get something going in Perl, to duplicate a single shell-command, use the system("<command>") in Perl instead. Use the best parts of ALL the tools you can, and don't try to force everything into a single path.

As far as the 50+ lines...as you learn more, that'll probably decrease drastically. I look at some of the stuff I wrote 10 years ago, and cringe.....
 
Old 04-06-2009, 07:17 PM   #52
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by TB0ne View Post
use the system("<command>") in Perl instead. Use the best parts of ALL the tools you can, and don't try to force everything into a single path.
using system() is still not advisable. Unless its really a third party tool that Perl doesn't have an api/module for.

Quote:
As far as the 50+ lines...as you learn more, that'll probably decrease drastically. I look at some of the stuff I wrote 10 years ago, and cringe.....
I think it really doesn't matter if its 50++ lines as long as its clear and show the reader of the code what's going on.
 
Old 04-07-2009, 03:00 AM   #53
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
using system() is still not advisable. Unless its really a third party tool that Perl doesn't have an api/module for.
...
Well, it really depends - regardless of Perl or another language.

One shouldn't forget that launching a separate process is probably the best encapsulation/separation possible.

It's not that I advise to always use 'system', but very often it is convenient. For example, I found using 'find' through 'system' somewhat more convenient than Perl File::Find module. There is another module with more OO interface, haven't tried it yet.
 
Old 04-07-2009, 01:38 PM   #54
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
I was going to have the user enter a file or file path and hit enter and keep doing that to add all the files they want to send to a temp directory. I will create a temp dir in a script so I can tar them all together with one tar command. Would it be better to use
Code:
mkdir "temp", 0777;
then i want to add the files to that temp dir. Should I use the cp command to add the files? or is there a better approach?

I was thinking the loop would keep grabbing the stdin undtil the the stdin eq 'done'.
then I want to tar them.

What is the difference between mkpath and mkdir?
 
Old 04-07-2009, 02:01 PM   #55
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
If I wanted to add the ability to sent directories, how would I test if the item they added was a file or a directory?
 
Old 04-07-2009, 02:20 PM   #56
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by circuit_girl View Post
I was going to have the user enter a file or file path and hit enter and keep doing that to add all the files they want to send to a temp directory. I will create a temp dir in a script so I can tar them all together with one tar command. Would it be better to use
Code:
mkdir "temp", 0777;
then i want to add the files to that temp dir. Should I use the cp command to add the files? or is there a better approach?

I was thinking the loop would keep grabbing the stdin undtil the the stdin eq 'done'.
then I want to tar them.

What is the difference between mkpath and mkdir?
Thus you disable user from entering an entry called 'done'.

And you shouldn't do anything - Ctrl-D is EOF on STDIN, so, say, you simply write in Perl:

Code:
while(defined(my $user_entry) = <STDIN>)
  {
  # do $user_entry processing here
  }
- Ctrl-D will cause the loop to terminate.
 
Old 04-07-2009, 02:24 PM   #57
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by circuit_girl View Post
If I wanted to add the ability to sent directories, how would I test if the item they added was a file or a directory?
Have you read 'perldoc perlfunc' or 'man perlfunc' ?

Have you seen there

Code:
       Alphabetical Listing of Perl Functions


       -X FILEHANDLE
       -X EXPR
?

Believe me, the answer is there, and it is very short and simple.

At all, read the whole 'perldoc perlfunc' just to learn the wide range of useful functions
 
Old 04-07-2009, 07:00 PM   #58
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,121

Rep: Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451
Quote:
Originally Posted by ghostdog74 View Post
using system() is still not advisable. Unless its really a third party tool that Perl doesn't have an api/module for.


I think it really doesn't matter if its 50++ lines as long as its clear and show the reader of the code what's going on.
I totally agree with both of those statements. However, if you're learning, the system call is a handy 'band-aid' to get you through some hurdles, until you learn the 'real' way to do it.

And another thing is...COMMENT HEAVILY. Even though a program makes sense to you now, let it run for six months or so, then come back to it, and see if you can still figure it out. I always try to comment things, and explain where the data is coming from, what is happening to it, and where it goes later.
 
Old 04-07-2009, 07:52 PM   #59
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by TB0ne View Post
I totally agree with both of those statements. However, if you're learning, the system call is a handy 'band-aid' to get you through some hurdles, until you learn the 'real' way to do it.
i advocate learning to do it "right" from the start .

Quote:
And another thing is...COMMENT HEAVILY. Even though a program makes sense to you now, let it run for six months or so, then come back to it, and see if you can still figure it out. I always try to comment things, and explain where the data is coming from, what is happening to it, and where it goes later.
Comments are necessary, but not too heavy. i am sure you know what i mean. comments can be reduced also if meaningful names are used. etc..
 
Old 04-19-2009, 08:21 PM   #60
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
Code:
mkdir "temp", 0777;    
system ("chmod 777 temp");  
do
{
  print "What files or folders do you want to send? (Hit ENTER after each file, to finish hold CTRL+D)";
  $user_entry = <STDIN>;
  if ( $user_entry )                                                      #IF FILE EXSISTS REMOVE THE PATH FOR TEMP FILE NAME AND ALL UNSAFE CHARACTERS####
  {
    $filename = $user_entry;
    my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );    #REMOVE THE PATH
    $filename = $name . $extension;                                       #CONCANONATE THE NAME OF FILE AND EXTENTION WITHOUT PATH
    system ("cp $user_entry $filename");
    print "This is what you entered; $user_entry  with the file name of: $filename \n";
  }
}
while ($user_entry);

print "Thank you for using my AMAZING emailer\n";
I have this right now but I am not sure how to go about getting files from a different location then teh current directory. I have some files that I want to sent that are located up the file tree and down some. how do I copy those files into the current temp directory I have created.

I am going to delete this directory and its contents when the emailer is done sending. What is the full comand I can use? rmdir -a???

thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
i need to write a script that automatically logins venix Linux - Newbie 3 03-03-2009 01:34 PM
Strange remote logins after starting a gnome session Clemente Linux - Desktop 2 11-11-2008 02:14 PM
Looking for software to track logins and commands with auto-email DukeLeto Linux - Software 1 07-20-2007 05:14 PM
scripting the unix command script lmcthbe Linux - General 7 05-28-2003 02:49 PM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

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

All times are GMT -5. The time now is 02:30 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