Results 1 to 6 of 6
Hey there,
I'm attempting to create a shell script using Ubuntu that will take one optional argument (which in this case is a path to a directory to backup) and ...
- 07-22-2009 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 3
Backup script
Hey there,
I'm attempting to create a shell script using Ubuntu that will take one optional argument (which in this case is a path to a directory to backup) and will back up (by using the tar command) the chosen directory which is the /home. And i want, after tar'ing the home directory, for it to save the output in /var/backups with the file name as the date.
I'm very new to this so I was just hoping I could get pointed in the right direction!
Cheers.
- 07-22-2009 #2
Hi Welcome !
Since you want direction rather than code
check out this link for bash shell programming (with list of examples) Advanced Bash-Scripting Guide
Learning bash will be very useful - Hope this helps
If you stuck some where with your program,post it here.- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 07-22-2009 #3
What exactly do you need direction for? I assume you know how to tar things and move them to other directories? Is it just the argument handling that is new for you?
- 07-22-2009 #4Just Joined!
- Join Date
- Jul 2009
- Posts
- 3
Yup the arguements are something im not familar with
- 07-23-2009 #5
Well if you name your program 'backup', and you want to call it like: backup /home to backup the home directory, then you will need to use positional arguments. They are referenced as $X where X is a number.
$0 is the name of the program, i.e. backup
$1 for you will be the directory to backup...
$2 would be extra...
To count the number of arguments (not including $0), you use $#. So you would want to check to make sure that the number of arguments is correct.
to check if $1 is emptyCode:if [ -z $1 ] then echo "Error: Too few arguments" echo "Usage -- $0 [dirtobak]" fi
to check if there are extra arguments.Code:if [ $# -gt 2 ] then echo "Error: Too many arguments" echo "Usage -- $0 [dirtobak]" fi
Once you have determined that the arguments were syntactically correct, you have to determine if they are valid. Since you expect $1 to be a directory, you'll want to make sure it isn't anything else.
to check if $1 isn't a directory.Code:if [ ! -d $1 ] then echo "Error: Invalid directory $1" echo "Usage -- $0 [dirtobak]" fi
Hopefully this will give you some help...
- 07-23-2009 #6Just Joined!
- Join Date
- Jul 2009
- Posts
- 3
Cheers for your help everyone im sure im heading in the right direction now and stuff seems to be coming along!


Reply With Quote
