[英]How to write RegEx in Shell Script?
如果任何输入参数包含可能的 shell 注入命令,如何在 Shell 脚本中编写 RegEx 以使退出代码为 1 的脚本失败。 一两个例子就足够了。 请帮忙。
这是代码片段:
invalid_format="^.*[;&|].*$"
invalid_format2="rmdir"
if [ "$LOCAL_DIR" =~ "$invalid_format" -o "$LOCAL_DIR" =~ "$invalid_format2" ];
then
echo "Error! LOCAL_DIR cannot contain command chaining characters like ; && ||"
exit 1
fi
但是,它失败并出现错误:
[: too many arguments
使用grep
并将您的字符串通过管道传输到grep
,然后检查返回值。
#!/bin/bash
STRING_TO_CHECK="abc"
REGEX="b"
echo "$STRING_TO_CHECK" | grep -E "$REGEX"
if [ $? -eq 1 ]
then
# regex not found
else
# regex found
fi
谢谢大家..在这里我完成了。
invalid_format="(^.[;&|].$)|(\brmdir\b)|(\bls\b)|(rm\s-)|(\bping\b)"
if [[ $LOCAL_DIR =~ $invalid_format ]]; then
echo "Error! LOCAL_DIR cannot contain command chaining characters like ; && || or possible shell injection commands"
exit 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.