LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-08-2004, 05:22 AM   #1
pablowablo
Member
 
Registered: Apr 2004
Posts: 131

Rep: Reputation: 15
My first script


It's very simple, just copies some of my files to my windows partition...

Tips are very much welcomed... Pls point out If I'm doing things wrongs or how I could improve this script... I'm still learning and just started reading up on shell scripting
Code:
#!/bin/bash

###############################
#  Script to copy my configuration files to my windows drive (in case I screw up 
#  Linux and be forced to reinstall :p
#  So far these are the only files I manually edited/created
#############################


if [ $# -eq 0 ];then 		#if no parameter then use default directory
	bkup_dir="/mnt/habo/Linux_Backups"
elif [ $# -ne 1 ];then	 	#script accepts only 1 parameter
		echo Too many parameters.. exiting
		exit
else				#use given directory
	bkup_dir=$1
fi

if ! [ -d $bkup_dir ];then	#if directory doesn't exist then create 
	echo Creating directory $bkup_dir...
	if mkdir $bkup_dir;then
		echo $bkup_dir successfully created
	else
		echo Cannot create $bkup_dir...exiting
		exit
	fi
fi

#Copy the files to backup directory
if cp /etc/fstab $bkup_dir/fstab.bak &&
   cp /etc/auto.master $bkup_dir/auto.master.bak &&
   cp /etc/auto.pablo $bkup_dir/auto.pablo.bak; then
	echo Configuration files successfully copied.
else
	echo Could not copy configuration files!
fi
 
Old 08-08-2004, 06:57 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
The code looks fine to me, what happens when you run it?

If you are creating directorys and files on a Windows mount point, you may well run into permission issues.

A couple of suggestions to improve the script.

1. Instead of testing the number of parameters used, as you are only wanting the first paramer try the following:

bkup_dir=${1:-/mnt/habo/Linux_Backups}

This will set the variable bkup_dir to the first parameter $1 if it exists, or the default of /mnt/habo/Linux_Backups if $1 doesn't exist.

2. When you call exit add a non zero number to the end. This way, if you call this script from another script you can check the exit value to see if it worked an exit code of 0 is the default value for a script or program that worked.

Happy learning!
 
Old 08-08-2004, 07:13 AM   #3
pablowablo
Member
 
Registered: Apr 2004
Posts: 131

Original Poster
Rep: Reputation: 15
hey thanks! I followed your advice and it still works.. tho I do not really know the syntax of that line hehe Ima do some more researching.... Thanks for the advice, really appreciate it..


Oh btw, the script runs ok I already set the permission on my Windows drive coz I always use it as a normal user

hehe right now out how to execute this script every 24 hrs using cron...
 
Old 08-08-2004, 07:48 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Cron uses a text file to schedule tasks based on 6 parameters separated by spaces.

Parameters
1 the minute you want the script to run (0-59) or * for every minute
2 The hour you want (0-23) or * for every hour
3 The day of the month (0-31) or *
4 The month (1-12) or *
5 The day of the week (0-6) or * 0=sunday
6 The script (and any parameters) you want to run

You can make a backup copy of your crontab file by running:

crontab -l > /usr/cron_save

Then edit by running:

crontab -e

Insert a line, and add your command to run every 24 hours


Code:
# Comment for my script
00 12 * * * /usr/local/myscript > /tmp/myscript.log 2>&1
This would run /usr/local/myscript every day at 12:00 and create a log of the latest result in /tmp/myscript.log

If you need to restore your original crontab file then run:

crontab /usr/cron_save
 
Old 08-08-2004, 10:05 AM   #5
pablowablo
Member
 
Registered: Apr 2004
Posts: 131

Original Poster
Rep: Reputation: 15
Hmmm do I have to restart the cron daemon to test my script?

I added this line to my crontab

Code:
2 23 * * * /home/pablo/pbkup > /tmp/myscript.log 2>&1
But nothing happened, myscript.log was not created...
 
Old 08-08-2004, 10:11 AM   #6
pablowablo
Member
 
Registered: Apr 2004
Posts: 131

Original Poster
Rep: Reputation: 15
Never mind it works now... I used this instead
Code:
0 22 * * * sh /home/pablo/Pablo\ Scripts/pbkup > /dev/null
Thanks for your help again!
 
Old 08-08-2004, 10:25 AM   #7
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Just so you know... you don't need to restart cron to run anything after you make changes. The program crontab lets cron know to reread the file for the user who invoked it.

That way little peon users can have crontab files for themselves and they don't need to wait for the system to reboot or cron to be restarted. This is one of the many reasons we use crontab instead of just editing the physical file with any old text editor.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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