views:

419

answers:

3

I have a variable that stores a Unix path, for example:

typeset unixpath=/foo/bar/

And I have to convert it to a DOS path using Korn shell scripting:

dospath=\\foo\\bar\\

+2  A: 

Try:

dospath=`echo $unixpath | sed 's/\//\\\\/g'`

Thanks to David Wolever for reminding me to use a $ to access the value of the variable!

eleven81
I'm no korn expert, but I think that needs a '$' somewhere... Anyway, that's exactly how I'd do it, if you hadn't posted first.
David Wolever
It depends whether eleven81 meant: echo /unix/path or echo $unixpath; either could be valid.
Jonathan Leffler
A: 

If you have ksh93:

ksh-M 93t 2008-11-04$ unixpath=/foo/bar/            
ksh-M 93t 2008-11-04$ print ${unixpath//\//\\\\\\\\}
\\foo\\bar\\
radoulov
A: 

I would have added as a comment to eleven81's answer, but I don't have the points

to make it slightly more readable, how about using an alternative sed delimiter

i.e.

dospath=`echo $unixpath | sed 's./.\\\\.g'`
Joe Watkins