-   Linux - Newbie (
-   -   Trying to create my first script, not doing very well. (

FriedCherry 07-31-2010 05:24 PM

Trying to create my first script, not doing very well.
I am trying to create a script that I thought would be a simple project. Unfortunately it is not happening for me.

I would like to write a script to go through a directory of video files, use mediainfo to grab the height of the video and if it is less than 480 to delete the file.

So as i step through each process, it just seems to be getting to be a much larger job than I thought. Would anyone know where I could find some tutorials on the part of stepping through each file name in a directory and passing it to another app to do something with?

Also if this is way to much for someone who just started... today, then you can let me know that as well.

Thanks in advance

GrapefruiTgirl 07-31-2010 05:40 PM

Hi there, Mark!

One must start somewhere when learning, so your goal is as good as any; and it probably won't be as hard as you think, once you understand how to use a few common tools, like `find` for example, which can recursively search a directory tree. Here's a link to a thread from a few months ago..

The above thread has a little chunk of script which checks the dimensions of a folder full of jpeg images, and resizes them depending on their current dimensions. This code could easily be adapted to suit your intention. It shows usage of `find` in a way that directly applies to your scenario, and demonstrates some basic shell scripting stuff like comparing variables, if/then usage, etc.

Whether you choose to start with that code or not, you'll do well to refer to the manual pages for `find` and `bash` (the shell, assuming you're using bash - if not, please tell us which shell you're using, and what operating system) while putting your script together.

We'll be quite happy to help you work your way through your script as it progresses. You'll be surprised - it won't be too hard.

FriedCherry 07-31-2010 09:11 PM

Thank you so much for your help. I am actually almost done now. I have ran into a slight issue. Some of my movie files are corrupted so when mediainfo is run against them, I get whitespace returned for my variable. So in if statement of my script when I am looking for numbers, I get this:

./ line 6: [: -lt: unary operator expected

So it doesn't delete the movie like I want. Any suggestions?
After I get that taken care of then I am done!

GrapefruiTgirl 07-31-2010 09:16 PM

Probably a good idea to show us the script. Need some sort of test to see if we've got a non-useful result in a variable, and/or an empty variable, before trying to do a numerical comparison. If you paste it in code tags we can have a look!

Code tags help, just in case:

FriedCherry 07-31-2010 09:18 PM

I got it. Ok, so here it is. Don't laugh :)


rm -rf ./*.txt
find . -name '*.wmv' -maxdepth 1 -exec basename "{}" \; > moviesfind.txt
while read MOVIE ; do
        HEIGHT=$(mediainfo "--Inform=Video;%Height%" $MOVIE)
        if [ ${HEIGHT:-0} -lt 480 ]; then
                rm -rf ./$MOVIE
done < moviesfind.txt

To fix that last issue I had, I changed from just the $HEIGHT variable to ${HEIGHT:-0} to give it a number. It worked.

If anyone wants to make this look better or more elegant, I am all for the suggestions. Again, thank you very much for the help!

//EDIT: Yes I am using bash and my OS is .... OS X 10.6.4 *runs and hides*


GrapefruiTgirl 07-31-2010 09:22 PM

Hey, that's a real good idea, the ${HEIGHT:-0} thing! And, you say this is your first script? Pretty darn good in my opinion. Nice work, and no improvement is absolutely necessary - the only thing I *might* do, would be to pipe the output of the find command directly into the loop, eliminating the need for that moviesfind.txt file; but it's up to you (and that sometimes causes weird effects, so if you do that and it doesn't work, don't be surprised - here's an interesting article about what I describe: ) :)

FriedCherry 07-31-2010 09:29 PM

Yes, very first ever script. I am not sure how to do the piping I guess, I thought putting it out to a file and reading it back in would be easier. I will take a look at that article.

You have no idea how much you helped me. This scripting thing is pretty cool. i will be doing more of it thats for sure!

GrapefruiTgirl 07-31-2010 09:32 PM

Happy to help - I enjoy scripting too :) but there's always more to be learned about it, and loads of little details waiting to trip you up :eek: - we're lucky at LQ, as there's usually someone around sooner or later who can help if you get stuck.

PS - One improvement in your script, that I neglected to note last night, is that you probably should "quote" your "$MOVIE" variables as they are read in - it's good practice, so in case of spaces in filenames, this will avoid troubles.

Cheers :)

All times are GMT -5. The time now is 12:09 PM.