Recommended Posts

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/bash

FAVI=/home/shane/freevo/avi
SAMBA=/home/shane/samba
FMOVIES=/home/shane/freevo/movies

# this function checks to see if samba is mounted

check_samba ()
{
mount | grep marsala > /dev/null
if [ "$?" = 0 ]; then echo "samba is mounted"; return 0
else echo "samba is not mounted"; return 1; fi
}

# this function trys to mount samba

mount_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; fi

for i in `ls $FAVI`; do ls $FAVI/$i >> /tmp/list; done
ls $FMOVIES >> /tmp/list

if [ -f /tmp/update ]; then rm /tmp/update; fi
for y in `ls $SAMBA`; do cat /tmp/list | grep $y > /dev/null
       if [ $? != 0 ]; then echo $y >> /tmp/update; fi
done
}

echo "samba needs to be mounted before proceeding"
check_samba
if [ "$?" = 0 ]; then update_list; else mount_samba; check_samba; fi
if [ "$?" = 0 ]; then update_list; else echo "samba failed to mount. exiting"; fi

Edited by shanenin
Link to post
Share on other sites

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

Link to post
Share on other sites

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 function

echo "samba needs to be mounted before proceeding"
check_samba

if [ $? = 0 ]; then update_list; else mount_samba; check samba; fi
case $? in
0) update_list;;
1) echo "samba failed to mount, update incomplete exiting"; exit;;
esac

I have come to understand every script can be done in so many different ways.

Link to post
Share on other sites
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 :)

Link to post
Share on other sites

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/bash

FAVI=/home/shane/freevo/avi
SAMBA=/home/shane/samba
FMOVIES=/home/shane/freevo/movies

if [ `whoami` != root ]; then echo "you must be root, exiting"; exit; fi

check_nfs ()
{
mount | grep "192.168.1" > /dev/null
if [ "$?" = 0 ]; then return 0; else return 1; fi
}


check_samba ()
{
mount | grep marsala > /dev/null
if [ "$?" = 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; fi

for i in `ls $FAVI`; do ls $FAVI/$i >> /tmp/list; done
ls $FMOVIES >> /tmp/list


if [ -f /tmp/update ]; then rm -f /tmp/update; fi
for y in `ls $SAMBA`; do cat /tmp/list | grep $y > /dev/null
       if [ $? != 0 ]; then echo $y >> /tmp/update;fi
done

return 4
}

check_nfs
if [ "$?" != 0 ]; then mount /home/shane/freevo; fi
check_nfs
if [ "$?" != 0 ]; then "echo nfs failed to mount, exiting"; exit; fi

check_samba

if [ $? = 0 ]; then update_list; else mount_samba; check_samba; fi
case $? in
0) 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; fi

echo "if needed, copying new files to freevo file server......"

for i in `sed -n '/\.mp4$/p' /tmp/update`; do cp $SAMBA/$i $FMOVIES; done

for i in `ls $FAVI`
  do cat /tmp/update | grep $i | xargs -i cp $SAMBA/'{}' $FAVI/$i; done


update_list
if [ -f /tmp/update ]; then
echo "#####################################################"
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 by shanenin
Link to post
Share on other sites

my understanding is bash is going to depeciate back ticks for substitutution. How would I change this line to an equivalent not using back ticks

for i in `sed -n '/\.mp4$/p' /tmp/update`; do cp $SAMBA/$i $FMOVIES; done

Link to post
Share on other sites
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'`
$x

Why the difference? No idea.

Link to post
Share on other sites
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.

Link to post
Share on other sites
  • 2 weeks later...
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 os
output = os.popen('command').read()

import commands
output = 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 by jcl
Link to post
Share on other sites

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 by shanenin
Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

so It just uses c headers in its inport statments? I'm confused. .. but I have been known to be wrong..

Link to post
Share on other sites
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 ;)

Link to post
Share on other sites

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.

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.

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.

Loading...