[英]How can I match parameters in command line with one regular expression?
我有一個腳本,並且我想禁止命令行中的某些命令(關閉,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與正則表達式混合在一起。
正確的正則表達式為:
if [[ "$string" =~ ^(shut|rm|init) ]]; then
echo "command not allowed!"
exit 1
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.