linux指令如何在行尾傳遞參數

linux指令如何在行尾傳遞參數

當我在終端機中發出命令時,我想在行尾傳遞一個參數。

所以我想要像下面的例子這樣的東西

find /path/to/directory -type f -exec grep -irl "SEARCH_PATTERN" {} \;

像這樣的格式:

find /path/to/directory -type f -exec grep -irl "$1" {} \; < "SEARCH_PATTERN"

這樣我就不必每次都去改變零件中的指令 -irl "SEARCH_PATTERN"。相反,我會在行尾更輕鬆地給出它。

先致謝

答案1

建立一個函數:

mygrep() { find /path -type f -exec grep -irl "$1" {} +; }

mygrep waldo

事實上,為什麼需要find

mygrep() { grep -irl "$1" /path; }

答案2

對於這種特殊情況,我現在唯一能想到的就是創建 SEARCH_PATTERN 環境變量,並在每次想​​要使用它時嘗試它。

快速範例;

$ SEARCH_PAT="abc"
$ find /path -type f -exec grep -irl "${SEARCH_PAT}" {} \;
$ SEARCH_PAT="xyz"
$ find /path -type f -exec grep -irl "${SEARCH_PAT}" {} \;

ETC...

相關內容