Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 13th, 2011, 09:32 PM
Kane666 Kane666 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 4 Kane666 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 42 sec
Reputation Power: 0
Shell Script Help

Hey guys anyone know how to make this script?


Ask for a username, and find all files that belong to this user. Copy these files to /tmp/username, where username is replaced by the provided username. Then produce a compressed tar archive of these files, and copy this archive to the username’s home folder. The script should validate that this user’s home folder exists, and act accordingly.

Reply With Quote
  #2  
Old October 14th, 2011, 06:46 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Yep, I do.

Is this home or course work?
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #3  
Old October 14th, 2011, 08:17 AM
Kane666 Kane666 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 4 Kane666 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
Yep, I do.

Is this home or course work?


a little of both a little of it is couse but most is home

Reply With Quote
  #4  
Old October 14th, 2011, 10:46 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Ok, in which case I will try and lead you to the knowledge ...
I suggest you use the man command to look up information about any command used, so if we were to make use of the read command, say, then man read would be the thing to use.

Before we start let it be known that 'throwing around' potentially large amounts of data can cause issues (running out of space, etc.) so should really be accompanied by tests, checks and error handling. That sort of thing will, generally, not be mentioned again by me, so ... beware!

What we will be doing is:

Prompting for the input of a username. Reading the user's input, validating a directory based on that name exists. If it does exist we then go and find all the files that that user owns, copying each one to a temporary directory (potentially making that direcrory first), then creating a tar archive, compressing at the same time, and once complete copy that archive to the user's home directory.

Starting at the top, use the echo command to produce the necessary prompt, then the read command to get the input.
We'd then use the -d conditional to test if the specified user directory exists.

Code:
echo -e "Please enter the user name: \c"
read username
if [ ! -d /home/$username ]
then
  echo "Specified user does not have a home directory"
  exit 2
fi


That deals with the getting of thee user. Now, to copy the files we need to find them:

Code:
mkdir -p /tmp/$username
find / -type f -user $username -exec cp {} /tmp/$username \;


And once that is complete we need to archive and compress ...
Use
Code:
man tar
and see what you come up with.

Reply With Quote
  #5  
Old October 14th, 2011, 06:09 PM
Kane666 Kane666 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 4 Kane666 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 42 sec
Reputation Power: 0
Hey i understand some of it thats good lol ill use the man pages to find out want these commands do thanks man if you want to could you help out with the next part? its cool if you dont you have helped me alot all ready but if you have the time.

Produce a report on screen based on a compressed tar archive that holds the following information:
· The number of files in this compressed tar archive,
· The compression rate (i.e. disk space when compressed divided by diskspace when uncompressed),
· The date and time this archive was constructed. Continuously ask for a file name until the word “compute” is entered. The script will then show, per given file, the number of words in that file, and the percentage of the total number of words of all files together.Create a script called swap that will swap the contents of two given directories. The
script will get the two directories through the script’s parameters. After swapping the files has been completed both directories will include an additional text file that holds:
how many files have been moved out, how many files have been moved in, and which other directory was involved.

Reply With Quote
  #6  
Old October 15th, 2011, 05:42 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Let's see ...
To get the number of files in a given archive you'd need to use the -t option on the tar command to 'test' the archive which will list the contents. Redirecting the output of that will enable you do several of the functions you need - counting the number of files (man wc). If you also use the -v (verbose) option for tar you will get the size of the files inside the archive listed as well, so you could add those up and compare to the total size of the archive (a lot of ways to do that!).
For that last one you will need to construct a loop that prompts for the file name (checking for blank input and the entry of the 'quit' word of compute is entered).
I'd suggest that once the archive to look at/into is identified you do the tar -t on it, redirecting the output to a temporary file so you have the data to hand without needing constantly to 'poke' the archive using tar.
As for the number of words in a given file against the number of words in all files, again we can use wc (look for the -w option), but that would require the archive to be extracted - but you can use the -O option to direct the file output to stdout (which means you can put a pipe in place to deal with counting words, etc.

Swapping directory contents .... gee, a whole lotta fun! There IS a cheat method to doing this. So, if you get extra marks for being 'clever', ponder this (assuming the input directories are called dir1 and dir2):
Code:
mv dir1 dir1.orig
mv dir2 dir1
mv dir1.orig dir2

Not so much swapping the contents as swapping the names which pretty much gives the same effect.
If you have to copy/move the files about, then tar dir1 and dir2 (making sure they both work), then empty dir1 and dir2 and un-tar the dir2 archive into dir1 and vice versa. To do the archives it would be best to make them 'relative' not 'absolute' so:
Code:
cd dir1
tar cf /tmp/dir1.tar .
cd dir2
tar cf /tmp/dir2.tar .
rm -rf *
cd dir1
rm -rf *
tar xf /tmp/dir2.tar
cd dir2
tar xf /tmp/dir1.tar

should do the business. You can then 'mess about' creating the text files that contain the additional details (quite simple - honest!)

Reply With Quote
  #7  
Old October 15th, 2011, 06:52 AM
Kane666 Kane666 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 4 Kane666 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 42 sec
Reputation Power: 0
ok dident know as much with that post lol but ill use the man pages to see still trying to do that produce a report on screen based on a compressed tar archive that holds the following information bit hoply i sould get it soon

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Shell Script Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap