在BSD系统上工作,并且很难搜索变量以查看其是否包含多个不同的子字符串中的任何一个。这就是我的工作原理:
> echo $lenck if test "${lenck#*$pattern1}" != "$lenck";
> then
> if test "${lenck#*$pattern2}" != "$lenck";
> then
> echo "Invalid or corrupted IP or process"
> echo $lenck | tee -a invalid.txt
> else
> pat2out=$lenck
> if test "${pat2out#*$pattern3}" != "$pat2out";
> then
> echo "Invalid or corrupted IP or process"
> echo $pat2out | tee -a invalid.txt
> else
> pat3out=$pat2out
> if test "${pat3out#*$pattern4}" != "$pat3out";
> then
> echo "Invalid or corrupted IP or process"
> echo $pat3out | tee -a invalid.txt
> else
> echo $pat3out | tee -a processed.txt
> fi
> fi
> fi
>
> else
> echo "Invalid or corrupted IP or process"
> echo $lenck | tee -a invalid.txt fi
问题是我需要能够搜索四种以上的模式。尝试了grep -e和其他方法,但是它们没有正确将字符串过滤到正确的文件中。任何帮助表示赞赏。
答案 0 :(得分:0)
似乎所有测试实际上都是针对相同的输入字符串进行测试的,因此您可以使用case statement:
echo "$lenck"
case "$lenck" in
*$pattern1*|*$pattern2*|*$pattern3*)
echo "Invalid or corrupted IP or process"
echo "$lenck" >> invalid.txt
;;
*)
echo "$lenck" >> processed.txt
;;
esac