Link to home
Start Free TrialLog in
Avatar of ugeb
ugebFlag for United States of America

asked on

sed one-liner for windows (bash shell)

Hi,

I'm trying to do a one-liner using sed in a Cygwin bash on Windows.  I want to be able to copy a directory from Windows explorer to Cywin and have it cd to that directory without complaining about the slashes. For example, in explorer I copy the address field that looks like this:

C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\

I'd like to paste this into bash as follows:

cd C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\

and have it change directories without complaining about the orientation of the slashes.  The spaces cause an issue too, so I would probably have to do:

cd "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"

I've played around with sed, but I'm no expert on it and it ends up trying to cd to directory:

C:\Users\Gene\AppData\Local\Google\Chrome\User

which doesn't exist.

Ideas?

Thanks!
Avatar of Mazdajai
Mazdajai
Flag of United States of America image

You can use single quotes instead of double quotes  or use the sed to replace the backslash to forward slash / double backslash.

sed 's!\\!/!g'

Open in new window

sed 's!\\!\\\\!g'

Open in new window

Avatar of ugeb

ASKER

I'm actually looking for the whole thing.  What does the entire bash/sed line look like with the input I gave above?
echo "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"| sed 's!\\!\\\\!g'
Avatar of ugeb

ASKER

Thanks, but I guess I need to clarify.  I want to do this:

cdr "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"

where cdr is an alias for "~/scripts/cdr.sh", and cdr.sh is the shell script. What you gave is both fixed in terms of the directory string, and it doesn't change to that directory.  I already know how to change a string to the proper format, it's the changing of the directory that I can't figure out.

The following is the long way around that I took for cdr.sh in order to debug:

#!/bin/bash
DIR1=/cygdrive/$*
DIR2=`echo $DIR1 | sed -e 's:C:c:;s:\\\:\\/:g' | sed -e 's/://g'`
ls -halt $DIR2
cd $DIR2

Open in new window


I execute it as

./cdr.sh "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"

Open in new window


The 'ls' works, and the 'cd' doesn't.  That's what I can't figure out and need the help on.

Thanks.
You can't execute cd from a bash shell script. The script is obeyed by a separate bash process which changes directory but has no effect on the process that invoked it. You have to recode cdr as a function and source it (perhaps in .bashrc). Then it will work.
(CMD.EXE .BAT files can do CD, because the same process executes them (Windows process invocation is way slower than Linux))
Avatar of ugeb

ASKER

That's interesting, I didn't know I couldn't cd from a shell script.

I have two issues.  

1)  This is within Cygwin, which is a Linux-like environment and so obeys that syntax.  I'm not in a windows terminal.  How do I cd from a script within Linux?

2)  I still have issues with spaces in directory names.  Even escaping the spaces doesn't work.

Advice?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ugeb

ASKER

I'm pretty sure $* is what I wanted after reading the bash docs again, so I'm still having an issue with that.  But, I'll play around with it.

The function idea works great, though, so thank you for that!