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

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:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type:content-transfer-encoding;
q=dns; s=default; b=Z2EivcE2dxz0yqY7d/mwSk3DT+BZISWQdWjjADZVq5s
LNh9wIrC/IkYFTnxEukkgEwi9fbgG+e4IHd9JMCx6i+iv9rxSl5puKD6Ff0vGgcO
Mr+mv7sAnHO3L6KlROxC90uyJSv1s3Z8gynK59TTJX2JCcD75fQcUurwd13WCSas
=
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:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type:content-transfer-encoding;
s=default; bh=vsU694YJZv/02SFd0Vgbi0AbNUU=; b=U182Ndv2wEdjEbnfM
eaNSmdLfEZWdcY1Qu823KTpWoMluDhmTg0xj+aVHU2s/tgg3MbHxhcZ0hv5DhMcT
3HnUd19cCEe4tgUHb+m1UHcc26BNhD1iN7JHN4Iv8DZGmBKosSzkJOy3R8/a3dW9
UJeFquXFMyqv4oX1Zl1jB70MUg=
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=-0.9 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_NONE autolearn=no version=3.3.2
X-HELO: resqmta-po-10v.sys.comcast.net
Message-ID: <558DAD1E.7060307@raelity.com>
Date: Fri, 26 Jun 2015 12:50:54 -0700
From: Ernie Rael <err AT raelity DOT com>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Problem passing file names with embedded space to Windows executable via bash function
References: <loom DOT 20150626T183854-995 AT post DOT gmane DOT org>
In-Reply-To: <loom.20150626T183854-995@post.gmane.org>
X-IsSubscribed: yes

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

- Raw text -


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