LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-31-2010, 06:24 PM   #1
FriedCherry
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Rep: Reputation: 0
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
Mark
 
Old 07-31-2010, 06:40 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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..

http://www.linuxquestions.org/questi...s-pngs-796781/

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.
 
Old 07-31-2010, 10:11 PM   #3
FriedCherry
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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:


./small.sh: 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!
 
Old 07-31-2010, 10:16 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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: http://www.phpbb.com/community/faq.php?mode=bbcode#f2r1
 
Old 07-31-2010, 10:18 PM   #5
FriedCherry
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I got it. Ok, so here it is. Don't laugh

Code:
#!/bin/bash
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
	fi
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*

Mark

Last edited by FriedCherry; 07-31-2010 at 10:20 PM.
 
Old 07-31-2010, 10:22 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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: http://mywiki.wooledge.org/BashFAQ/024 )

Last edited by GrapefruiTgirl; 07-31-2010 at 10:24 PM.
 
Old 07-31-2010, 10:29 PM   #7
FriedCherry
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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!
 
Old 07-31-2010, 10:32 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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 - 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

Last edited by GrapefruiTgirl; 08-01-2010 at 08:42 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 03:40 PM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 09:15 PM
How to create a Create script "easier"? lorio Linux - Newbie 6 10-10-2006 07:05 PM
Need help to create a script barneyt Programming 2 08-18-2006 05:11 PM
How do I create a script? ACURA TL-S Linux - Newbie 1 08-03-2003 10:58 PM


All times are GMT -5. The time now is 09:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration