Regex 如何将命令行中的参数与一个正则表达式匹配?

Regex 如何将命令行中的参数与一个正则表达式匹配?,regex,linux,bash,shell,parameter-passing,Regex,Linux,Bash,Shell,Parameter Passing,我有一个脚本,我想禁止命令行中的一些命令(shutdown、rm、init)。但它似乎不起作用,因为它似乎匹配所有东西: 我怎么能这么做 [root@devnull hunix]# cat p.sh #!/bin/bash string=$1; if [[ "$string" =~ [*shut*|*rm*|*init*] ]] then echo "command not allowed!"; exit 1; fi [root@devnull hunix]# ./p.sh shut

我有一个脚本,我想禁止命令行中的一些命令(shutdown、rm、init)。但它似乎不起作用,因为它似乎匹配所有东西: 我怎么能这么做

[root@devnull hunix]# cat p.sh
#!/bin/bash

string=$1;

if [[ "$string" =~ [*shut*|*rm*|*init*] ]]
then
  echo "command not allowed!";
  exit 1;
fi
[root@devnull hunix]# ./p.sh shutdown
command not allowed!
[root@devnull hunix]# ./p.sh sh
command not allowed!
[root@devnull hunix]# ./p.sh rm
command not allowed!
[root@devnull hunix]# ./p.sh r
command not allowed!
[root@devnull hunix]#

你把shell glob和regex混在一起了

正确的正则表达式是:

if [[ "$string" =~ ^(shut|rm|init) ]]; then
  echo "command not allowed!"
  exit 1
fi

你把shell glob和regex混在一起了

正确的正则表达式是:

if [[ "$string" =~ ^(shut|rm|init) ]]; then
  echo "command not allowed!"
  exit 1
fi