From windows to Linux paths
Hi there
During the weekend I was bombarded by hundreds of thousands of millions of emails asking how to do the reverse transformation of paths, from windows to Linux. I've got the following script (called windows_path):
Code:
#!/bin/bash
server_name=${@//\\/\/}
projects_name=/mnt/server/projects
quotes_name=/mnt/server/quotes
server_name=${current_dir/Z:/$projects_name}
server_name=${server_name/Y:/$quotes_name}
echo $server_name
However there are a couple of things to be aware of. First, this script wants the windows path as a parameter (in my case it would be usually pasted from an email). But although it works fine doing a straight invocation from the command line ...
Code:
pabloa$ windows_path Y:\Batch 6 - June 24 2011
/mnt/server/quotes/Batch 2 - June 24 2011
... most of the time I want the result to be passed onto some other command, and then it breaks:
Code:
pabloa$ cd $(windows_path Y:\Batch 6 - June 24 2011)
bash: cd: /mnt/servidor/quotes/Batch: No such file or directory
So we need to "protect" the result with quotation marks. Then it works fine:
Code:
pabloa$ cd "$(windows_path Y:\Batch 6 - June 24 2011)"
pabloa$ pwd
/mnt/servidor/quotes/Batch 6 - June 24 2011
I've tried to solve this slight inconvenience replacing echo $server_name with echo ${server_name// /\ } and even echo ${server_name// /\\ } to no avail. Maybe someone out there knows of a good way of dealing with this.
By the way, a very good source of information for the string substitutions I've done in this and the previous scripts can be found here: Manipulating Strings, highly enjoyable reading.
Cheers.
P.
Re: From windows to Linux paths
Following the thrilling saga of working with Windows paths under Linux, here there is an extra twist that I'm sure will be appreciated by the masses.
A worthy addition to the script posted earlier is this:
Code:
echo $server_name
# add the following
cd "$server_name"
bash
And then after running the script, we will be placed in the directory given as parameter. I couldn't find other way of doing this. There is no way (that I could find) of staying in the new directory when the script returns.
Cheers.
P.
Re: From windows to Linux paths
Quote:
...I was bombarded by hundreds of thousands of millions of emails...
Loved that...;)
Re: From windows to Linux paths
Quote:
Originally Posted by
Cotty
Loved that...;)
... and I've read every single one of them!
Cheers.
P.