2

I have a Windows diff tool that's simply better than any Linux diff tool I've found (and I've done some pretty extensive analysis on visual diff tools). I want to use that tool on Linux (via Wine) and be able to pass Linux file paths to it, in part because two-pane file managers like Double Commander automatically pass the full file paths when calling the diff tool.

So I'm asking the reverse of this question: How can I convert the Linux-style file paths, including escaping characters such as spaces, to Windows-style file paths that would be accepted by a Windows tool ran via Wine?

For a simplistic example,

Input:

/path/to/file1.ext /path/to/file2.ext

Output

"\path\to\file1.ext" "\path\to\file2.ext"

Of course, in the real world, files can have spaces and whatnot, so I'm looking for a solution more reliable than the bash substitution I have so far:

#!/bin/bash
p1=$1
p2=$2
wine /utils/wincmp.exe ${p1//\//\\} ${p2//\//\\}
  • 3
    Windows generally accepts / in place of \ just fine; are you sure it doesn't work already if that's all you're doing to them? – Michael Homer Dec 28 '17 at 7:02
  • 1
    In Grigsoft's CompareIt! / is the option introducer character and it supports a whole bunch of options such as /n and /r. Part of the problem here is I suspect the Win32 tool not supporting anything like the Unix -- convention, thus having no way to stop it from thinking that POSIX-style paths are command options. (I have always made sure that my OS/2, DOS, and Win32 tools allow either - as the option character or have a // mechanism. Not everyone does, though.) – JdeBP Dec 28 '17 at 7:55
4

Your script is actually very close. All that you need is the winepath tool, that comes as part of WINE, to do the conversion:

#!/bin/sh
exec wine /utils/wincmp.exe "`winepath --windows \"$1\"`" "`winepath --windows \"$2\"`"

Or using Laurent Bercot's execline:

#!/command/execlineb -S2
backtick -i left { winepath --windows $1 }
backtick -i right { winepath --windows $2 }
multisubstitute {
    importas left left
    importas right right
}
wine 
/utils/wincmp.exe ${left} ${right}

  • Didn't know about winepath and it does the trick! – Dan Dascalescu Dec 30 '17 at 2:02
1
  1. Always double-quote your variables so that you don't need to care about spaces or shell meta-characters.

  2. you can use sed or tr to convert /s to \. e.g. sed 's:/:\\:g' or tr '/' '\\' (note: because \ is the shell escape character, it needs to be escaped to be treated as a literal backslash).

  3. Alternatively, bash's string manipulation operators will do the job (but aren't posix standard, so aren't portable). The only mistake you made there was in not quoting the variables.

But note that, as mentioned by @Michael Homer, it shouldn't be necessary to modify the path separator...I've certainly had no difficulty running wine program.exe /unix/path/to/file in the past.

resulting in:

#!/bin/bash

p1="$1"
p2="$2"

# optional transformation
p1="$(printf "%s" "$p1" | tr '/' '\\')"
p2="$(printf "%s" "$p2" | tr '/' '\\')"

wine /utils/wincmp.exe "$p1" "$p2"

or

#!/bin/bash

p1="${1//\//\\}"
p2="${2//\//\\}"

wine /utils/wincmp.exe "$p1" "$p2"

or even:

#!/bin/bash
wine /utils/wincmp.exe "${1//\//\\}" "${2//\//\\}"

(but this last version doesn't give you any opportunity to sanity-check the file arguments, or do any kind of option handling - e.g. with getopts)


Finally, you may want to reconsider your preference for a Windows diff tool, or at least conduct a more thorough survey before deciding (you've only evaluated a small subset of available tools and GUI wrappers). A Windows/wine based tool will not integrate pleasantly into Linux/UNIX-based development workflow.

(personally, I mostly use either diff -u or colordiff -u piped into less. Given that I'm working with text files, there's no need for a GUI interface. It's also identical to what I get from git diff etc. On the rare occasions I need a side-by-side diff, I mostly use sdiff).

  • 1
    That's probably a bit rich coming from here really, from M. Dascalescu's perspective. Xe has tested all of tools that get listed on this WWW site in questions such as unix.stackexchange.com/questions/4573 and more besides. It might be worth xyr asking a What GUI diff tool does not hang trying to compare these 2 files? question, but there's really no grounds for saying that xe has not been at least as thorough as we are in questions like unix.stackexchange.com/questions/406525 . – JdeBP Dec 28 '17 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.