This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: Problem passing file names with embedded space to Windows executable via bash function
- From: Ernie Rael <err at raelity dot com>
- To: cygwin at cygwin dot com
- Date: Fri, 26 Jun 2015 12:50:54 -0700
- Subject: Re: Problem passing file names with embedded space to Windows executable via bash function
- Authentication-results: sourceware.org; auth=none
- References: <loom dot 20150626T183854-995 at post dot gmane dot org>
On 6/26/2015 12:06 PM, JJ Ottusch wrote:
I am trying to define a function in my '.bash_profile' that takes a file or
list of files as an argument and passes the list to a Windows executable
after converting all the filenames to full path filenames with 'cygpath'.
I use something similar to this, perhaps it would meet your needs. You
may want to use different option to cygpath.
#!/usr/bin/bash
targs() {
if (($# == 0)); then
args=()
else
IFS=$'\n'
args=($(cygpath -m -- "$@"))
IFS=$' \t\n'
fi
for i in "${args[@]}"; do echo "'$i'"; done
some_command "${args[@]}"
}
-ernie
The whole point of the function is to use 'cygpath' to put the filenames in a
Windows compatible format for me so I don't have to do it every time I call
the Windows executable from a CYGWIN term.
My function works fine except in cases where any part of the path or filename
contains a space. In that case the list of arguments the Windows executable
sees is incorrectly broken apart by the embedded spaces. I can verify this by
looking at argc.
Here are some examples with comments. All commands run from
'/cygdrive/c/Temp/Part1\ Part2/' in a CYGWIN RXVT term.
Let the windows executable be named 'oglv64' and start with the usual
int main(int argc, char** argv)
Let there be two files it can take as arguments named 'file1' and 'file2' in
the directory '/cygdrive/c/Temp/Part1 Part2', where 'Part1 Part2' is a
subdirectory name with an embedded space. Let the defined function be named
'foo'.
1. Show the files
$ dir /cygdrive/c/Temp/Part1\ Part2/
file1* file2*
2. Test the function with 'echo' instead of 'oglv64'. 'cygpath' works
correctly on each file, but there is a bare space between 'Part1' and 'Part2'
which can be expected to cause problems with 'oglv64'.
$ foo() { echo `cygpath -am "$@"` ; }; foo file*
C:/Temp/Part1 Part2/file1 C:/Temp/Part1 Part2/file2
3. So, escape the space. Looks good.
$ foo() { echo `cygpath -am "$@" | sed 's/ /\\\ /'` ; }; foo file*
C:/Temp/Part1\ Part2/file1 C:/Temp/Part1\ Part2/file2
4. Although I can't show it, the Window executable works exactly as desired
when given the above line as its input. argc=3 and 'oglv64' operates on the
two files as desired.
$ oglv64 C:/Temp/Part1\ Part2/file1 C:/Temp/Part1\ Part2/file2
5. Now try this inside the function definition. As shown below, this didn't
work. argc=5 instead of 3.
$ foo() { oglv64 `cygpath -am "$@" | sed 's/ /\\\ /'` ; }; foo file*
Can't load C:/Temp/Part1\.
Can't load Part2/file1.
Can't load C:/Temp/Part1\.
Can't load Part2/file2.
6. Put the full argument in quotes and try again. The 'echo' result is
correct, but given on two separate lines. However, the Windows executable
sees argc=2 and thinks it's all one long filename. Wrong, as expected.
$ foo() { echo "`cygpath -am \"$@\" | sed 's/ /\\\ /'`" ; }; foo file*
C:/Temp/Part1\ Part2/file1
C:/Temp/Part1\ Part2/file2
$ foo() { oglv64 "`cygpath -am \"$@\" | sed 's/ /\\\ /'`" ; }; foo file*
Can't load C:/Temp/Part1\ Part2/file1
C:/Temp/Part1\ Part2/file2.
7. Drop the escaped space and try forcing quotes at the end of each 'cygpath'
result. Looks fine with 'echo', but the Windows executable still sees argc=5
rather than argc=3.
$ foo() { echo `cygpath -am "$@" | sed 's/^/\"/;s/$/\"/'` ; }; foo file*
"C:/Temp/Part1 Part2/file1" "C:/Temp/Part1 Part2/file2"
$ foo() { oglv64 `cygpath -am "$@" | sed 's/^/\"/;s/$/\"/'` ; }; foo file*
Can't load "C:/Temp/Part1.
Can't load Part2/file1".
Can't load "C:/Temp/Part1.
Can't load Part2/file2".
8 On the other hand, using the output from 'echo' as a direct argument to
'oglv64' works fine.
$ oglv64 "C:/Temp/Part1 Part2/file1" "C:/Temp/Part1 Part2/file2"
No success so far. I am looking to find a simple solution to this problem
(other than eliminating spaces from all file/directory names). It would have
multiple applications.
jjo
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple