0
mailin

Are you a UNIX guru?

Recommended Posts

I don't know exactly...

it seems to me, in the example he gave that he was creating an alias (mycopy) for the cp command, so figuring I should do the same...

but its not working... of course not, I have 10 min. to submit it in, so of course its not working :P

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
actually, that alias doesn't work. :$ check your backup directory. I bet it's empty. I forgot that bash aliases don't take arguments. :$ still stuck on csh-land, I guess.

I'd just write the script. something like:

#!/bin/bash
cp $@ ~/backup

should do it.

Share this post


Link to post
Share on other sites
oh boy - this is making me laugh! :D

$ ls backup

myls
$ alias copy="$@ ~/backup"
$ ls
backup big.ouput big.output buckaroo demo myls project whoison
$ copy demo
-bash: /home/jdece/backup: is a directory
$ ls backup
myls
$

Arianna Frances

Share this post


Link to post
Share on other sites
Not in the wrong direction at all!

I wish I could use it, but I'd get zero points for using someting that hasn't been covered yet.

I just need to get this part to work....

even my JAVA and C++ classes this semester have been easier than this UNIX class :S

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Thanks tons for your help Brian - and everyone.

I have to pass in the assignment now.... but I was still getting those errors....

very interesting how it all works... but I'm looking forward to my 'last class' tonight :D

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
owww, ok. That sucks. Us 'geeks' in school used to try and outcode each other in class. Writing elegant or better yet obfusticated code always got discussion if not extra points.
But an alias should be easy enough, it sounds like you got it. Maybe in the future you will get the 'fun' elegant type stuff. I even aced a final one time, then the prof called me up and said "Show me, how this works" I explained what it did, basically a loophole way to manipulate a linked list without screwing with pointers. He wanted to be sure I knew what I was doing. Then said, "Well, I have never seen anyone do it that way, but it meets the technical requirements, here's your A."
But he was a great prof anyway, worked for years in the oil industry and then for TI. I was one that taught cause he liked it. We had great arguments. And that was at community college.

Anyway, I hope you get that experience. I am sorry about the Java thing though....ick.
--
All the flaming and trolls of wreck dot with a pretty GUI.

Share this post


Link to post
Share on other sites
  Quote

I am sorry about the Java thing though....ick.



LOL! Don't be! I'm totally loving my Java and C++ classes! They rock!

PS. I'm taking some of these classes at a community college too - learning more there than at the university! That 'real world' experience makes a HUGE difference!


Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Well, congrats on liking Java, it might have been my prof/textbook, but I hated it. Of course I was 'weened' on C and Turbo Pascal, then on to C++ Java always seemed like a web language to me...more security features (annoyances) than I wanted. Sometimes OOP is great, other times, good ole quick C is.
I cannot imagine taking a *nix admin class in a cram format. My *nix was Op. Systems II, and the final was 200 question multi-multi choice, like a-e, then aa-ee.
Hope you did okay on the project. ;)
--
All the flaming and trolls of wreck dot with a pretty GUI.

Share this post


Link to post
Share on other sites
  Quote

Create a shell script that will copy a file (named myls) to a backup directory located in your home directory.






spectre:~ matthewcrocker$ cat - > myscript
#!/bin/bash

(mkdir -p ~/backup) && (cp myls ~/backup/myls)
spectre:~ matthewcrocker$ chmod 700 myscript



The && stuff makes sure the cp only runs if the mkdir command completed successfully

Share this post


Link to post
Share on other sites
I'm sure this is too late, but I think I might be able to help.

So far no one appears to have been able to work out the nature of the problem you were set and have given answers that reflect exactly that.

1) create a shell script that will copy things to a backup directory

2) in the example given the format was [script name] [file 1] [file2].

To those that thought this was a badly constructed example (kaerock) go back to reading your unix text books.

This is not an Alias.

To decipher the problem into legible english.

Create a shell script that will take file(s) named on the command line and copy them to a back up folder.

So, in the above example file1 and file2 should create file1copy file2copy in the backup folder.

Ferret, it looks like your code came from The Happy Hacker, plus it doesn't actually solve the task at hand. Your script simply archives stuff put into a specified folder.

