delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2015/06/26/15:06:59

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:to:from:subject:date:message-id:mime-version
:content-type:content-transfer-encoding; q=dns; s=default; b=TSb
mgnzA3msU38LKrRivz0ZTMFq8S08Sr6X7iBN7drCVLK5dmwNg97WM3cfV67XLKJN
Hn0uDpQxEfm1XXB0um1V6Akof0ldfb6kpMlGz3frSERUpNNbTgoyJMCXIyPo9b/u
lDzrc5UbYBXsj2/mUKpI48XB8vmuF0ssRK1BFvQQ=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:to:from:subject:date:message-id:mime-version
:content-type:content-transfer-encoding; s=default; bh=h3wP4vDyE
JC4ZPrq1NWsxJ4Ol4g=; b=aIJ2ewV1pZ5tLYDzvyWbvfMO7fkDfK3sdokCAMoRC
YWg7mZ6GdSlBXOVsEeHLSHQTglAnwuS8+ToHXFSOD1EM11cmUuz1PhFwLNM9J9jP
JLQcbnmaYwuk1L4KVIMZQT3VO55KGaTZ4baM8nL2T33F//kVbuBsS9Xc4taKCgod
ew=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-4.0 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2
X-HELO: plane.gmane.org
To: cygwin AT cygwin DOT com
From: JJ Ottusch <ncokwqc02 AT sneakemail DOT com>
Subject: Problem passing file names with embedded space to Windows executable via bash function
Date: Fri, 26 Jun 2015 19:06:30 +0000 (UTC)
Lines: 94
Message-ID: <loom.20150626T183854-995@post.gmane.org>
Mime-Version: 1.0
User-Agent: Loom/3.14 (http://gmane.org/)
X-IsSubscribed: yes

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'. 

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019