tg3793
November 21st, 2010, 03:35 AM
Good morning everyone (not that time matters on the Internet :-))
I've searched several sites and have been able to pick up a great deal of what I need to know on tldp.org (http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html) and Youtube. I was surprised however that I didn't see more examples of the way Bruceydk was demonstrating it. I find his method 'very' useful.
PHP Code:
#!/bin/sh
# nautilus passes every file selected as an argument to this script, what happens
# here is that it loops through each of the arguments (filenames).
for arg; do
# uses the file command to determine the type of file (uses the "magic numbers")
# and stores it in a variable called filetype
filetype=`file -i "$arg"`
# -n tests to see if the argument is non empty, if it doesn't match jpeg
# the echo would return nothing (i.e. it would be empty and so the condition
# would fail).
if [ -n "`echo $filetype | grep -i 'jpeg'`" ]; then
# so if the filetype matches jpeg it'll use jpegtran to rotate it
jpegtran -rotate 90 "$arg" > temp.jpg;
mv temp.jpg "$arg"
else
# if it can't determine if the file is a jpeg it uses convert from
# imagemagick to manipulate it, which is a bit strange because it might
# aswell be a text file (ASCII text).
convert -rotate 90 "$arg" "$arg"
fi
done
And with his example in mind I'd like to post a script that I've been playing around and editing and ask a specific question or two about it. Here is the script:
#!/bin/bash
while [[ -n "$1" ]]; do
#if a file and not a dir
if [[ -f "$1" ]]; then
#the images that I copy from my cell phone don't have exif headers
#so I am using the -mkexif switch first to match the exif information
#to the "created date" in the .jpg file.
jhead -mkexif "$1"
#by default, jpegtran will only copy some
# Exif data, so we'll specify "all"
jpegtran -rotate 270 -copy all -outfile "$1" "$1"
#Then the next line uses the -ft switch which will match the "modified date"
#using the exif date and time previously matched from the first line
#of this script.
jhead -ft "$1"
#clear rotation/orientation tag so that
# some viewers (e.g. Eye of GNOME)
# won't be fooled
jhead -norot "$1"
fi
shift
done
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Ok; what I'm curios about is the "for arg; do" in the first script compared to the "while [[ -n "$1" ]]; do" in the second script. I'm still quite new to this but from what I can tell isn't this two ways of doing the same thing? And 'if' I am correct could someone explain to me if there is a benefit of one way versus the other.
I know that one is scripting for "sh" and the other for "BASH". But again which way would be better to do it (bring in files selected) and why.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
I've searched several sites and have been able to pick up a great deal of what I need to know on tldp.org (http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html) and Youtube. I was surprised however that I didn't see more examples of the way Bruceydk was demonstrating it. I find his method 'very' useful.
PHP Code:
#!/bin/sh
# nautilus passes every file selected as an argument to this script, what happens
# here is that it loops through each of the arguments (filenames).
for arg; do
# uses the file command to determine the type of file (uses the "magic numbers")
# and stores it in a variable called filetype
filetype=`file -i "$arg"`
# -n tests to see if the argument is non empty, if it doesn't match jpeg
# the echo would return nothing (i.e. it would be empty and so the condition
# would fail).
if [ -n "`echo $filetype | grep -i 'jpeg'`" ]; then
# so if the filetype matches jpeg it'll use jpegtran to rotate it
jpegtran -rotate 90 "$arg" > temp.jpg;
mv temp.jpg "$arg"
else
# if it can't determine if the file is a jpeg it uses convert from
# imagemagick to manipulate it, which is a bit strange because it might
# aswell be a text file (ASCII text).
convert -rotate 90 "$arg" "$arg"
fi
done
And with his example in mind I'd like to post a script that I've been playing around and editing and ask a specific question or two about it. Here is the script:
#!/bin/bash
while [[ -n "$1" ]]; do
#if a file and not a dir
if [[ -f "$1" ]]; then
#the images that I copy from my cell phone don't have exif headers
#so I am using the -mkexif switch first to match the exif information
#to the "created date" in the .jpg file.
jhead -mkexif "$1"
#by default, jpegtran will only copy some
# Exif data, so we'll specify "all"
jpegtran -rotate 270 -copy all -outfile "$1" "$1"
#Then the next line uses the -ft switch which will match the "modified date"
#using the exif date and time previously matched from the first line
#of this script.
jhead -ft "$1"
#clear rotation/orientation tag so that
# some viewers (e.g. Eye of GNOME)
# won't be fooled
jhead -norot "$1"
fi
shift
done
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Ok; what I'm curios about is the "for arg; do" in the first script compared to the "while [[ -n "$1" ]]; do" in the second script. I'm still quite new to this but from what I can tell isn't this two ways of doing the same thing? And 'if' I am correct could someone explain to me if there is a benefit of one way versus the other.
I know that one is scripting for "sh" and the other for "BASH". But again which way would be better to do it (bring in files selected) and why.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~