-1

I'm writing a bash script. I need to introduce a colon after the first occurrence of a character. /c/Users/ertwr/org/readme.html and in order to open them with the native Windows browser the need a colon after the unit letter /c:/Users/ertwr/org/readme.html

I use the following expression which works remarkably well, even for multiple files or whitespaces, but only for the specified unit since I can't store the unit letter in the regular expression.

#!/usr/sbin/env bash 
IFS=$'\n'
${HOME}/scoop/shims/chrome "${@/\/\c\//\c\:\/}"

The problem is it only works for the C letter the way it's written. So I need a way to make it work independently for ANY letter.

Things I've tried and don't work for multiple files or files with spaces:

- can't capture the regex

I really want this one to work, but I'm doing something wrong.

#!/usr/sbin/env bash
IFS=$'\n'

regex=(\/[a-zA-Z]\/)
if [[ "$@" =~ "$regex" ]]; then
    ${HOME}/scoop/shims/chrome "${@/$BASH_REMATCH/${BASH_REMATCH}\:}"
fi

- sed append

#!/usr/sbin/env bash 
IFS=$'\n'
WADDR=`sed 's|\<[a-zA-Z]\>|&:|' "$@"`
${HOME}/scoop/shims/chrome "${WADDR}"

Sed particularly feeds the content of the file as url, instead of the name of the file. While it works when using a text file as input.

1
  • 1
    For Windows programs you can just use normal Windows style path. For example, here notepad.exe opens a file /c/Windows/System32/notepad.exe "C:\path\to\file". Don't forget to mention the double quote.
    – Biswapriyo
    Oct 13, 2019 at 14:18

2 Answers 2

4

To convert WSL style path to Windows style, use -w option with wslpath binary. By default the mounted path in WSL is /mnt so I choose it in example. You can remove the /mnt part from the path.

For example,

  • Command: wslpath -w "/mnt/c/Windows/System32"
  • Output: C:\Windows\System32

For more valid options just execute wslpath and it will show the help page:

wslpath: Invalid argument
Usage:
    -a    force result to absolute path format
    -u    translate from a Windows path to a WSL path (default)
    -w    translate from a WSL path to a Windows path
    -m    translate from a WSL path to a Windows path, with '/' instead of '\'

EX: wslpath 'c:\users'

Here is an example of simple shell script to open a text file in Notepad:

#! /bin/sh

read -p "Enter WSL style path: " variable
echo "Opening file in Notepad.exe"
/mnt/c/Windows/System32/Notepad.exe $(wslpath -w "$variable")

Here is the modified shell script from question:

#!/bin/sh
IFS=$'\n'
echo "${@}"
WADDR="$(/bin/wslpath -w "${@}")"
/mnt/c/Windows/System32/Notepad.exe "${WADDR}"

Further details:

0
0

who knew? I solved it on my own.

#!/usr/sbin/env bash
IFS=$'\n'

if [[ "$@" =~ \/([a-zA-Z])\/ ]]; then 
    ${HOME}/scoop/shims/chrome "${@/$BASH_REMATCH/\/${BASH_REMATCH[1]}\:\/}"
fi

So, to make it compatible with an NT URI, to feed it to a binary, I just need to remove one of the matched / like this.

#!/usr/sbin/env bash
IFS=$'\n'

if [[ "$@" =~ \/([a-zA-Z])\/ ]]; then 
    ${HOME}/scoop/shims/chrome.exe "${@/$BASH_REMATCH/${BASH_REMATCH[1]}\:\/}"
fi

And without self-up vote and white kinght upvotes. Just code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .