mailin 0 #1 October 5, 2004 Then please help me! I missed class last week - and the book (Teach yerself UNIX in 24 hours ) 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! JenArianna Frances Quote Share this post Link to post Share on other sites
kaerock 1 #2 October 5, 2004 mkdir ~/backup cp myls ~/backup That'll be $2, please. You be the king and I'll overthrow your government. --KRS-ONE Quote Share this post Link to post Share on other sites
AggieDave 6 #3 October 5, 2004 Quote mkdir ~/backup cp myls ~/backup That'll be $2, please. For a Unix guy, that's more like $40.--"When I die, may I be surrounded by scattered chrome and burning gasoline." Quote Share this post Link to post Share on other sites
kaerock 1 #4 October 5, 2004 skydiver rate :> You be the king and I'll overthrow your government. --KRS-ONE Quote Share this post Link to post Share on other sites
mailin 0 #5 October 5, 2004 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 JenArianna Frances Quote Share this post Link to post Share on other sites
CrazyIvan 0 #6 October 5, 2004 QuoteQuote mkdir ~/backup cp myls ~/backup That'll be $2, please. For a Unix guy, that's more like $40. 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. Quote Share this post Link to post Share on other sites
indyz 1 #7 October 5, 2004 QuoteQuote mkdir ~/backup cp myls ~/backup That'll be $2, please. For a Unix guy, that's more like $40. 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). Quote Share this post Link to post Share on other sites
indyz 1 #8 October 5, 2004 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. Quote Share this post Link to post Share on other sites
kaerock 1 #9 October 5, 2004 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 Quotebut 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 Quote Share this post Link to post Share on other sites
kaerock 1 #10 October 5, 2004 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 QuoteOk, 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 Quote Share this post Link to post Share on other sites
indyz 1 #11 October 5, 2004 QuoteThis 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. Quote Share this post Link to post Share on other sites
flyingferret 0 #12 October 5, 2004 If I remember anything about school, the example is probably 'wrong' for those of us in the working world. However, pointing that out never makes the teacher very happy.-- All the flaming and trolls of wreck dot with a pretty GUI. Quote Share this post Link to post Share on other sites
mailin 0 #13 October 5, 2004 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? JenArianna Frances Quote Share this post Link to post Share on other sites
AggieDave 6 #14 October 5, 2004 QuoteHowever, 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." Quote Share this post Link to post Share on other sites
mailin 0 #15 October 5, 2004 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.... JenArianna Frances Quote Share this post Link to post Share on other sites
AggieDave 6 #16 October 5, 2004 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." Quote Share this post Link to post Share on other sites
kaerock 1 #17 October 5, 2004 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 QuoteYes, 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 Quote Share this post Link to post Share on other sites
indyz 1 #18 October 5, 2004 Quoteshould I be using an alias to rename the cp command according to the example that he used? That's another way to do it. alias mycopy="cp $* ~/backup" Quote Share this post Link to post Share on other sites
flyingferret 0 #19 October 5, 2004 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. QuoteA 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. Quote Share this post Link to post Share on other sites
Kris 0 #20 October 5, 2004 My name is Inigo Montoya, you Kill -9 my process, prepare to Vi. The script posted right above this post is the bomb-diggity. Sky, Muff Bro, Rodriguez Bro, and Bastion of Purity and Innocence!™ Quote Share this post Link to post Share on other sites
mailin 0 #21 October 5, 2004 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 Quote Share this post Link to post Share on other sites
mailin 0 #22 October 5, 2004 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? JenArianna Frances Quote Share this post Link to post Share on other sites
indyz 1 #23 October 5, 2004 you want ~/backup (which expands automagically to /home/youusername/backup), not /backup (which is the directory backup in the root of the filesystem, which you don't have write access to) Quote Share this post Link to post Share on other sites
mailin 0 #24 October 5, 2004 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 Arianna Frances Quote Share this post Link to post Share on other sites
flyingferret 0 #25 October 5, 2004 Do you have to alias? Or just call the script?-- All the flaming and trolls of wreck dot with a pretty GUI. Quote Share this post Link to post Share on other sites