Skip to content

Instantly share code, notes, and snippets.

@anya-tokugawa
Last active February 19, 2021 12:07
Show Gist options
  • Save anya-tokugawa/3312ef924c525af60d5128826f6b12af to your computer and use it in GitHub Desktop.
Save anya-tokugawa/3312ef924c525af60d5128826f6b12af to your computer and use it in GitHub Desktop.
Windowsからダブルクリックで、Windows上にあるシェルスクリプトを実行する方法
@echo off
REM ====================================================================================
REM Windowsの *.sh ファイルをダブルクリックで実行する方法 - How to run a shell script file on Windows by double-clicking on it.
REM HowTo: 予めこのファイルを *sh拡張子のデフォルトで開くアプリにしておく。
REM ----------------------------------
REM Author: Yuto Nakagawa, Licence: unlicenced/public-domain
REM ====================================================================================
REM スクリプトの絶対パスを取得[0]
set ProgramName=%1
REM 囲んでいるダブルクォーテーションをシングルクォーテーションに変換(バックスラッシュのエスケープを防ぐため)[1]
set QuoteFixed=%ProgramName:"='%
REM Windowsスタイルの絶対パスをWSLスタイルに変換する - wslpath [2]
REM 出力結果は、PathFixedに入れる。[3]
FOR /F "DELIMS=" %%A IN ('wsl.exe -- wslpath %QuoteFixed%') DO SET PathFixed=%%A
REM 実行する
bash.exe -c %PathFixed%
REM ====================================================================================
REM 引用文献 - Ref:
REM [0] https://teratail.com/questions/48882
REM [1] https://stackoverflow.com/questions/1964192/removing-double-quotes-from-variables-in-batch-file-creates-problems-with-cmd-en
REM [2] https://superuser.com/questions/1113385/convert-windows-path-for-windows-ubuntu-bash
REM [3] https://syon.github.io/refills/rid/1498316/
REM ====================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment