Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need a very simple script which does this:

mogrify -resize $1x$2^ -gravity center -crop $1x$2+0+0 $3

so that I can call it in this way:

cropresize.sh 110 110 *.png

the problem is that the shell expands the *.png pattern instead of passing it as it is to the script.

How can I achieve this (script, alias or any other equivalent solution is fine)?

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

Since the shell performs glob expansion before the arguments are handed over to the command, there's no way I can think of to do it transparently: it's either controlled by the user (quote the parameter) or brute-force (disable globbing completely for your shell with set -o noglob).

You're looking at the problem from the wrong end. Change your script to accept multiple filename arguments:

x=$1
y=$2
shift 2
mogrify -resize ${x}x${y}^ -gravity center -crop ${x}x${y}+0+0 "$@"
share|improve this answer
    
After some thought I came to this conclusion but I couldn't find how to specify "the rest of the arguments after the first two", I was just missing shift :) –  Matteo Riva Mar 3 '11 at 14:59
add comment

Have you tried

cropresize.sh 110 110 \*.png

But I can only see zsh (not bash) expanding it like you say.

share|improve this answer
    
Well I can even enclose the argument in single quotes like '*.png' but I'd really like it to be completely transparent as if it were a builtin command –  Matteo Riva Mar 3 '11 at 14:31
    
Are you using bash or some other shell? Because in bash what you ask works, but in zsh (which I use) even echo *.png tries expanding to all png files. –  ryuslash Mar 3 '11 at 14:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.