Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2810

WSL から open でウェブサーフィン

$
0
0

以前、こんな記事WSL から openを書きました。
そしたら、今日上がったdotfiles Advent Calendar 2019 の 18 日目の記事WSLにもそこそこ使えるopenが欲しい!で、色々と改善する方法を教えてもらえたので早速取り入れたいと思います。

改善点は以下のとおりです。
1. ググった結果をopen
2. URLをopen
3. 複数入力に対応

注意

powershell.exe start

Import-Module : The specified module 'C:\tools\poshgit\dahlbyk-posh-git-4184928\src\posh-git.psd1' was not loaded because no valid module file was found in any module directory.
At C:\Users\username\OneDrive\写真\ドキュメント\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:2 char:1
+ Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-4184928\src\posh-git ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (C:\tools\poshgi...c\posh-git.psd1:String) [Import-Module], FileNot FoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

と怒られたので使っていません。

実装

if[[$(uname-r)=~ Microsoft ]];then
    local Chrome='/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe'function open(){if[$# -eq 0 ];then
            echo"ERROR: get no input argument."echo"Please specify file-paths, URLs, googling-words"echo"open [file/path]"return 1
        fi
        for arg;do
            if[-e"${arg}"];then
                readlink-f${arg} | xargs wslpath -m | xargs cmd.exe /c start
            elif[[${arg}=~ http ]];then
                echo"${arg}" | xargs -I{} bash -c"${Chrome} '{}'"else
                echo"${arg}" | sed's/ /+/g' | xargs -I{} bash -c"${Chrome} 'https://www.google.com/search?q={}'"fi
        done}fi

実行例

open "hello world" https://www.google.co.jp https://qiita.com "wsl open" ~/Downloads

実行結果

  • chrome(インターネット・ブラウザ)
    • hello world の検索結果 (google.com)
    • google.com
    • qiita.com
    • wsl open の検索結果 (google.com)
  • explore(ファイル・ブラウザ)
    • ダウンロード(シンボリックリンクを事前に設定している必要があります)

zsh の safix alias を使えば、pptやpdfも開けます。参考:WSL から open

以前との違い

以前詳細に記事を書いたので、以前と変えていない箇所については説明しません。参考:WSL から open

no input error

if[$# -eq 0 ];then
    echo"ERROR: get no input argument."echo"Please specify file-paths, URLs, googling-words"echo"open [file-paths/URLs/googling-words]"return 1
fi
  • 入力がない場合はエラーを返す

複数の入力に対応

for arg;do
    if[-e"${arg}"];then
        readlink-f${arg} | xargs wslpath -m | xargs cmd.exe /c start
    fi
done

Chrome で開く

local Chrome='/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe'for arg;do
    if[[${arg}=~ http ]];then
        echo"${arg}" | xargs -I{} bash -c"${Chrome} '{}'"else
        echo"${arg}" | sed's/ /+/g' | xargs -I{} bash -c"${Chrome} 'https://www.google.com/search?q={}'"fi
done

おわりに

  • Chrome='/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe'はブラウザであれば変えても多分大丈夫
  • powershell.exe startを使ったほうがいい理由と使えない理由が知りたい
  • wsl 内からフォルダを開こうとすると、怒られた(開けるけど)
'\\wsl$\Ubuntu-18.04\home\wsl_username'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

Viewing all articles
Browse latest Browse all 2810

Trending Articles