1

回声“大家好!”

我需要检查 linux 脚本的输入参数是否符合我的安全需求。它应该只包含 az 字符、0-9 位数字、一些空格和“+”号。例如:“3 分钟做 r51+r11”

这对我不起作用:

if grep -v '[0123456789abcdefghijklmnopqrstuvwxyz+ ]' /tmp/input; then
  echo "THIS DOES NOT COMPLY!";
fi

有什么线索吗?

4

2 回答 2

0

You are telling grep:

Show me every line that does not contain [0123456789abcdefghijklmnopqrstuvwxyz+ ]

Which would only show you lines that contains neither of the characters above. So a line only containing other characters, like () would match, but asdf() would not match.

Try instead to have grep showing you every line that contains charachter not in the list above:

if grep '[^0-9A-Za-z+ ]' file; then

If you find something that's not a number or a letter or a plus, then.

于 2013-10-28T15:39:08.463 回答
0

您想测试整行(假设 /tmp/input 中只有一行),而不仅仅是任何地方的单个字符是否匹配,因此您需要将其锚定到行的开始和结束。试试这个正则表达式:

^[0123456789abcdefghijklmnopqrstuvwxyz+ ]*$

请注意,您可以使用范围缩短它:

^[0-9a-z+ ]*$

于 2013-10-28T15:38:56.077 回答