read this: http://www.tldp.org/LDP/abs/html/

Don't worry about the Advanced part of it's name, it'll take you through everything. If you're already studying other languages you already have a basic understanding of topics like variables and flow control.

Especially check out the the following chapters:
http://www.tldp.org/LDP/abs/html/sha-bang.html
http://www.tldp.org/LDP/abs/html/othertypesv.html

If you're covering other languages just consider the problem programmatically. Break down things into tasks. Example: you know you need to accept multiple arguments on the command line, how would you collect them and use them? read man cp online, or on the system you're using if you can.

When I'm writing scripts I try to stay away from the computer until I understand the problem space properly. then I write everything out as a list in plain english. Once you have defined the problem correctly and the steps you need to take to solve the problem you can start programming, by translating what you wrote into plain english into the syntax the tools require. Remember, computers are really stupid and you have to tell them everything. Writing everything out beforehand can help you pinpoint the tasks that you'd take for granted but you must explicitly tell the computer.

Problem: run a backup of files listed on the command line to a backup directory.

Questions?
1. what can i use to copy files?
2. what syntax does the tool use? (for scripting)
3. what will the backup folder be called?
4. where will it be?
5. what does the script require before it's run?


Solution?
0. what does the script require before it's run.
0a. The script should set up the environment variables, a backup folder, she-bang line etc.
1. when called capture the file names (variables)
2. create a loop to iterate through the list of file names 3. For each filename that goes through the loop copy the file to the backup directory.
3. exit program

These are the very basic steps required for the program. It'll be quick and dirty, you'll have to rely on your environment (whatever that is) to provide feedback - bash, luckily, is pretty good with feedback (even if its somewhat confusing and cryptic at first).

Improvements to the script would be:
1. check the files called on are actually existing
2. automatically handle different file types (directories or files)
3. interactivity (asking the user if they're sure).
4. allowing for multiple copies of the same file (to track revisions)
5. better error handling.


Hope some of this helps.

TV's got them images, TV's got them all, nothing's shocking.

Share this post


Link to post
Share on other sites
Yeah, I never claimed the code as mine, hence the quotes. Not sure who happy hacker is, but this came from linuxplanet and was written by a Carla Schroder. As I mentioned, I did not think alias was the way to go, which is why I posted a script. Granted it was a show off script, but still, it did what was asked. Nothing more was specified. When I posted it, I thought the entire writeup would give an understanding, and then the core elements could be pulled out.

  Quote

Create a shell script that will copy a file (named myls) to a backup directory located in your home directory.



That was the original problem statement, it contained nothing about syntax or arguments.

Now that being said, I agree with your methods, but we are both looking back on it with our current knowledge. With a problem description that loose, the only thing for certain was a shell script and to copy a file. The rest is extrapolation, period.

I am familiar with tldp, but would have been like handing someone a dictionary, especially when pressed for time. She had minutes to do this and was just looking for something that worked.

In the end the student felt it was about an alias, so she went with that. Which is honestly probalby just as safe as a bunch of us 5 years down the road giving outside help.

So, your logic is assumptive and based on things outside the problem statement. Additionally the material is way too much to chew write now. Even the sample scripts were a bit much based on the class pace.

Now, I would not have typed any of the above had it not been for the statement
  Quote

So far no one appears to have been able to work out the nature of the problem you were set and have given answers that reflect exactly that


I think we/I assessed it pretty well and did so before she had to turn it in. Further, I insist that the code I posted meets the guideline and then some including folder integrity. In any case maybe your post can provide insight with future work.
--
All the flaming and trolls of wreck dot with a pretty GUI.

Share this post


Link to post
Share on other sites
Ok, so I'm getting partial credit on that one question because I did use the correct syntax for copy: cp $* $HOME/backup.

The final, piece of cake! If I end up at a place that actually uses UNIX then I'll probably take the rest of the course.

Thanks for everyones help!

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Guest
  Quote

  Quote

My name is Inigo Montoya, you Kill -9 my process, prepare to Vi. :P



Groan :S



hehehe LOL :D

mh

.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0