Register
It is currently Sat Oct 25, 2014 11:29 am

if statement for script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Fri Sep 14, 2012 8:59 am   

Joined: Fri Sep 14, 2012 8:52 am
Posts: 8
Its about my second week in bash. It has proven hard to find tutorials or quality material. When searching for a forum I ran across this site so maybe I can see how it goes here B-)

I am using a print shell script for printing everything in regards to .jpg to a file.

Code:
Code:
stat --printf="%n\t%s\n" *.jpg >> results.csv


However I am trying to figure out how I can write the code to only print to a csv based on a size.

I thought it might have been:

Code:
#!/bin/bash

filesize=12345*.jpg
if filesize;
then
   stat --printf="%n\t%s\n" .jpg >> results.csv
done


Top
 Profile  
 PostPosted: Fri Sep 14, 2012 9:43 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Welcome George!

I don't get this line:
Code:
filesize=12345*.jpg

You mean you want to check all file sizes, then treat the corresponding files, if they are equal/greater than/... to a certain number?
Or perhaps you want to check the length of the filenames, or what?


Top
 Profile  
 PostPosted: Fri Sep 14, 2012 10:20 am   

Joined: Fri Sep 14, 2012 8:52 am
Posts: 8
I want to check the exact filesize of an image to 34.9kb. This size file is the exact file size of an image we process from a server that says no file exists. Now I have a folder that has the clients thumb images based on name so I cant sort through a huge folder all day. Im trying to make a script that will print to a csv file based on the image size. I thought if i declared filesize as a variable it may work that way but Im unsure how to declare a jpeg size as a variable and process it through an if statement. I can print the entire jpeg image folder name and size with stat but need to take it further.

The exact size of the file is 40318. I need to take files that are that size and print it to a csv


Top
 Profile  
 PostPosted: Fri Sep 14, 2012 11:40 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
for f in dir/*.jpg; do
    if (($(wc -c < "$f") == 40318)); then
        printf '%s\t%d\n' "$f" 40318 >> results.csv
    fi
done


Something like this?


Top
 Profile  
 PostPosted: Fri Sep 14, 2012 1:02 pm   

Joined: Fri Sep 14, 2012 8:52 am
Posts: 8
jsz wrote:
Code:
for f in dir/*.jpg; do
    if (($(wc -c < "$f") == 40318)); then
        printf '%s\t%d\n' "$f" 40318 >> results.csv
    fi
done


Something like this?



i tried to run it and it doesnt work. Can you also explain what it is doing?? I understand the for THIS do THAT and done, IF this THEN this END. But I dont understand the
Code:
(($(wc -c < "$f") == 40318));
I would imagine like other code
Code:
==
would be the equal than. Dont I still need to declare f??


Top
 Profile  
 PostPosted: Fri Sep 14, 2012 5:11 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
curiousgeorge wrote:
i tried to run it and it doesnt work.

How do you know that it doesn't work?


curiousgeorge wrote:
Can you also explain what it is doing??

Loop through each JPG file located in the directory dir.
If a JPG file's size is equal to 40318 bytes, then print the corresponding filename and the said size to results.csv.


curiousgeorge wrote:
[...] I dont understand the
Code:
(($(wc -c < "$f") == 40318));


This is the part where you check whether the size of the file being processed is equal to 40318 bytes.


curiousgeorge wrote:
I would imagine like other code
Code:
==

would be the equal than.

Yep, == means equal to.


curiousgeorge wrote:
Dont I still need to declare f??

Nope, f is the file being processed in the loop.
It'll take each filename as its value, one by one.

Try this line in your shell if you don't get it yet:
Code:
for w in word1 word2; do echo "$w"; done


Top
 Profile  
 PostPosted: Wed Sep 19, 2012 11:09 am   

Joined: Fri Sep 14, 2012 8:52 am
Posts: 8
I copy and pasted the code in a .sh file and changed dir to /images for the location foder of images and ran the code. Nothing happened. I tried changing:

Code:
printf '%s\t%d\n' "$f" 40318 >> results.csv


to

Code:
stat --printf="%n\t%s\n" >> results.csv


and nothing happened


Top
 Profile  
 PostPosted: Mon Sep 24, 2012 2:40 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
images/ then, not /images.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP