Cygwin執行Windows捷徑檔案(.LNK)

Cygwin執行Windows捷徑檔案(.LNK)

我使用 Cygwin 作為 Windows 上的 cmd 替代品,並且正在清理系統 PATH 變數。

我現在有一個包含 exe 和快捷方式 (.LNK) 檔案的資料夾,該資料夾位於 PATH 中,其中包含我從命令列使用的所有小型應用程式和軟體。

一切都可以透過 CMD 運行,但是快捷方式、.LNK 檔案不能透過 Cygwin 運行。相反我得到

bash: /cygdrive/e/Apps/uniserver.lnk: 無法執行二進位文件

我唯一的猜測是因為它認為 .lnk 應該是符號連結?

有沒有辦法讓 Cygwin 啟動快捷方式?

答案1

您可以使用該實用程式從 Cygwin 執行 Windows LNK 文件cygstart,該實用程式是cygutils 包, 如下:

cygstart [OPTION]... FILE [ARGUMENTS]

cygstart --help了解可用選項。

對於您的情況,以下內容應該足夠了:

cygstart /cygdrive/e/Apps/uniserver.lnk

答案2

我已經做了類似的設置,並想是否可以節省.lnk每次打字的時間。嘗試過 command_not_found_handle從 Bash 4.0 開始添加,看起來可以工作:

# add this to your .bashrc
command_not_found_handle ()
{
    if [[ $1 == *.* || $1 == */* ]]; then
        echo "$1: command not found"
        return 127
    fi

    local binbase=/cygdrive/e/Apps/
    local name=$1
    shift

    # You might want to tweak precedence
    if [[ -f ./$name.bat ]]; then
        exec "./$name.bat" "$@"
    elif [[ -f ./$name.lnk ]]; then
        cygstart "./$name.lnk" "$@"
    elif [[ -f $binbase/$name.bat ]]; then
        exec "$binbase/$name.bat" "$@"
    elif [[ -f $binbase/$name.lnk ]]; then
        cygstart "$binbase/$name.lnk" "$@"
    else
        echo "$name: command not found"
        return 127
    fi
}

例如,鍵入只會uniserver觸發此鉤子並發現/cygdrive/e/Apps/uniserver.lnk要啟動。

編輯:另一種方法是從整個 $PATH 中找到捷徑。

command_not_found_handle ()
{
    if [[ $1 == *.* || $1 == */* ]]
    then
        echo "$1: command not found"
        return 127
    fi

    local name=$1
    shift

    if [[ -f ./$name.bat ]]
    then
        exec "./$name.bat" "$@"
    elif [[ -f ./$name.lnk ]]
    then
        start "./$name.lnk" "$@"
    elif [[ -f $(type -P $name.bat) ]]
    then
        exec "$(type -P $name.bat)" "$@"
    elif [[ -f $(type -P $name.lnk) ]]
    then
        cygstart "$(type -P $name.lnk)" "$@"
    else
        echo "$name: command not found"
        return 127
    fi
}

答案3

使用DOS內建指令start。不過,我猜 Cygwin 無法存取 DOS 內建程序,因此您必須編寫包裝器,例如 mystart.bat,並使用包裝器腳本啟動您的 LNK。

我不認為您可以在 Cygwin 中「關聯」LNK 檔案以使 Cygwin 自動啟動您的包裝器,但也許其他人可以建議一種方法來做到這一點。

相關內容