X-Spam-Check-By: sourceware.org Date: Sat, 28 Oct 2006 14:45:04 -0700 From: Gary Johnson To: cygwin AT cygwin DOT com Subject: Re: Problem passing Windows path names from batch file to bash script Message-ID: <20061028214504.GA26508@suncomp1.spk.agilent.com> Mail-Followup-To: cygwin AT cygwin DOT com References: <20061028034348 DOT GA17030 AT suncomp1 DOT spk DOT agilent DOT com> <31DDB7BE4BF41D4888D41709C476B6570416928D AT NIHCESMLBX5 DOT nih DOT gov> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <31DDB7BE4BF41D4888D41709C476B6570416928D@NIHCESMLBX5.nih.gov> User-Agent: Mutt/1.5.12 (2006-07-14) X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com 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 course). $ 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, though: 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 quotes. > 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" Command: foo "\\host\user\file with spaces" Output: "\\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 file: 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. Regards, Gary -- Gary Johnson | Agilent Technologies garyjohn AT spk DOT agilent DOT com | Wireless Division | Spokane, Washington, USA -- 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/