It may be that the problem lies not so much with the script itself, but the way you are calling it.
If you have a file named filename with spaces then you cannot pass it to a script with
script filename with spaces
because the shell is going to pass that as three separate arguments,
and so your script will look for one file named filename,
a second file named with, and then a third file named spaces
— any of which may or may not exist, but in any case won't be the file
you meant to specify, the single file named filename with spaces.
To pass the file to a script, it must therefore be quoted.
The quoting rules depend on the environment you are using, but
in general if you put double-quotes around the filename (whether
or not it has spaces) you should be fine. On UNIXy systems,
single-quotes work, as does "escaping" the spaces with backslashes:
"filename with spaces" # works more or less universally
'filename with spaces' # works a lot of places, but not in the Windows
+ shell.
filename\ with\ spaces # ditto
And there may be other ways as well; check your system's documentation.
See also this Q&A entry: How do you open a filehandle to a DOS file which has spaces in the filename?
|