Register
It is currently Thu Apr 24, 2014 7:36 pm

Scripting Help


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Sun Aug 27, 2006 7:42 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
Hi there,
yay for my first post anyway moving on, i frquent these forums reading what interesting stuff can be done with bash, anyway - lets cut the chase shall we?

The Script

I'll set this script to automaticly run whenever i plug my usb stick into my box
the script will, in turn copy everything from my usb stick into a directory - heres the catch though

the folder will depend on what the date is. So far this is all i have.
In the script i'm trying to define two variables, the first variable defines the first part of what the directory will be called; "USB-BACKUP" (VarUsbdir) now this is the part which gets me:
-
VarDirectory=/home/mikus/$DATE;$VarUsbdir
-
i find it hard to explain what i'm trying to do here, but i'll try my best.

I'm trying to make a variable containing the directory i will later need no mkrdir (can this be done?) do i seperate variables with ; ?

anyway, then i have

mkdir $VarDirectory.

i'm not at home right now so i can't try this but could i please have a few pointers? Sorry if this question is too nubish but i'm new to linux.


Top
 Profile  
 PostPosted: Sun Aug 27, 2006 10:09 pm   

Joined: Mon May 16, 2005 6:29 pm
Posts: 94
Location: Upstate NY
mikus:
1. i might be wrong but: if you are looking for help on a script you are working on this is not the proper place to ask for it. that is what the sandbox is for. if you are looking for someone to create a script for you then it belongs in the requests section. we can move this thread to the proper section once you can clarify what you are looking for.

2. im not sure if you are asking for help with how to do the syncronizing,
if so rsync will be your best bet. this thread might help point you in the right way:

http://ubuntuforums.org/showthread.php?t=26534

3, if you are asking how to use mkdir to create multiple levels then you want to use the "-p" switch (man mkdir for other options). <edit> jbsnake: the original phrase is forbidden here :wink: </edit>

4. as for the variables i'm not sure at all what you are trying to do. i am going to assume that you are trying to use one variable to create the directory with the date command and then copy the contents of the usb device to that directory.

also, there are links in this section and in the community section to the bash/advanced bash scripting guides that might be able to explain things better.

in the future please try to be a bit more clear about what you are requesting and provide as much information and/or relevant code as possible so we can see the bigger picture of what you are trying to do to best be able to help you (or anyone here for that matter)

### THIS IS AN EXAMPLE ###
# this is not how i would normally go about coding something like this
# but it should give you a rough idea on what you are looking for

#!/bin/bash
CURRENT_DATE="$(date +%m.%d.%Y)";
USBDISK="/mnt/usbdisk/";
BACKUPDIR="/home/user/backup/$CURRENT_DATE/";

# check to see if the backup directory already exists
if [ ! -d $BACKUPDIR ];
then
mkdir -p $BACKUPDIR
fi

cp -R $USBDISK $BACKUPDIR

echo "Done";
exit 0

### END EXAMPLE ###


Top
 Profile WWW  
 PostPosted: Sun Aug 27, 2006 10:50 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
Very sorry.

I was looking for help, not for someone to write me the script (just to clarify).


you pretty much guessed what i was trying to do, thanks.


EDIT: when you get a chance can you please explain a bit of whats happening in this script, i think i understand it, but charecters like ! throw me off, don't worry about any of the switches, as i got shown (a while ago) how to use the man command 8)

once again sorry for posting in the wrong section.


Top
 Profile  
 PostPosted: Mon Aug 28, 2006 6:37 am   
User avatar

Joined: Tue May 17, 2005 12:55 am
Posts: 86
Location: Big Easy, Louisiana
Quote:
Moved to the Sandbox


Top
 Profile YIM  
 PostPosted: Mon Aug 28, 2006 7:23 am   

Joined: Mon May 16, 2005 6:29 pm
Posts: 94
Location: Upstate NY
taken from the advanced bash scripting tutorial/howto (http://tldp.org/LDP/abs/html/fto.html just after the 7.3 example)

<snip>
! "not" -- reverses the sense of the tests above (returns true if condition absent).
</snip>

so in the example i gave you i was checking to see if the directory exists before it was created. if it did move on if not then create it.

for more of what you can do with "tests" check out "info test" from a cli. it's similar to a man page. also look at the other reference material i pointed you to in a previous post

btw....welcome to the group


Top
 Profile WWW  
 PostPosted: Mon Aug 28, 2006 8:04 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
but... as we all learned from reading the man page on mkdir... the -p creates the directory *only* if it does *not* exist (and doesn't error if it does exist)
so this:
Code:
if [ ! -d $BACKUPDIR ];
then
mkdir -p $BACKUPDIR
fi

can be changed to this:
Code:
mkdir -p $BACKUPDIR


Top
 Profile  
 PostPosted: Mon Aug 28, 2006 8:46 am   

Joined: Mon May 16, 2005 6:29 pm
Posts: 94
Location: Upstate NY
good point....my bad :)


Top
 Profile WWW  
 PostPosted: Mon Aug 28, 2006 8:12 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
exelent, thanks guys. I'm still very new to the whole linux scene (i'm loving it)

so far this scripting is really good fun to play around with, i like it far more than batch scripting in windows :oops:


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP