Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new help with bourne scripting 1

Status
Not open for further replies.

enpointe

Technical User
Jul 15, 2003
5
US
I am totally new with scripting, would appreciate any of your help/suggestions.. Wanna write a script which runs every monday and which copys previous weeks files from a folder and zips into one file and email it an email address. I could figure out the email part, but was confused how to do the initial part.. Some of you are super expert in scripting looking at the forums. i believe it wont even take a minute for u guys to write this script. any help will be highly appreciated.. Any suggesting for books on scripting. Thanks in advance.



 
Script would need to be setup in a crontab.

# SCRIPT CRONTAB ENTRY:
30 8 * * 1 /home/SOMEONE/script.ksh

First Field: Minutes
Second Field: Hour
Third Field: Day of month
Fourth Field: Month of year
Fifth Field: Day of Week (0 = Sunday, ..., 6 = Saturday)

Script: (Sample)

#!/usr/bin/sh

FOLDER="YOUR_ABSOLUTE_PATH_TO_FOLDER_HERE"
# Ex: FOLDER="/home/SOMEONE/BACK/UP/FILES"

(cd ${FOLDER}; tar -cvf ../backup.tar .)
# I would use tar since this is pretty universal compared
# to different zip utilities out there.

YOUR_EMAIL_SECTION_HERE

#EOS

Of course you can get more complicated in a hurry, but this should get you started.

As far as books are concerned, I use ksh more often than sh and they are pretty compatable. Most unix books have a small section regarding on how to script. One good book which covers more than just sh, is:

Linux Programming: A Beginner's Guide
Petersen
McGraw Hill, Published October 2000, 421 pages
 
Hi pmcmicha,

First of all, Many Thanks to you for the prompt reply. The script which u gave will tar the entire folder, right ? I want is given by an example x/x/x folder contains 100 files, one new file being created everyday, I wanted only all files before previous weeks to be zip in files.. So for example if today is monday 21th July, I want all files before july 13th to be zipped or moved in one folder, I could figure out the zipping/tar thing .. please let me know if you understood what i m trying to say. thanks for the book, I will look in that book.. And appreciate your reply.
 
This command should do it:

Script: (Sample)

#!/usr/bin/sh

FOLDER="YOUR_ABSOLUTE_PATH_TO_FOLDER_HERE"
# Ex: FOLDER="/home/SOMEONE/BACK/UP/FILES"

(cd ${FOLDER}; find . -mtime +7 > list)
(cd ${FOLDER}; tar -cvf ../backup.tar `cat list`)
# I would use tar since this is pretty universal compared
# to different zip utilities out there.
# You can try this for zip, but some adjustments might have
# to be made:
(cd ${FOLDER}; zip -9 `cat list`)

YOUR_EMAIL_SECTION_HERE

#EOS


The -mtime option on find will find all files that are seven days and older. If you wanted to find the last seven days only, make the +7 a -7 instead.
 
Thanks pmcmicha for the quick response. I deeply apologize for the last appreciation. Was out of office for a vacation, returned today itself. Your script is working absolutely fine and all I need to do now is to figure out the email stuff. Thanks again. If i have any question I will reply on this topic.
 
Hi pmcmicha

Do you think some thing like this would work an attachment and emailing to xxx email address

#!/bin/sh
#scp root@domain.com:/tmp/filetoemail.txt

if [ -e /tmp/filetoemail.txtname.txt ];
then
uuencode /tmp/filetoemail.txt attached.txt | mail -s "Your file is attached" person@yahoo.com
else
mail -s "Caution !! file was not available to send" root@domain.com
fi


I also get following errors:
./email: scp: not found
./email: test: argument expected

Thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top