I'm trying to check if the user input
I'm trying to check if the user input $1 exist in an array. And here is my code:
我正在尝试检查用户输入$ 1是否存在于数组中。这是我的代码:
array=(
aaa.bbb.ccc
ccc.ddd.aaa
)
echo ${array[@]} | grep -o -w "$1"
What I expect is only "aaa.bbb.ccc" or "ccc.ddd.aaa" can be found. However, in this case, like "aaa" or "ccc" can also pass the validation. Seems like the dot is not treated as part of the string.
我所期待的只能找到“aaa.bbb.ccc”或“ccc.ddd.aaa”。但是,在这种情况下,像“aaa”或“ccc”也可以通过验证。似乎点不被视为字符串的一部分。
Should I use regex or other grep option?
我应该使用正则表达式还是其他grep选项?
2 个解决方案
#1
3
It sounds like you want:
这听起来像你想要的:
- literal string matching
- against entire input lines.
文字字符串匹配
针对整个输入线。
Therefore, use -F (for literal matching) and -x for whole-line matching:
因此,使用-F(用于文字匹配)和-x用于整行匹配:
array=(
aaa.bbb.ccc
ccc.ddd.aaa
)
printf '%s\n' "${array[@]}" | grep -Fx "$1"
Now, only literal strings aaa.bbb.ccc and ccc.ddd.aaa will match, not substrings such as aaa.
Since you're matching the entire line, use of -o (to only output the matching part of each line) is unnecessary.
现在,只有文字字符串aaa.bbb.ccc和ccc.ddd.aaa匹配,而不是像aaa这样的子字符串。由于您匹配整行,因此不需要使用-o(仅输出每行的匹配部分)。
Also note how printf '%s\n' "${array[@]}" must be used to print each array element on its own line, whereas echo ${array[@]} prints the elements on a single line (with whitespace normalization due to word-splitting and potential globbing applied, because ${array[@]} is not enclosed in "...").
另请注意printf'%s \ n'“$ {array [@]}”必须如何用于在自己的行上打印每个数组元素,而echo $ {array [@]}在单行上打印元素(带有由于分词和应用潜在的globbing而导致的空白归一化,因为$ {array [@]}未包含在“...”中。
The problem with your approach:
您的方法存在问题:
grepdefaults to (basic) regular expressions, so a$1value containing.would result in that.being interpreted to mean "any single character".grep默认为(基本)正则表达式,因此包含的$ 1值。会导致这种情况。被解释为“任何单个字符”。
echo ${array[@]}is problematic due to potentially unwanted whitespace normalization and globbing, as stated;echo "${array[@]}" | grep -Fwo "$1"would be an improvement, but bears a slight risk of false positives.如上所述,echo $ {array [@]}是有问题的,因为可能不需要的空格规范化和通化。 echo“$ {array [@]}”| grep -Fwo“$ 1”将是一个改进,但具有误报的轻微风险。
#2
0
Escape the . with a preceding \, and it won't match aa but will match a.a:
逃避。使用前面的\,它将不匹配aa但会匹配a.a:
echo a.a | grep '\.'
Output:
a.a
To show this works with positional parameters, here's a little function that behaves much like the previous code:
为了证明这适用于位置参数,这里有一个小功能,其行为与前面的代码非常相似:
foo() { echo "$2" | grep "$1" ; }
foo '\.' a.a
Output:
a.a
exist in an array. And here is my code:I'm trying to check if the user input
I'm trying to check if the user input $1 exist in an array. And here is my code:
我正在尝试检查用户输入$ 1是否存在于数组中。这是我的代码:
array=(
aaa.bbb.ccc
ccc.ddd.aaa
)
echo ${array[@]} | grep -o -w "$1"
What I expect is only "aaa.bbb.ccc" or "ccc.ddd.aaa" can be found. However, in this case, like "aaa" or "ccc" can also pass the validation. Seems like the dot is not treated as part of the string.
我所期待的只能找到“aaa.bbb.ccc”或“ccc.ddd.aaa”。但是,在这种情况下,像“aaa”或“ccc”也可以通过验证。似乎点不被视为字符串的一部分。
Should I use regex or other grep option?
我应该使用正则表达式还是其他grep选项?
2 个解决方案
#1
3
It sounds like you want:
这听起来像你想要的:
- literal string matching
- against entire input lines.
文字字符串匹配
针对整个输入线。
Therefore, use -F (for literal matching) and -x for whole-line matching:
因此,使用-F(用于文字匹配)和-x用于整行匹配:
array=(
aaa.bbb.ccc
ccc.ddd.aaa
)
printf '%s\n' "${array[@]}" | grep -Fx "$1"
Now, only literal strings aaa.bbb.ccc and ccc.ddd.aaa will match, not substrings such as aaa.
Since you're matching the entire line, use of -o (to only output the matching part of each line) is unnecessary.
现在,只有文字字符串aaa.bbb.ccc和ccc.ddd.aaa匹配,而不是像aaa这样的子字符串。由于您匹配整行,因此不需要使用-o(仅输出每行的匹配部分)。
Also note how printf '%s\n' "${array[@]}" must be used to print each array element on its own line, whereas echo ${array[@]} prints the elements on a single line (with whitespace normalization due to word-splitting and potential globbing applied, because ${array[@]} is not enclosed in "...").
另请注意printf'%s \ n'“$ {array [@]}”必须如何用于在自己的行上打印每个数组元素,而echo $ {array [@]}在单行上打印元素(带有由于分词和应用潜在的globbing而导致的空白归一化,因为$ {array [@]}未包含在“...”中。
The problem with your approach:
您的方法存在问题:
grepdefaults to (basic) regular expressions, so a$1value containing.would result in that.being interpreted to mean "any single character".grep默认为(基本)正则表达式,因此包含的$ 1值。会导致这种情况。被解释为“任何单个字符”。
echo ${array[@]}is problematic due to potentially unwanted whitespace normalization and globbing, as stated;echo "${array[@]}" | grep -Fwo "$1"would be an improvement, but bears a slight risk of false positives.如上所述,echo $ {array [@]}是有问题的,因为可能不需要的空格规范化和通化。 echo“$ {array [@]}”| grep -Fwo“$ 1”将是一个改进,但具有误报的轻微风险。
#2
0
Escape the . with a preceding \, and it won't match aa but will match a.a:
逃避。使用前面的\,它将不匹配aa但会匹配a.a:
echo a.a | grep '\.'
Output:
a.a
To show this works with positional parameters, here's a little function that behaves much like the previous code:
为了证明这适用于位置参数,这里有一个小功能,其行为与前面的代码非常相似:
foo() { echo "$2" | grep "$1" ; }
foo '\.' a.a
Output:
a.a
exist