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


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.
> 
> 
> Here's an illustration of the problem.  I've created a batch file,
foo.bat, that contains the following lines:
> 
>     @echo off
>     echo %1 '%1'
>     C:\cygwin\bin\bash -c 'echo %1'
>     C:\cygwin\bin\bash bar %1 '%1'
> 
> and a file, bar, in the same directory containing these lines:
> 
>     echo $1 "$1"
>     echo $2 "$2"
> 
> Executing the following command from the Command Prompt,
> 
>     foo "\\host\user\file with spaces"
> 
> produces the following output:
> 
>     "\\host\user\file with spaces" '"\\host\user\file with spaces"'
>     \host\user\file with spaces
>     \host\user\file with spaces \host\user\file with spaces
>     "\host\user\file with spaces" "\host\user\file with spaces"
> 
> 
> I can come up with a number of reasons why I think the results should
be different, but that doesn't really matter, since there must be
something I'm missing.  Would someone please explain this behavior or
direct me to the fine manual I should have read?  How should one pass
such a path name from Windows to a bash script without losing any
information?
> 
> Thanks,
> Gary
> 
> -- 

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

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

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

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.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


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