Your script is broken for the reason mendicant identified. You don't call functions you define with backticks or $(). That's used for output substitution, where the output of the command is replaced where the backticks or $() occured.
Your script has a thousand other major issues as well:
- UNIX users generally hates menus. Interactiveness for a task this simple is bad. All it lets me do is select JPG or PNG, essentially. It also breaks my ability to use the command in more powerful ways, like providing a whole list of directories.
- It does some dangerous things, which I'll outline below.
- It changes the PWD of the user and doesn't change it back, which is probably the worse sin you can do.
I'm going to provide a replacement script that's more in the style of a traditional UNIX script, as a courtesy.
Originally Posted by
testingubuntu
Code:
user=`whoami`
locate="/" # the location the user types in for a directory
images="/home/$user" #default directory to start in
You could have just said:
Code:
images="/home/`whoami`"
Code:
function CONVERTJPG
{
read -p "Type the path to the jpegs [$images]:" locate
cd $locate
This ought to be "$locate" in this context.
Code:
for img in $(ls *.[jJ][pP][gG])
This is the really bad sin. I'm going to say this once, and it's important:
Never, ever, use backticks (``) or $() on unbounded expressions.
That menas if you don't know the output of the command in advance, you can't use them. This is because if the output is too large, you'll crash the shell or possibly have other bad behavior.
Not safe. In this case, for supports filename matching, so you just say:
Code:
for img in $.[jJ][pP][gG]
Code:
convert -sample 25%x25% $img thumb-$img # Create the thumbnails
Both variables need "" around them to protect against "Filesnames With Spaces In Them". In fact, anything that's a filename almost always needs "", unless it's a constant one you know doesn't.
CONVERTPNG has the same badness going on. As does TEST.
Anyway, the way I'd write a script to convert whole directories of PNGs or JPGs and be semi-intelligent about it is:
Code:
#!/bin/bash
function usage {
echo "Usage: $0 [-v] jpg|png|both dirnames..."
echo "Creates thumbnails of all jpgs, pngs or both filetypes in the supplied dirnames."
}
VERBOSE=0
set -- `getopt -n "$0" -o v -- "$@"`
if [ "$?" -ne 0 ] ; then
usage
exit 1
fi
for arg in "$@" ; do
shift
if [ "$arg" == "--" ] ; then
break
fi
case "$arg" in
"-v")
VERBOSE=1
;;
*) # Unreachable
usage
exit 1
;;
esac
done
FORMAT=""
case "$1" in
jpg)
FORMAT="[jJ][pP][gG]"
;;
png)
FORMAT="[pP][nN][gG]"
;;
both)
FORMAT="[jJpP][pPnN][gG]" # Not perfect, but works, especially for an example.
;;
*)
usage
exit 1
;;
esac
shift
# Process directories now
for i in "$@" ; do
for file in "$i/*.${FORMAT}" ; do
convert -sample 25%x25% "$file" "thumb-$file"
done
if [ "$VERBOSE" -eq "1" ] ; then
echo "$0: Done processing $i"
fi
done
This is untested, but ought to work.
Bookmarks