LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to copy specific directory based on variable to folder with that variable name (https://www.linuxquestions.org/questions/programming-9/script-to-copy-specific-directory-based-on-variable-to-folder-with-that-variable-name-780327/)

fluxburn 01-06-2010 12:26 PM

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 =_)

ozanbaba 01-06-2010 12:47 PM

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

fluxburn 01-06-2010 06:18 PM

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.

GrapefruiTgirl 01-06-2010 06:31 PM

The &1 on line 2 should be a $1

Sasha

ozanbaba 01-07-2010 02:44 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3816887)
The &1 on line 2 should be a $1

Sasha

very stupid typo

fluxburn 01-07-2010 05:05 PM

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.

ozanbaba 01-07-2010 05:28 PM

Quote:

Originally Posted by fluxburn (Post 3818129)
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.

konsolebox 01-07-2010 07:59 PM

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.


All times are GMT -5. The time now is 04:10 PM.