Trying to create my first script, not doing very well.
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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!
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!
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 09:24 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!
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 07:42 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.