VIM:檢查外部程式是否正在執行的函數

VIM:檢查外部程式是否正在執行的函數

使用 vim 函數,我想使用 pgrep 檢查程式是否正在運行,如果沒有運行則執行某些操作。特別是,我想實現這樣的目標:

function! checkifrunning(mystring)
    if 'pgrep "mystring"' shows that something is NOT running
        --do something--
    endif
endfunction

我的猜測是我需要使用“system()”函數,但我不確定如何使用。有人可以幫忙嗎?

編輯:我想要一個特別使用 pgrep 的解決方案,而不是其他方式

答案1

function! checkifrunning(mystring)
    if !system('pgrep "' . a:mystring . '"')
        " --do something--
    endif
endfunction

從技術上來說!運行於數位,如果給定字串,則首先將字串轉換為數字。但是,如果沒有進程正在運行,則 的輸出pgrep將為空,轉換為數字時為 0。

相反'pgrep "' . a:mystring . '"',你也可以這樣做'pgrep ' . shellescape(a:mystring)

答案2

你可以使用像

:call system("pgrep process name ")

要列印輸出,

:echo system("pgrep process name ")

您可以將輸出儲存在變數中,例如

:let a=system("pgrep process name ")

並且,在任何循環、列印或使用字串函數中使用該變數。

相關內容