#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    4
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,439
    Rep Power
    1688
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,439
    Rep Power
    1688
    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.
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    4
    Rep 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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,439
    Rep Power
    1688
    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!)
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    4
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo