LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-20-2013, 02:48 PM   #1
seanferd
LQ Newbie
 
Registered: Feb 2006
Distribution: Debian Wheezy
Posts: 13

Rep: Reputation: 0
Bash Scripting Help, can't get values to compare


Hi everyone,

I'm having some trouble with a Bash script that I wrote quite a while ago. I'm running on Debian Wheezy at the moment, the last time I knew this worked was on Lenny probably.

Here's the snippet that I'm having trouble with:

Code:
for i in *.jpg;
do
        DIM=`identify $i | cut -d' ' -f3 | cut -dx -f1,2`;
        WIDTH=`echo $DIM | cut -dx -f1`;
        HEIGHT=`echo $DIM | cut -dx -f2`;

        if [ $HEIGHT -gt $WIDTH ]; then
                mv $i ./tall;
        fi
done
Essentially the 'if' is never validating these two and I have no idea why. It previously worked fine, but after upgrading it just doesn't seem to like it. I searched around and found that bash isn't typed so this should be fine as well.

What I'm cutting out for the parameters is the filesize. Echoing them reveals that it's correctly picking it up e.g. 1900x1200 - though, again, the comparison is always false. If anyone can provide some insight here, I'd be extremely grateful.

If anyone cares, the script itself was written to resize a bunch of images. It runs through and looks at whether it's landscape or portrait and then moves the image to a new directory if so. Eventually it will come back and resize based on what directory the files are in. I'd love to just rewrite it, but the script is huge and this portion is the only piece that doesn't seem to work correctly anymore.

Thanks for any thoughts!
 
Old 12-20-2013, 05:46 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by seanferd View Post
Essentially the 'if' is never validating these two and I have no idea why.
This snippet is self-contained and small enough for you to be able to run it on say three files and show us the actual output
Code:
set -vx
find /some/path -type f -iname \*.jpg | while read ITEM; do
 DIM=($(identify "${ITEM}")); [ "${DIM[2]}" != "${DIM[2]//x/}" ] || break
 [ ${DIM[2]//x*} -gt ${DIM[2]//x*} ] && mv "${ITEM}" /some/path/tall
done
 
Old 12-30-2013, 01:08 PM   #3
seanferd
LQ Newbie
 
Registered: Feb 2006
Distribution: Debian Wheezy
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
This snippet is self-contained and small enough for you to be able to run it on say three files and show us the actual output
Code:
set -vx
find /some/path -type f -iname \*.jpg | while read ITEM; do
 DIM=($(identify "${ITEM}")); [ "${DIM[2]}" != "${DIM[2]//x/}" ] || break
 [ ${DIM[2]//x*} -gt ${DIM[2]//x*} ] && mv "${ITEM}" /some/path/tall
done
Hi unSpawn,

Thanks for replying. Sorry for not getting back to you. I ran what you provided on three images, here's the output:

Code:
+ find ./ -type f -iname '*.jpg'
+ read ITEM
+ DIM=($(identify "${ITEM}"))
identify "${ITEM}")
identify "${ITEM}")
identify "${ITEM}"
++ identify ./002.jpg
+ '[' 3648x2736 '!=' 36482736 ']'
+ '[' 3648 -gt 3648 ']'
+ read ITEM
+ DIM=($(identify "${ITEM}"))
identify "${ITEM}")
identify "${ITEM}")
identify "${ITEM}"
++ identify ./003.jpg
+ '[' 3648x2736 '!=' 36482736 ']'
+ '[' 3648 -gt 3648 ']'
+ read ITEM
+ DIM=($(identify "${ITEM}"))
identify "${ITEM}")
identify "${ITEM}")
identify "${ITEM}"
++ identify ./001.jpg
+ '[' 3648x2736 '!=' 36482736 ']'
+ '[' 3648 -gt 3648 ']'
+ read ITEM
 
Old 12-30-2013, 01:30 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Uh.
Code:
find /some/path -type f -iname \*.jpg | while read ITEM; do DIM=($(identify "${ITEM}"))
 [ ${DIM[2]//*x/} -gt ${DIM[2]//x*/} ] && mv "${ITEM}" /some/path/tall/
done
Don't know what I was thinking...
 
Old 01-07-2014, 10:33 AM   #5
seanferd
LQ Newbie
 
Registered: Feb 2006
Distribution: Debian Wheezy
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Uh.
Code:
find /some/path -type f -iname \*.jpg | while read ITEM; do DIM=($(identify "${ITEM}"))
 [ ${DIM[2]//*x/} -gt ${DIM[2]//x*/} ] && mv "${ITEM}" /some/path/tall/
done
Don't know what I was thinking...
This works as expected. Thank you! I'm having a little trouble understanding all that's going on here, though. So, we're finding all of the regular files that end in jpg. The while is running through them all, I assume the read ITEM is like saying 'for ITEM in *.jpg' but based on the input from the find, correct?

Where I'm confused is the beginning of $[DIM[2]// . I assume this is like a sed wherein you just strip the numbers prior to the x in identify, right? If that's true and it works, I don't understand why my original script stopped. You're essentially doing the same -gt that I had, on the same values I was trying before - what gives? Is the output within brackets actually cast as an int or something?

Either way, thanks so much. I like what you've done here, this is way more straightforward and streamlined than what I had previously. Most importantly, it works again!

Last edited by seanferd; 01-07-2014 at 10:37 AM.
 
Old 01-08-2014, 07:05 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See this section on Parameter Expansion and indeed read the whole page; very useful stuff.
http://wiki.bash-hackers.org/syntax/...ch_and_replace
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash scripting problem: CSV list of filenames and actual files compare; Issue with plus symbol gn000we Linux - General 2 08-05-2013 06:57 PM
Bash Scripting, looping through subdirectories to create 1 and 0 column of values conejo_perez16 Linux - Newbie 3 08-18-2012 06:33 PM
bash: how to compare REAL numeric values? muebi Linux - General 3 02-28-2012 08:00 AM
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 10:11 AM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:36 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
Open Source Consulting | Domain Registration