LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2010, 12:26 PM   #1
fluxburn
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
Script to copy specific directory based on variable to folder with that variable name


So this is what I want the script to do.

I plug in a usb harddrive, I mount the drive manually.
I want the variable to be a username.
Then the script copies these files after making the directory for the user
mkdir /home/usrbak/username
cp -rfn /media/Disk/Documents\ and\ Settings/username/Favorites /home/usrbak/username
cp -rfn /media/Disk/Documents\ and\ Settings/username/Desktop /home/usrbak/username
cp -rfn /media/Disk/Documents\ and\ Settings/username/My\ Documents/ /home/usrbak/username
cp -rfn /media/Disk/Documents\ and\ Settings/username/Local\ Settings\Application/ Data\Microsoft\Outlook\*.pst /home/usrbak/username

and that's it. So the script really only needs me to answer two questions, #1 what is the name of the mount, and #2 what is the name of the user. So those are variables, anyone have a simple tutorial for a script like this? I even might automate the mounting of the disk as well.


thanks for the reply below =_)

Last edited by fluxburn; 01-06-2010 at 02:49 PM.
 
Old 01-06-2010, 12:47 PM   #2
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
Code:
#!/bin/sh

set -eu # so we fail with unbound variables and any error

mkdir -p /home/usrbak/&1 # it wonn't complain if dir exists

cp -rfn $2/Documents\ and\ Settings/$1/Favorites /home/usrbak/$1
cp -rfn $2/Documents\ and\ Settings/$1/Desktop /home/usrbak/$1
cp -rfn $2/Documents\ and\ Settings/$1/My\ Documents/ /home/usrbak/$1
cp -rfn $2/Documents\ and\ Settings/$1/Local\ Settings\Application/ Data\Microsoft\Outlook\*.pst /home/usrbak/$1
use it like this

script username /media/Disk
 
Old 01-06-2010, 06:18 PM   #3
fluxburn
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
so I run the script ./back username /media/disk/

it errors on the 2nd line, cannot create directory, as it doesn't seem to input variable 1. This is after I removed the -p, which gives a command not found error instead.

Last edited by fluxburn; 01-06-2010 at 06:22 PM.
 
Old 01-06-2010, 06:31 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
The &1 on line 2 should be a $1

Sasha
 
Old 01-07-2010, 02:44 AM   #5
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by GrapefruiTgirl View Post
The &1 on line 2 should be a $1

Sasha
very stupid typo
 
Old 01-07-2010, 05:05 PM   #6
fluxburn
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
I just finished a class up on linux administration and learned about redirection with &1 < \, etc.

So I guess I can do this kind of script with many things. Thanks for the help.
 
Old 01-07-2010, 05:28 PM   #7
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by fluxburn View Post
I just finished a class up on linux administration and learned about redirection with &1 < \, etc.

So I guess I can do this kind of script with many things. Thanks for the help.
shell scripting is not a herd thing to learn: know how to get parameters, know if hen else, know for loop and you're set for long time.
 
Old 01-07-2010, 07:59 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You can easily learn a lot more about bash with Bash Guide for Beginners and with Advanced Bash-Scripting Guide. You'll get a lot of difference even with just a quick glance.

Also remember that 'man bash' is your friend.

Last edited by konsolebox; 01-07-2010 at 08:01 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 10:19 AM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
[SOLVED] Using a directory listing as the argument/variable in a script Twelveone Programming 3 06-23-2008 08:51 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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