简体   繁体   中英

Bash script: Using grep to find a string that is also an option

I call the program with the text I want to find, so programname '-r'

Then, within the script I have text="${1}"

find . -r -name "hi.*" -exec grep -l "${text}" {} \;

The second half of that simplifies to grep -l -r and it waits for another input

How do I specify that -r is the string to be found, and not an option?

Add -- after the -l [or your last valid option]. That stops option processing in grep so that your text will be interpreted as a string to search for and not an option:

find . -r -name "hi.*" -exec grep -l -- "${text}" {} \;

POSIX标准要求grep支持选项-e ,该选项强制将以下参数视为正则表达式,而不是另一个选项。

find . -r -name "hi.*" -exec grep -l -e "$text" {} \;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM