A simple Backup script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    All I have to do is write a script that will take two arguments. The first argument is a list which will contain filenames. The second argument is a directory. Basically the idea is that this script will backup all the filenames that are listed in the file from the first argument.
    I talked to my proffessor and looked at my book. Both have stated that A foreach loop is the best way to go about handling this assignment.

So far I can see that the foreach loop is only able to take in one argument. This works fine but I need to add another argument for the user to write so that the data is sent to the directory. Can I give a foreach loop more than one argument???

  1. Relevant commands, code, scripts, algorithms:
    #!/bin/csh

foreach filename (`cat $argv[1])
echo $filename
cat $filename
end

All this does is it takes a list of files, prints out the names of those files and then the content of each file.

  1. The attempts at a solution (include all code and scripts):
    I tried using the set $argv[1] = $< and set $argv[2] = $< to have the user give the arguments right away. But it didnt work. The subscript ws out of range.

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

University of Colorado at Colordo Springs
The Course is: CS209 Programming with Unix
The Proffessor is Pam Carter.

---------- Post updated at 02:21 AM ---------- Previous update was at 02:14 AM ----------

I forgot to mention that all I really need to do once I extract all the files from the filelist is use the mv or the cp command to copy or move them to the directory. It's really not that hard.

---------- Post updated at 04:25 AM ---------- Previous update was at 03:21 AM ----------

Nevermind. I figured it out.

heres the finished code:

#!/bin/csh
foreach filename (`cat $argv[1]`)
cp $filename $argv[2]
end

I didn't realize that $argv[2] can be placed anywhere in the script while still referring to the second argument. Its that whole idea of positional parameters. I was so used to coding with java and other programming languages that use order and where the placement of variables/parameters actually mattered. It took me about 5 hours to figure that out. I am mad and happy at the same time.

In order to avoid file name collisions you will need to recreate the directory structure under argv[2] see dirname $file so the script would look a bit like this.
Iterate over the filenames listed in argv[1]if the named file exists and is readableget the directory name
if there is not a corresponding directory under argv[2][INDENT]make a corresponding directory under argv[2] (check out the -p flag of mkdir)
copy the existing file into this new directory
[/INDENT]And that's it