shanenin Posted May 17, 2005 Report Share Posted May 17, 2005 (edited) I have been wanting to write a a script that would copy media files off of my windows computer to my linux media center. I currently convert my dvds to mpeg 4 using nero-recode. I also download all of my tv shows to the windows computer, I choose the windows computer becaise it is left on more often. The reason I needed a script was do to my human error. I was ersaing video files before getting the transered over to my linux media center(thinking I already transfered them)I got my script half way done. I got it to the point of mounting samba and checking to see if I have new files to be transfered, then it outputs those files to a textfile. Now I need to decide if I want to try and automate the copying process. If I had to copy them to the same place it would be easy, but each tv show needs to be copyied to a different directoy. I may try to fully automate it, or make it interactive. I am haveing such a blast making this script work. I could not even sleep last night because of the excitement of coding it. For a real programmer this is obviosly very juvenile, but for me it was quite an accomplishment.If anyone wants to look at my pride and joy here it is #!/bin/bashFAVI=/home/shane/freevo/aviSAMBA=/home/shane/sambaFMOVIES=/home/shane/freevo/movies# this function checks to see if samba is mountedcheck_samba (){mount | grep marsala > /dev/nullif [ "$?" = 0 ]; then echo "samba is mounted"; return 0else echo "samba is not mounted"; return 1; fi}# this function trys to mount sambamount_samba (){echo "mounting samba"; mount /home/shane/samba}update_list (){echo "checking for new video files to be transfered"if [ -f /tmp/list ]; then rm /tmp/list; fifor i in `ls $FAVI`; do ls $FAVI/$i >> /tmp/list; donels $FMOVIES >> /tmp/listif [ -f /tmp/update ]; then rm /tmp/update; fifor y in `ls $SAMBA`; do cat /tmp/list | grep $y > /dev/null if [ $? != 0 ]; then echo $y >> /tmp/update; fidone}echo "samba needs to be mounted before proceeding"check_sambaif [ "$?" = 0 ]; then update_list; else mount_samba; check_samba; fiif [ "$?" = 0 ]; then update_list; else echo "samba failed to mount. exiting"; fi Edited May 18, 2005 by shanenin Quote Link to post Share on other sites
shanenin Posted May 17, 2005 Author Report Share Posted May 17, 2005 I guess it is not working totally correct. If samba is mounted before running the script it runs the update_list function twice. If I start the script without samba mounted I seems to run it correctly Quote Link to post Share on other sites
shanenin Posted May 19, 2005 Author Report Share Posted May 19, 2005 I fixed it with the use of a case statement. I changed the last part of my script to look like this. I also need to add a return 4 to my update_list functionecho "samba needs to be mounted before proceeding"check_sambaif [ $? = 0 ]; then update_list; else mount_samba; check samba; ficase $? in0) update_list;;1) echo "samba failed to mount, update incomplete exiting"; exit;;esacI have come to understand every script can be done in so many different ways. Quote Link to post Share on other sites
iccaros Posted May 19, 2005 Report Share Posted May 19, 2005 I have come to understand every script can be done in so many different ways.now that you discovered the secrect.. you can never tell Quote Link to post Share on other sites
shanenin Posted May 20, 2005 Author Report Share Posted May 20, 2005 every time I try to "fix something", I manage to create some other problem, it seems never ending. Quote Link to post Share on other sites
shanenin Posted May 21, 2005 Author Report Share Posted May 21, 2005 (edited) I think i got my script working well(those bugs will probably appear) It now sorts all of my different tv shows and movies and copies them to the correct destination directory. The only requiremant is my tv shows must be named similar to their directory. Example: the tv show "king.of.queens.704.avi" must match with the directory name "king.of.queens". Any media file that the script can't find where to copy it to is echoed to the shell so I can manually move it. Here is my final script#!/bin/bashFAVI=/home/shane/freevo/aviSAMBA=/home/shane/sambaFMOVIES=/home/shane/freevo/moviesif [ `whoami` != root ]; then echo "you must be root, exiting"; exit; ficheck_nfs (){mount | grep "192.168.1" > /dev/nullif [ "$?" = 0 ]; then return 0; else return 1; fi}check_samba (){mount | grep marsala > /dev/nullif [ "$?" = 0 ]; then return 0; else return 1; fi}mount_samba (){mount -t smbfs //marsala/n3 -o credentials=/etc/credentials,rw /home/shane/samba}update_list (){if [ -f /tmp/list ]; then rm -f /tmp/list; fifor i in `ls $FAVI`; do ls $FAVI/$i >> /tmp/list; donels $FMOVIES >> /tmp/listif [ -f /tmp/update ]; then rm -f /tmp/update; fifor y in `ls $SAMBA`; do cat /tmp/list | grep $y > /dev/null if [ $? != 0 ]; then echo $y >> /tmp/update;fidonereturn 4}check_nfsif [ "$?" != 0 ]; then mount /home/shane/freevo; ficheck_nfsif [ "$?" != 0 ]; then "echo nfs failed to mount, exiting"; exit; ficheck_sambaif [ $? = 0 ]; then update_list; else mount_samba; check_samba; ficase $? in0) update_list;;1) echo "samba failed to mount, update incomplete exiting"; exit;;esac[ -f /tmp/update ]if [ "$?" != 0 ]; then echo "new new files were found"; exit; fiecho "if needed, copying new files to freevo file server......"for i in `sed -n '/\.mp4$/p' /tmp/update`; do cp $SAMBA/$i $FMOVIES; donefor i in `ls $FAVI` do cat /tmp/update | grep $i | xargs -i cp $SAMBA/'{}' $FAVI/$i; doneupdate_listif [ -f /tmp/update ]; thenecho "#####################################################"echo "the following files could not automatically be sorted"echo "and copied over to the freevo server."echo "#####################################################"cat /tmp/update; else echo "copy successful"; fi Edited June 2, 2005 by shanenin Quote Link to post Share on other sites
shanenin Posted May 22, 2005 Author Report Share Posted May 22, 2005 my understanding is bash is going to depeciate back ticks for substitutution. How would I change this line to an equivalent not using back ticksfor i in `sed -n '/\.mp4$/p' /tmp/update`; do cp $SAMBA/$i $FMOVIES; done Quote Link to post Share on other sites
jcl Posted May 22, 2005 Report Share Posted May 22, 2005 for i in $(sed -n '/\.mp4$/p' /tmp/update); do cp $SAMBA/$i $FMOVIES; done Quote Link to post Share on other sites
shanenin Posted May 22, 2005 Author Report Share Posted May 22, 2005 that seems easy enough :-)are back ticks used for command substitution in other languages? do you know why bash is depreciating them? Quote Link to post Share on other sites
jcl Posted May 22, 2005 Report Share Posted May 22, 2005 are back ticks used for command substitution in other languages?Perl and Ruby. Probably others.do you know why bash is depreciating them?POSIX recommends against using them because they subtly and needlessly change the semantics of the shell language. For example,~ $ echo '\$x' \$x~ $ echo $(echo '\$x')\$x~ $ echo `echo '\$x'`$xWhy the difference? No idea. Quote Link to post Share on other sites
shanenin Posted May 22, 2005 Author Report Share Posted May 22, 2005 interesting. Giving that example, would it be a good habit not to use back ticks, is there any reason I should keep using them? Quote Link to post Share on other sites
jcl Posted May 22, 2005 Report Share Posted May 22, 2005 interesting. Giving that example, would it be a good habit not to use back ticks, is there any reason I should keep using them?It doesn't really matter as long as you're aware of the potential for problems. AFAICT most people still use backticks, at least when they're interacting with the shell. Two keystrokes and broken semantics beats three shifted keystrokes and correct semantics. Quote Link to post Share on other sites
shanenin Posted June 2, 2005 Author Report Share Posted June 2, 2005 lets say I wanted to do this same scripts using python, which I know absolutely nothing about. Am I able to use the same gnu programs like: cp, sed, mount? Quote Link to post Share on other sites
jcl Posted June 2, 2005 Report Share Posted June 2, 2005 (edited) lets say I wanted to do this same scripts using python, which I know absolutely nothing about. Am I able to use the same gnu programs like: cp, sed, mount?Yeah. I have no idea how, but I know you can.Edit: It seems that these both provide the equivalent of command substitution:import osoutput = os.popen('command').read()import commandsoutput = commands.getoutput('command')Except for mounting, everything in your script could be written in Python, so there wouldn't really be much need for this feature. Edited June 3, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted June 3, 2005 Author Report Share Posted June 3, 2005 (edited) I just ordered a python "for absoulute beginners" book. I need to start from the very beginning. Other then my very small amount of bash scripting I do not really know much of anything about programming. I can't wait until it comes. Edited June 3, 2005 by shanenin Quote Link to post Share on other sites
iccaros Posted June 3, 2005 Report Share Posted June 3, 2005 at the python web site is a compleate beginners tutorial, and seveal pdf books. I have a few Python books but ended up using the web site more. Quote Link to post Share on other sites
shanenin Posted June 4, 2005 Author Report Share Posted June 4, 2005 you are probably right, that all the info I need is available online, but a book feels nice; I like to get a way from the computer when I can. Quote Link to post Share on other sites
iccaros Posted June 7, 2005 Report Share Posted June 7, 2005 I also recomend picking up a book on C.. (not c++ unless you also what ot learn that) as Python is kind of a scripting version of C.you will find that you can probaly learn two languages at the same time since most of the same code will run on both. like I am doing with C++ and javascript. Quote Link to post Share on other sites
jcl Posted June 7, 2005 Report Share Posted June 7, 2005 Erk. C and Python are miles apart, syntactically and semantically. No non-trivial code can shared between them. Quote Link to post Share on other sites
iccaros Posted June 9, 2005 Report Share Posted June 9, 2005 so It just uses c headers in its inport statments? I'm confused. .. but I have been known to be wrong.. Quote Link to post Share on other sites
jcl Posted June 9, 2005 Report Share Posted June 9, 2005 so It just uses c headers in its inport statments? I'm confused. .. but I have been known to be wrong..I don't use Python, but I've never seen any sign of C outside of the runtime and native modules. It may well have some kind of header-based automagic foreign-function interface capability. But so do some implementation of Common Lisp, and if you compare CL to C in public you'll want to keep your asbestos coveralls handy Quote Link to post Share on other sites
shanenin Posted June 10, 2005 Author Report Share Posted June 10, 2005 I just got my book; so far I am really impressed. The syntax feels very natural. The first two practice programs I have written were done without syntax errors, I did not even have to reference the book. This is out of the first chapter, so it is not complex. I remember trying to write my first practice C program, I spent the majority of the time finding syntax errors. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.