Hi,
Got an annoying shell script bug. I'm using GNU bash, version 3.1.17(1)-release. Here's a testcase.sh shell script:
#!/bin/bash -v myfiles="/home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt" echo binary --usethese $myfiles binary --usethese $myfiles
Relevant output to console:
$ ./testcase.sh #!/bin/bash -v myfiles="/home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt" echo binary --usethese $myfiles binary --usethese /home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt binary --usethese $myfiles $
I want to call an external binary with a parameter made of filenames which may contain spaces.
If I run the output of the echo statement above (4th line of output) manually by cutting and pasting into the console, the scanner works fine.
However, it always fails if the same command is manually called with quotes around the parameter value:
WORKS: binary --usethis /home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt
DOESN'T WORK: binary --usethis "/home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt"
Now, despite me *not* putting in quotation marks in the shell script, it appears that the call on the third line is being interpreted as though I have used them ... and so the call fails.
Anyone got any suggestions?
Peter.
On 02/03/07, samwise samwise@bagshot-row.org wrote:
#!/bin/bash -v myfiles="/home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt" echo binary --usethese $myfiles binary --usethese $myfiles
I can't try this atm to check, but have you tried ...
myfiles="/home/samwise/Desktop/file\ with\ spaces.txt /tmp/otherfile.txt"
as you want the backslash to be passed to the binary, so /it/ knows that it's an escape character.
Greg [Resent cc'ing the list]
Yeah, that didn't work. I've been getting some old-skooling on btintra.unix and I think the problem is that the binary needs quotation marks rather than backslashes ... Still have some work to do to get it there but at least I'm not stuck anymore.
Also worth noting that I've discovered putting "set -x" at the top of the script is a god-send ... ;)
Cheers, Greg.
Peter.
On Fri, 2007-03-02 at 19:08 +0000, samwise wrote:
t at least I'm not stuck anymore.
Also worth noting that I've discovered putting "set -x" at the top of the script is a god-send ... ;)
ahh set -x how ye has saved me.
Other similar useful things are shown here, well worth a read if you are a newcomer to shell scripting.
http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
Might well come in handy .. the solution wasn't as simple as I'd thought.
Now messing with arrays. :(
*sigh*
Peter.
samwise samwise@bagshot-row.org wrote:
I want to call an external binary with a parameter made of filenames which may contain spaces.
I don't think bash handles this well at all because it reparses strings sometimes but not others. Either you do it a different way (as your later comment suggests you're doing, using arrays) or you change shell - this exact problem is part of the reason I use /bin/rc when possible.
Sorry,
On 02/03/07, MJ Ray mjr@phonecoop.coop wrote:
samwise samwise@bagshot-row.org wrote:
I want to call an external binary with a parameter made of filenames which may contain spaces.
I don't think bash handles this well at all because it reparses strings sometimes but not others. Either you do it a different way (as your later comment suggests you're doing, using arrays) or you change shell - this exact problem is part of the reason I use /bin/rc when possible.
Sorry,
Well, finally got it sorted on Friday evening ... and didn't have to resort to arrays. Basically, I was sending through escaped spaces ... and the binary can't handle that. It works from the command line because bash strips those off. So, I needed to quote the parameters, but not escape them. However, even that didn't work with double quotation marks - I had to resort, in the end, to single quotation marks.
Finally got their ... but I've spent far too many man-hours on what should have been a simple task. :/
Ah, well - hopefully I learned something for it!
Peter.