LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-16-2007, 01:38 AM   #1
ryanbrimmer
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Rep: Reputation: Disabled
Script Review


Hi, I recently started at this company and I don't know a whole lot about writing scripts. I was wondering if someone could review the script below and let me know if i could make any changes on it.

Thanks,

-Ryan

#!/bin/bash
#Copying files to Temporary Daily Log File
$ cp /audit /audit/dailylogs

# Emailing logs to network administrator.
$ cat crontab
MAILTO=rbrimmer@coadmin.net
00 00 1 * * /audit/dailylogs
End
 
Old 06-16-2007, 04:22 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Hi, and welcome to LQ!

If this is your script
Code:
#!/bin/bash
#Copying files to Temporary Daily Log File
$ cp /audit /audit/dailylogs

# Emailing logs to network administrator.
$ cat crontab
MAILTO=rbrimmer@coadmin.net
00 00 1 * * /audit/dailylogs
End
pretty much everything but the first two lines is wrong.
$ is not part of a command, it's part of an interactive
prompt.

cron entries don't belong into shell scripts.

End is not a bash keyword.



Cheers,
Tink
 
Old 06-16-2007, 07:20 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
It's time for some books....

Bash Guide for Beginners, by Machtelt Garrels (at tldp.org)

Classic Shell Scripting from O'Reilly
 
Old 06-16-2007, 02:59 PM   #4
ryanbrimmer
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: Disabled
Wow!

Like I said I am new at this. All I have done is research, and apparently by your disheartening reply it's not helping. In any case my deadline for the script is Monday. Any other ideas?

-Ryan
 
Old 06-16-2007, 03:07 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Well ... if you told us what your objectives are, maybe?


Cheers,
Tink
 
Old 06-16-2007, 03:21 PM   #6
ryanbrimmer
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: Disabled
Excellent Point Tinkster....

Sorry about that. I need to write the script to email me the auditlogs file every morning for reveiw.

Thanks,

-Ryan
 
Old 06-16-2007, 03:30 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
At a given time, or do you want to kick that off manually?

Seeing the cron entry I'll assume that you want it timed.
The stuff below is untested and probably won't work for
you - but it may give you a hint in the right direction.

Give your script a sensible name, and put it in a place
that cron knows about (or specify the full path in cron).

If the file gets rotated each night you can use something like
Code:
#!/bin/sh
cat `find /audit -ctime -1 -type f`| mail -s "audit log" rbrimmer@coadmin.net
And in crontab (crontab -e username)
Code:
1 0 1 * * /path/to/script
Note: on some systems this will send an email to root or the
process owner informing them about the status of the script run.



Cheers,
Tink

Last edited by Tinkster; 06-16-2007 at 03:31 PM.
 
Old 06-16-2007, 03:45 PM   #8
ryanbrimmer
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: Disabled
Post Thanks.

I actually want the script automated, not manual. How did you learn this stuff? Just experience? I have had my head in books and online tutorials for weeks now, and it just won't click.
 
Old 06-16-2007, 03:52 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Experience, reading man-pages, and trying to understand how
Slackware works ;} ... which is quite easy because of its
KISS approach to everything.

Hope it helps a bit?


Cheers,
Tink
 
Old 06-16-2007, 04:06 PM   #10
ryanbrimmer
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks a bunch.

That just cut my work load in half. I still have to prepare the presentation for my executive staff. I do want to learn writing scripts and understand Linux more. I just get so frustrated in books, and the tutorials. I read them so much my eyes cross . I'll just keep reading and maybe one day it will click for me.

It help immensly, thanks again.

-Ryan
 
Old 06-16-2007, 04:42 PM   #11
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by ryanbrimmer
I actually want the script automated, not manual. How did you learn this stuff? Just experience? I have had my head in books and online tutorials for weeks now, and it just won't click.
Cool to hear Tink's helped you out - I'm not remotely a whiz myself but maybe something in all this will also help.

In the simplest sense, a shell script just a file that's been chmod'ed executable, has '#!/bin/sh' as its preamble, and is just a list of commands after that - a 'batch file' of commands that can be just as you'd type them in at the command line, only wrapped up in a single simple invocation. Then you can get a lot more elaborate from there.

To take my usual shell, the bash manual page lists about half a dozen metacharacters, about a half-dozen more control operators, and a few reserved words. (! case do done elif else esac fi for function if in select then until while { } time [[ ]])

It then describes simple commands, pipelines, lists, and compound commands, which include many of the reserved words and control flow constructs (if, for, while, case).

You learn about these and quoting and the various expansions and substitutions (brace, tilde, parameter, command, arithmetic, process, pathname) and learn about redirection, functions, conditional expressions, and signals and you've basically got it covered.

The real trick is learning the shell builtins and standard external *nix commands to actually harness all their power. The shell scripting framework itself is not too bad. As an infodump, it sounds like a lot, but it's not too terribly many pieces (maybe a dozen or two from a certain way of counting) and if you just break it down to those pieces and take things a step at a time, you'll get it.

So I recommend using the command line as your standard interface (excluding web browsing and image editing and the like, of course - though a lot can be done with that there, too) so you get familiar with all the tools at your disposal (by using them and reading their manual pages). If you don't tend to use the CLI utilities regularly it's going to be like a strange incomprehensible universe but, if you're familiar with them, it's just a case of getting your familiar troops lined up and making them march. And then reading some scripting book straight through just to get a lay of the land and sort of break the ice on what can be a somewhat cryptic manual page at first, and then tear into your shell's manual page, itself. And practice. And re-read the manual page. But everybody has their own learning methods, so just keep experimenting until you find something that works for you. Maybe you're actually reading dry abstractions too much and need to do more of the hands-on practice. Either way - good luck.
 
Old 06-16-2007, 06:17 PM   #12
ryanbrimmer
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: Disabled
That was a much more broken down explanation. I think I was trying to just take it all in one big chunk and read all the dry material on it. You're also absolutely correct about actually getting into the command line and reading the man pages. Getting hands on experience is probably the best way for me to learn at this point.

Thanks,

-Ryan
 
  


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
LQ Review dudeman41465 LQ Suggestions & Feedback 4 10-30-2006 02:13 PM
please review my startup script Serena *BSD 1 07-24-2006 09:54 AM
is there a review for this? Zuggy General 2 12-22-2004 05:17 AM
bash script: Review please carmstrong Linux - General 1 06-13-2004 09:43 AM
IPtables Script Review carmstrong Linux - Security 6 05-04-2004 12:55 AM


All times are GMT -5. The time now is 08:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration