0
mailin

Are you a UNIX guru?

Recommended Posts

Then please help me!

I missed class last week - and the book (Teach yerself UNIX in 24 hours :S) is confusing me.

I have 1 more question on my homework assignment but I have to study for the test tonight too and I'm feeling very overwhelmed.

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

I feel so stupid that I can't figure this out [:/]

Thanks for any and all help!

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
but is that a script? the example my teacher gave in notes (as bare minimum as they are) have something a bit different

if it was just a matter of creating the directory and copying the file... well crap, thats the easy part

EDIT TO ADD MY TEACHERS EXAMPLE:

Example:

mycopy letter memo

The above would copy the files letter and memo to /home/rdefe/backup


Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Quote

Quote

mkdir ~/backup
cp myls ~/backup


That'll be $2, please.



For a Unix guy, that's more like $40.:P



In the I.T field nowadays, $2 is being OVERPAID :(, a guy in India can do the same for 25 cents [:/]
__________________________________________
Blue Skies and May the Force be with you.

Share this post


Link to post
Share on other sites
Quote

Quote

mkdir ~/backup
cp myls ~/backup


That'll be $2, please.



For a Unix guy, that's more like $40.:P


I was gonna say $175/hr minimum two hours.

Make sure you put the proper shebang at the top of the file ("#!/bin/sh" or whatever).

Share this post


Link to post
Share on other sites
Actually, that example is terribly wrong.

If you look at the manpage for 'cp', you'd see that the last argument in the argument list would be the destination. That example would copy 'letter' to 'memo', assuming 'mycopy' worked like 'cp'.

To make what I posted a script, you put the text in a file, then run 'chmod 700 ' and you run the file. "#!/bin/sh" is probably not required in this case, although it is good pratice to include it at the beginning of the file.

-R

Quote

but is that a script? the example my teacher gave in notes (as bare minimum as they are) have something a bit different

if it was just a matter of creating the directory and copying the file... well crap, thats the easy part

EDIT TO ADD MY TEACHERS EXAMPLE:

Example:

mycopy letter memo

The above would copy the files letter and memo to /home/rdefe/backup


Jen



You be the king and I'll overthrow your government. --KRS-ONE

Share this post


Link to post
Share on other sites
This assumes bash is on the machine and also located in the ''/bin" directory, this is not portable. Hard-coding the location of the home directory is also dangerous, using "~" or "${HOME}" is preferred as it is portable.

-R
Quote

Ok, try this:



#!/bin/bash

mkdir -p /home/rdefe/backup
cp $* /home/rdefe/backup


call the script mycopy and do a "chmod +x" on it.



You be the king and I'll overthrow your government. --KRS-ONE

Share this post


Link to post
Share on other sites
Quote

This assumes bash is on the machine and also located in the ''/bin" directory, this is not portable. Hard-coding the location of the home directory is also dangerous, using "~" or "${HOME}" is preferred as it is portable.



She specified the directory. And I was trying to keep things simple and my assumptions about bash will be correct on 95% of modern unix machines.

Share this post


Link to post
Share on other sites
Yes, i using bash....

but I'm still horribly confused...

I hate this book.... I was able to copy the file to the newly created backup directory... but I did it using the cp command

should I be using an alias to rename the cp command according to the example that he used?

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Quote

However, pointing that out never makes the teacher very happy



Most computer science types are teachers because they weren't good enough to make it as a real IT type...or they just weren't good enough programmers, and there's a rare, very rare type that got tired of working the real world and decided to teach.

So basically a lot of the teachers don't know fuckall beyond what the book says and what they want to see from their students. They don't know real world application.
--"When I die, may I be surrounded by scattered chrome and burning gasoline."

Share this post


Link to post
Share on other sites
Unfortunately, this is not the case with mine... (wish it was)

He works as a unix system admin for Fleet Bank (now Bank of America) during the day and teaches this class one night a week (just as he has for 9 years now).

Sometimes I think he takes for granted that we're STUDENTS who don't know jack sh!t about this. I really hope my first job doesn't involve UNIX....

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Quote

Unfortunately, this is not the case with mine... (wish it was)

He works as a unix system admin for Fleet Bank (now Bank of America) during the day and teaches this class one night a week (just as he has for 9 years now).

Sometimes I think he takes for granted that we're STUDENTS who don't know jack sh!t about this. I really hope my first job doesn't involve UNIX....



You're VERY VERY lucky. Use that guy's knowledge and experience and learn as much as you can from it. That'll help you out a lot in the long run.

Oh, and you should be motivated to learn UNIX, it kicks the shit out of everything that MS ever tried to make.
--"When I die, may I be surrounded by scattered chrome and burning gasoline."

Share this post


Link to post
Share on other sites
I'm guessing he wanted you to figure that you need to use 'cp'. That's the command for the job, I can't see why he'd want to obfusicate it (any more than it already is) by making you alias common commands.

-R

Quote

Yes, i using bash....

but I'm still horribly confused...

I hate this book.... I was able to copy the file to the newly created backup directory... but I did it using the cp command

should I be using an alias to rename the cp command according to the example that he used?

Jen



You be the king and I'll overthrow your government. --KRS-ONE

Share this post


Link to post
Share on other sites
Agreed, with Dave, you are very lucky, i only had a few profs like that.
Your first job might not include unix, because some sysadmin is writing scripts to do it :)
You wanna impress him, use this. All nicely commented and everything. It will impress him. BUT you better be able to explain it if he asks. On the other hand you could just use the core stuff and chunk the rest.

Quote

A Simple Backup Script

This script demonstrates a number of common ways to use Bash. A slick Bash trick is to take a script like this, and execute it one line at a time. That's a great way to build and test a script.

To run it as a script, save it as text file, with a nice name like "backup," and make it executable:

$ chmod +x backup

There are severals ways to run it. Change to the directory it's in, and use one of these:

$ ./backup
$ bash backup

Or, do what leet geeks do, and put it in /usr/bin. Or keep it all to yourself- create your own personal scripts directory, and put it in your path:

$ mkdir ~/scripts

Append to your PATH, using your own filepath, of course.

/home/carla/scripts

This allows you to run the script like any other command:

$ backup

And now, the script itself:

#!/bin/bash
## This is the famous "sha-bang" statement,
## that tells the shell which command
## interpreter to use. See man magic
## to learn the inner workings of sha-bang.
## this script uses the tar, mkdir, and cp commands
## plus Bash's conditional statements
## smart admins write lots of comments

# test for existing backup directory
# if it does not exist, create it
# -e is the Bash way of asking "does this file exist"
# $ means variable substitution. HOME is a builtin
# Bash variable.
if [ -e $HOME/1backups ]
then
echo "The backup directory exists."
else
echo "A backup directory does not exist, and will be created."
mkdir $HOME/1backups
echo "A backup directory has been created"
fi

# now we define our own variables
# location of files to backup
FILES=$HOME/archive-files
# name of compressed archive
ARCHIVENAME=backups.tgz
# location of backup directory
BACKUPDIR=/archive/carla/
# create compressed archive, copy to backup directory
tar czf $ARCHIVENAME $FILES
cp -ap $ARCHIVENAME $BACKUPDIR

if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi

Going through this script and understanding which parts are Bash commands, and which are Linux commands, is important for understanding how to put scripts together.


--
All the flaming and trolls of wreck dot with a pretty GUI.

Share this post


Link to post
Share on other sites
Thanks for all your help guys. I realize I should probably feel more fortunate with this class... but what I feel is overwhelmed.

This was a UNIX Admin course that was crammed into 5 weeks - its a 3 part course that will next include programming for UNIX (which I thankfully don't have to take). As a programming major I should be enjoying it, but I'm feeling confused.... I'll eventually get it, just not in this class.

The error I'm getting when creating the alias is such:


$ cd
$ ls
backup big.ouput big.output buckaroo demo myls project whoison
$ cd /home
$ ls
aevan gclar nfakh os18 os27 os36 os45 os54 os63 os72 os9
aferr gwolc os1 os19 os28 os37 os46 os55 os64 os73 pjean
balbr hmuhl os10 os2 os29 os38 os47 os56 os65 os74 psoum
bgrah jdece os11 os20 os3 os39 os48 os57 os66 os75 rdefe
cmarq jogid os12 os21 os30 os4 os49 os58 os67 os76 rfont
cmore jvinc os13 os22 os31 os40 os5 os59 os68 os77 rguin
dkrus kchho os14 os23 os32 os41 os50 os6 os69 os78 style
donp ljack os15 os24 os33 os42 os51 os60 os7 os79 svinc
egonz mflah os16 os25 os34 os43 os52 os61 os70 os8 wpass
ffont nbroo os17 os26 os35 os44 os53 os62 os71 os80 wprice
$ cd /home/jdece
$ ls
backup big.ouput big.output buckaroo demo myls project whoison
$ unalias mycopy
$ alias mycopy="cp $* /backup"
$ mycopy demo
cp: cannot stat `/backup': No such file or directory
$ ls
backup big.ouput big.output buckaroo demo myls project whoison
$ unalias mycopy
$ ls -l
total 28
drwxrwxr-x 2 jdece jdece 4096 Oct 5 14:20 backup
-rw-rw-r-- 1 jdece jdece 31 Oct 5 11:48 big.ouput
-rw-rw-r-- 1 jdece jdece 251 Oct 5 10:58 big.output
-rw-rw-r-- 1 jdece jdece 279 Oct 5 11:56 buckaroo
-rw-rw-r-- 1 jdece jdece 198 Oct 5 10:55 demo
-rw-rw-r-- 1 jdece jdece 0 Oct 5 13:07 myls
drwxrwxr-x 6 jdece jdece 4096 Sep 28 15:05 project
-rw-rw-r-- 1 jdece jdece 185 Sep 28 14:44 whoison
$ ls -f
. .bash_profile .bash_history .viminfo myls backup
.. .bashrc project demo buckaroo
.bash_logout .emacs whoison big.output big.ouput
$ ls -F
backup/ big.ouput big.output buckaroo demo myls project/ whoison
$ alias mycopy="cp $* backup"
$ mycopy demo
cp: omitting directory `backup'
$


What am I doing wrong that alias is not working? [:/]
Arianna Frances

Share this post


Link to post
Share on other sites
I know it will work, and do it well....

but we haven't gone over if/then/else statements in this class yet


How come when your learning something they show you the way you SHOULDN"T do something before they show you the way you should, or the way that is best?

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
makes sense - thanks!



$ alias mycopy="cp $* ~/backup"
$ ls
myls
$ cd /home/jdece
$ ls
backup big.ouput big.output buckaroo demo myls project whoison
$ mycopy demo
cp: omitting directory `/home/jdece/backup'


Still getting that stupid error tho :S
Arianna Frances

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