delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/10/28/11:32:24

X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
Subject: RE: Problem passing Windows path names from batch file to bash script
Date: Sat, 28 Oct 2006 11:32:08 -0400
Message-ID: <31DDB7BE4BF41D4888D41709C476B6570416928D@NIHCESMLBX5.nih.gov>
In-Reply-To: <20061028034348.GA17030@suncomp1.spk.agilent.com>
From: "Buchbinder, Barry \(NIH/NIAID\) [E]" <BBuchbinder AT niaid DOT nih DOT gov>
To: "Gary Johnson" <garyjohn AT spk DOT agilent DOT com>, <cygwin AT cygwin DOT com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT 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
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id k9SFWJIc029707

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/


- Raw text -


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