This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem passing Windows path names from batch file to bash script

On 2006-10-28, Buchbinder, Barry (NIH/NIAID) [E] wrote:
> From: Gary Johnson; Sent: Friday, October 27, 2006 11:44 PM
> > I am trying to pass Windows path names from a Windows batch file
> > to a Cygwin bash script.  I have found a solution using Windows
> > environment variable substitution to replace \s with /s before
> > passing the name to bash, but I cannot figure out how to pass
> > the name without that replacement.  The problem is with names of
> > the form
> > 
> >     "\\host\user\file with spaces"
> > 
> > No matter what I've tried, bash insists on collapsing the two
> > leading \s to one, like this,
> > 
> >     \host\user\file with spaces
> > 
> > before anything else can be done with the name.


> It is not clear to me that bash is doing this, since cygpath behaves
> identically when run from cmd.exe (XPpro).
> 	c:\>cygpath -u "\\host\user\file with spaces"
> 	/c/host/user/file with spaces
> 	c:\>cygpath -u '\\host\user\file with spaces'
> 	/c/host/user/file with spaces

Good observation.  Thanks.  That does remove bash from the picture.  
The results from executing those same commands from the bash prompt 
are oddly different, though (ignoring the "/cygdrive" directory, of 

    $ cygpath -u "\\host\user\file with spaces"
    /cygdrive/c/host/user/file with spaces

    $ cygpath -u '\\host\user\file with spaces'
    //host/user/file with spaces

    $ cygpath -u '"\\host\user\file with spaces"'
    "//host/user/file with spaces"

> If you know that your batch file will always be feeding something
> starting with \\host to bash, you could add and extra pair of back
> slashes.
> 	c:\>cygpath -u '\\\\host\user\file with spaces'
> 	//host/user/file with spaces
> 	c:\>cygpath -u "\\\\host\user\file with spaces"
> 	//host/user/file with spaces

The batch file is called from Windows, so I don't have any control 
over the path name string.  I think there are only four cases, 

    1.  C:\directory\file_without_spaces
    2.  "C:\directory\file with spaces"
    3.  \\host\user\file_without_spaces
    4.  "\\host\user\file with spaces"

The drive letter could be different, but the format would be the 
same.  So I think it would be sufficient to test the path in the 
bash script for a leading backslash and if present, double it.

> It looks like if you can use single quotes in bash, it works OK.
> 	/c> p='\\host\user\file with spaces' ; cygpath -u $p ; cygpath
> "$p"
> 	//host/user/file
> 	with
> 	spaces
> 	//host/user/file with spaces

Yes, but I have the path as a shell parameter, not as a literal
string, so I have to expand the parameter without expanding the
backslashes, and expanding the parameter can't be done within single

> You might consider dumping it into a file and the converting by hand in
> your script.
> Batch:
> 	c:\>echo "\\host\user\file with spaces" > c:\cygwin\tmp\ttt
> Script:
> 	foo "$(sed -e 's,\\,/,g' -e 's/" *//g' /tmp/ttt)"
> 	rm /tmp/ttt
> Note, I have only partly tested it so you might have to play with it a
> bit more before it works.

Wow, that does work!

    Batch (foo.bat):
        @echo off
        echo %1 > mypath.txt
        C:\cygwin\bin\bash bar

    Script (bar):
        path=$(< mypath.txt)
        echo $path
        /bin/cygpath -u "$path"

        foo "\\host\user\file with spaces"

        "\\host\user\file with spaces"
        "//host/user/file with spaces"

It's such a pity to have to use a temporary file to pass a simple
parameter, though.

The solution I've been using so far is to put this in the batch 

    SET arg=%1
    C:\cygwin\bin\bash --login -c 'bash_script %arg:\=/%'

This replaces all the backslashes in arg with forward slashes before  
arg is given to bash.  (I just stumbled upon that in my desperate 
reading of Windows help commands.  I know next to nothing about 
batch file programming.)

I sure wish I knew why it was doing this, but at least I have some
alternative solutions to consider.  Thanks again.


Gary Johnson                 | Agilent Technologies     | Wireless Division
                             | Spokane, Washington, USA

Unsubscribe info:
Problem reports:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]