On Sun, May 14, 2017 at 07:02:41PM +1000, Tony White wrote:
> #!/bin/sh
> # run this is script in the image folder
> # change the value 320 to whatever width you want
> response = 320
> for f in *; do
> # prefix the results with sm_ or change to what you want
> convert $f -resize $response +profile "*" sm_$f
> done
You really should quote your variables.
If any of the filenames have spaces (or other problematic characters
like *, ?, [, (, and other characters with special meaning to the
shell) in them, this script will break.
Those are all valid characters in a filename on all linux
filesystems. There are only two characters which are not valid in a
filename, a forward-slash and a NUL byte. Anything else is permitted.
To avoid any problems cause by such annoying characters, you should
surround variables names with double-quotes when you use them, for the
same reason you quoted the "*". e.g.
convert "$f" -resize "$response" +profile "*" "sm_$f"
There are other reasons to quote variables, especially when you can not
have complete knowledge or control over the value of a variable.
As a general rule, **ALWAYS** double-quote variables when you use them.
It can pretty nearly never hurt to double-quote a variable, but there
are plenty of situations using an unquoted variable can cause severe
damage.
(NOTE: there are some very specific rare situations where you don't want
to and shouldn't quote variables, but if you´re capable of writing
the kind of shell code that requires that, you'll know when not to
quote. Otherwise, just quote variables all the time: 99 times out
of 100 (or more) it is exactly the right, safe, and correct thing
to do. Or, to put it another way, unless you know exactly WHY you don't
want to quote a specific variable, then quote it)
BTW, this script has clearly not been tested - typed in from memory?
There should be no space between the variable name, the equals sign, or
the value being assigned. ´response=320' would assign the value 320 to
the variable $response. 'response = 320' does not, it is an attempt to
run a command called 'response' with two command line arguments '=' and
'320'.
Also BTW, use double-quotes when you need to interpolate variables or
sub-shell results etc into a string, and it's best to use single-quotes
otherwise, like so:
convert "$f" -resize "$response" +profile '*' "sm_$f"
That's the key difference between double and single-quotes - single-quotes
are for static, fixed, literal strings. Double-quotes are for for variables
and other dynamically generated output.
Finally, when passing filename arguments to programs that understand the
GNU convention of '--' to indicate the end of program options, it's also
always a good idea to use it to prevent filenames beginning with a '-'
from being interpreted as options. e.g. if a directory contains a file
called '-rf' in it, there is a huge difference between running 'rm *'
and 'rm -- *'
craig
--
craig sanders
___
luv-main mailing list
luv-main@luv.asn.au
https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main