grep 不接受 getopts
grep doesn't take getopts
有一个目录:
.
├── file4.txt
├── file5.yml
├── txt
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
└── yml
└── file6.yml
一些文件包含一个词 hello
:
>> grep -r 'hello' .
./file4.txt:hello
./yml/file6.yml:hello
./txt/file1.txt:hello
./txt/file2.txt:hello
./file5.yml:hello
现在我想写一个脚本,它会在不同的位置和文件中搜索 hello
,即:
$ grep -EHori --exclude-dir={txt,yml} --include="*.txt" 'hello' .
./file4.txt:hello
$ grep -EHori --exclude-dir={txt,yml} --include="*.yml" 'hello' .
./file5.yml:hello
$ grep -EHori --exclude-dir={yml} --include="*.yml" 'hello' .
./yml/file6.yml:hello
./file5.yml:hello
$ grep -EHori --exclude-dir={txt} --include="*.yml" 'hello' .
./yml/file6.yml:hello
./file5.yml:hello
$ grep -EHori --exclude-dir={txt} --include="*.txt" 'hello' .
./file4.txt:hello
./txt/file1.txt:hello
./txt/file2.txt:hello
我有:
#!/bin/bash
exclude_path="txt"
file_types="*.txt"
include_path="./"
check_path="./"
while getopts ":e:t:i:p" opt; do
case $opt in
e)
exclude_path="${OPTARG}"
;;
t)
file_types="${OPTARG}"
;;
i)
include_path="${OPTARG}"
;;
p)
check_path="${OPTARG}"
esac
done
result=$(grep -EHori --exclude-dir=$exclude_path \
--include=$file_types 'hello' "$check_path")
echo $result
但是,它不适用于 exclude_path
和 include_path
的多个值,即:
grep -r --exclude-dir={dir1,dir2,dir3} --include={type1,type2,type3} keyword /path/to/search
此外,如果我使用 -p
,grep 会抱怨 No such file
。
$ ./grep.sh
./file4.txt:hello
$ ./grep.sh -t *.yml
./file5.yml:hello
$ ./grep.sh -p yml -t *.yml
grep: : No such file or directory
$ ./grep.sh -t *txt,*.yml
我确实需要将 result
保存为一个变量,因为我会进一步使用它。我想我应该使用 eval
和 grep
和转义变量,但我不确定。
您的 getopt 语句中有错字:
while getopts ":e:t:i:p" opt; do
应该是
while getopts ":e:t:i:p:" opt; do
没有最后一个 :
p
不接受任何参数,所以当你输入 -p yml
它执行 grep ... ''
因为 check_path=""
因为 OPTARG 是p
为空:
$ bash -x ./grep.sh -p yml -t '*.yml'
...
++ grep -EHori --exclude-dir=txt '--include=*.txt' hello ''
grep: : No such file or directory
...
固定脚本输出:
$ ./grep.sh -p yml -t '*.yml'
yml/file6.yml:hello
旁注:请记住正确包含您的参数,键入 ./grep.sh -p yml -t *.yml
将执行 ./grep.sh -p yml -t file5.yml
,因为 *
被 bash 扩展。可能你打算将它包含在 '
中,否则 ./grep.sh -p yml -t *.yml
returns 只是空行(来自 echo
),因为 file5.yml
不在 [=27= 内]目录。
问题已解决,适用于
eval grep -EHori --exclude-dir={$exclude_path} \
--include={$file_types} 'hello' "$check_path"
有一个目录:
.
├── file4.txt
├── file5.yml
├── txt
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
└── yml
└── file6.yml
一些文件包含一个词 hello
:
>> grep -r 'hello' .
./file4.txt:hello
./yml/file6.yml:hello
./txt/file1.txt:hello
./txt/file2.txt:hello
./file5.yml:hello
现在我想写一个脚本,它会在不同的位置和文件中搜索 hello
,即:
$ grep -EHori --exclude-dir={txt,yml} --include="*.txt" 'hello' .
./file4.txt:hello
$ grep -EHori --exclude-dir={txt,yml} --include="*.yml" 'hello' .
./file5.yml:hello
$ grep -EHori --exclude-dir={yml} --include="*.yml" 'hello' .
./yml/file6.yml:hello
./file5.yml:hello
$ grep -EHori --exclude-dir={txt} --include="*.yml" 'hello' .
./yml/file6.yml:hello
./file5.yml:hello
$ grep -EHori --exclude-dir={txt} --include="*.txt" 'hello' .
./file4.txt:hello
./txt/file1.txt:hello
./txt/file2.txt:hello
我有:
#!/bin/bash
exclude_path="txt"
file_types="*.txt"
include_path="./"
check_path="./"
while getopts ":e:t:i:p" opt; do
case $opt in
e)
exclude_path="${OPTARG}"
;;
t)
file_types="${OPTARG}"
;;
i)
include_path="${OPTARG}"
;;
p)
check_path="${OPTARG}"
esac
done
result=$(grep -EHori --exclude-dir=$exclude_path \
--include=$file_types 'hello' "$check_path")
echo $result
但是,它不适用于 exclude_path
和 include_path
的多个值,即:
grep -r --exclude-dir={dir1,dir2,dir3} --include={type1,type2,type3} keyword /path/to/search
此外,如果我使用 -p
,grep 会抱怨 No such file
。
$ ./grep.sh
./file4.txt:hello
$ ./grep.sh -t *.yml
./file5.yml:hello
$ ./grep.sh -p yml -t *.yml
grep: : No such file or directory
$ ./grep.sh -t *txt,*.yml
我确实需要将 result
保存为一个变量,因为我会进一步使用它。我想我应该使用 eval
和 grep
和转义变量,但我不确定。
您的 getopt 语句中有错字:
while getopts ":e:t:i:p" opt; do
应该是
while getopts ":e:t:i:p:" opt; do
没有最后一个 :
p
不接受任何参数,所以当你输入 -p yml
它执行 grep ... ''
因为 check_path=""
因为 OPTARG 是p
为空:
$ bash -x ./grep.sh -p yml -t '*.yml'
...
++ grep -EHori --exclude-dir=txt '--include=*.txt' hello ''
grep: : No such file or directory
...
固定脚本输出:
$ ./grep.sh -p yml -t '*.yml'
yml/file6.yml:hello
旁注:请记住正确包含您的参数,键入 ./grep.sh -p yml -t *.yml
将执行 ./grep.sh -p yml -t file5.yml
,因为 *
被 bash 扩展。可能你打算将它包含在 '
中,否则 ./grep.sh -p yml -t *.yml
returns 只是空行(来自 echo
),因为 file5.yml
不在 [=27= 内]目录。
问题已解决,适用于
eval grep -EHori --exclude-dir={$exclude_path} \
--include={$file_types} 'hello' "$check_path"